array problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kristie380
Forum Commoner
Posts: 36
Joined: Sun Oct 09, 2005 10:51 pm

array problem

Post 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\" >";
}
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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 . '>';
}
?>
kristie380
Forum Commoner
Posts: 36
Joined: Sun Oct 09, 2005 10:51 pm

Post 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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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."
Post Reply