Forum Moderators: coopster
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\" >";
}
}
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 :)).
$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\">";
}
$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\">";