Page 1 of 1

array problem

Posted: Tue May 01, 2007 2:54 pm
by kristie380
I have a form with checkboxes that I want users to be able to log back in and edit whenever needed and I want their previous selections to remain checked. The problem I'm having is that my code will only return the 2nd and 3rd entry in my array. So if a person previously had 5 options checked, my form is only showing 2 of the options checked. I can't seem to figure out what I'm doing wrong. I have about 60 options in my form but have shortened it to just 2. See below...

Code: Select all

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

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

$activities = $newArray['activities'];

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

$arrayValues = array("$activity[0]","$activity[1]","$activity[2]","$activity[3]","$activity[4]","$activity[5]","$activity[6]","$activity[7]","$activity[8]","$activity[9]","$activity[10]");


if (in_array("Academic Decathalon",$arrayValues))  {
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",$arrayValues))  {
echo "<input name=\"activities[]\" type=\"checkbox\" value=\"Aerospace Club\" checked>";
 } else {
echo "<input name=\"activities[]\" type=\"checkbox\" value=\"Aerospace Club\" >";
}
}

Posted: Tue May 01, 2007 3:10 pm
by feyd
An optimization with regards to your database design may be in order. Unless "activities" is a SET, I would probably suggest adding two more tables: one for the various activities, and one to link the user and activity they are involved with.

Posted: Tue May 01, 2007 3:47 pm
by RobertGonzalez
I agree with feyd. Fix the database. In the meantime, can you get away with something like this?

Code: Select all

<?php
$sql = "SELECT * 
        FROM `signup2` 
        WHERE username = '{$_SESSION['username']}' 
        AND password = '{$_SESSION['password']}'";

$result = mysql_query($sql,$db) or die(mysql_error());

$activitie = array();
while ($newArray = mysql_fetch_array($result))
{
    $activities = $newArray['activities'];
}

$arrayValues = array();
if (!empty($activites))
{
    $activity = explode(",", $activities);
    for ($i = 0; $i < count($activity); $i++)
    {
        $arrayValues[] = $activity[$i];
    }
}

$appendField = '';
for ($i = 0; $i < count($arrayValues); $i++)
{
    $appendField = 'Academic Decathalon' == $arrayValues[$i] ? ' checked="checked"' : '';
    echo '<input name="activities[]" type="checkbox" value="Academic Decathalon"' . $appendField . '>';
    echo 'Academic Decathalon</td><td width="178">';
    
    $appendField = 'Aerospace Club' == $arrayValues[$i] ? ' checked="checked"' : '';
    echo '<input name="activities[]" type="checkbox" value="Aerospace Club"' . $appendField . '>';
}
?>

Posted: Tue May 01, 2007 4:00 pm
by kristie380
I previously had my database set up with a separate field for each activity. But it was just so many fields I was trying to find a way to shorten it. I will play with it a bit more and if I still have no success, I will stick with my current method. In the meantime, any other suggestions are welcome! Thanks!

Posted: Tue May 01, 2007 8:16 pm
by feyd
Unless I misread Everah's post, we're not even remotely referring to separate fields for the activities. Records is what we're talking about. Note how I specifically said "adding two more tables."