Forum Moderators: coopster

Message Too Old, No Replies

array problem

         

kristie380

4:46 am on Apr 30, 2007 (gmt 0)

10+ Year Member



Ok what I'm trying to do here is make it so that when people log in to edit their accounts, their previous selections will remain checked in the form so they don't have to go back and re-check every selection before submitting the form. But I can't seem to figure out what I'm doing wrong. Here is a sample piece of my code:

while ($newArray = mysql_fetch_array($result))
{

$activities = $newArray['activities'];

$activity = explode(",", $activities);

if ('$activity[]' == "Academic Decathalon") {
echo "<input name=\"activities[]\" type=\"checkbox\" value=\"Academic Decathalon\" checked>";
} else {
echo "<input name=\"activities[]\" type=\"checkbox\" value=\"Academic Decathalon\" >";
}

}

cameraman

5:58 am on Apr 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have two problems in your if statement. First, because of the single quotes on the left, you're comparing two literal strings against each other which will never be equal. Second, the syntax you're using on the left is used to create a new element in an array and assign a value to it. Try using in_array() [us.php.net].

if (in_array("Academic Decathalon",$activity)) {

You can also shorten this up a bit:
while ($newArray = mysql_fetch_array($result))
{

$activities = $newArray['activities'];

$activity = explode(",", $activities);
echo '<input name="activities[]" type="checkbox" value="Academic Decathalon"';
if (in_array("Academic Decathalon",$activity)) echo ' checked';
echo ' >';
}

Surrounding your echo with single quotes allows you to place double-quotes inside without escaping them, saving some typing and making it easier to read. Just using the if to add checked saves some more typing (or pasting :)).

kristie380

5:47 pm on Apr 30, 2007 (gmt 0)

10+ Year Member



This works great except that it is only returning 2 of the elements in the array instead of all of them. How do I get it to return all of the array?

cameraman

6:44 pm on Apr 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't see anything in the code snippet you posted that would cause that behaviour. Could you provide some more info?

kristie380

6:53 pm on Apr 30, 2007 (gmt 0)

10+ Year Member



Sure - I have about 60 options for people to check off. I also tried using array_search but that didn't change anything. So basically if someone has 5 options checked, it is only showing 2 of the options as checked.

$sql = "SELECT * FROM `signup2` WHERE username = '".$_SESSION['username']."' AND password = '".$_SESSION['password']."'";
$result = mysql_query($sql,$db);

while ($newArray = mysql_fetch_array($result))
{

$id = $newArray['id'];
$firstname = $newArray['firstname'];
$maidenname = $newArray['maidenname'];
$lastname = $newArray['lastname'];
$activities = $newArray['activities'];

$activity = explode(",", $activities);

if (in_array("Academic Decathalon",$activity)) {
echo "<input name=\"activities[]\" type=\"checkbox\" value=\"Academic Decathalon\" checked>";
} else {
echo "<input name=\"activities[]\" type=\"checkbox\" value=\"Academic Decathalon\" >";
}
echo "Academic Decathalon</td>
<td width=\"178\">";

if (in_array("Aerospace Club",$activity)) {
echo "<input name=\"activities[]\" type=\"checkbox\" value=\"Aerospace Club\" checked>";
} else {
echo "<input name=\"activities[]\" type=\"checkbox\" value=\"Aerospace Club\" >";
}

echo "Aerospace Club </td>
<td width=\"173\">";
}

cameraman

10:41 pm on Apr 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's only showing two because you're only checking for two of them in your code snippet - you'll need to do that for each and every one of the options you present. You could streamline it a little by placing the options in an array that you can use for both checking and displaying:

$options = array('Academic Decathalon','Aerospace Club','Bagel Making','Crochet Mastery','Demonstrative Speaking');

$sql = "SELECT * FROM `signup2` WHERE username = '".$_SESSION['username']."' AND password = '".$_SESSION['password']."'";
$result = mysql_query($sql,$db);

while ($newArray = mysql_fetch_array($result))
{

$id = $newArray['id'];
$firstname = $newArray['firstname'];
$maidenname = $newArray['maidenname'];
$lastname = $newArray['lastname'];
$activities = $newArray['activities'];

$activity = explode(",", $activities);
foreach($options as $one_option) {
if (in_array($one_option,$activity)) {
echo "<input name=\"activities[]\" type=\"checkbox\" value=\"$one_option\" checked>";
} // EndIf this activity checked
else {
echo "<input name=\"activities[]\" type=\"checkbox\" value=\"$one_option\" >";
} // EndElse this activity not checked
echo "$one_option</td>
<td width=\"178\">";
} // EndForEach possible activity
} // EndWhile getting records

If you want to maintain your variable widths you could store those in the array as well, although it gets a little more complex:
$options = array('Academic Decathalon' => 178,'Aerospace Club' => 173,'Bagel Making' => 160,'Crochet Mastery' => 170,'Demonstrative Speaking' => 180);
The foreach becomes:
foreach($options as $one_option => $width) {
and the line with the cell width:
<td width=\"$width\">";