Jump to content

[SOLVED] I only get the last part of an array


tryingtolearn

Recommended Posts

Hi again,

 

I am trying to preselect all the checkboxes on an edit form if they are part of the record in the database

 

I put this to get an array of categories that pertain to that record being edited.

 

$res2 = mysql_query ("SELECT id FROM cat_associations WHERE tem_id=$tid");
while (list($cat) = mysql_fetch_row($res2)) {
  		 if ($cat) $cats=array($cat);
	 }

 

Then the checkboxes are added to the form with this

 

$row1 = array();
$rows = array();
$res = mysql_query ("SELECT id, title, parent FROM categories  ORDER BY title");
while (list($id, $title, $parent) = mysql_fetch_row($res)) {
if ($parent) $rows[$parent][$id] = $title; else $row1[$id] = $title;

}

echo "<table border=\"1\" cellpadding=\"5\" cellspacing=\"0\" width=\"100%\">\n";
echo '<tr><td><b>', implode('</b></td><td><b>', $row1), '</b></td></tr>';
echo "<tr valign=\"top\">\n";
foreach ($row1 as $k => $v) {
echo"<td>\n";
if (isset($rows[$k])) {	
	foreach ($rows[$k] as $id => $title){ 	
		if (in_array($id, $cats)){
	echo"<input type=\"checkbox\" name=\"cat_x[]\" value=\"$id\" checked>$title<br>";
	}else{
	echo"<input type=\"checkbox\" name=\"cat_x[]\" value=\"$id\" >$title<br>";
	}
	}

	}
echo "</td>\n";
}
echo "</tr>\n";
echo "</table>\n";

 

The part that is throwing me is this

if (in_array($id, $cats)){
	echo"<input type=\"checkbox\" name=\"cat_x[]\" value=\"$id\" checked>$title<br>";
	}else{
	echo"<input type=\"checkbox\" name=\"cat_x[]\" value=\"$id\" >$title<br>";
	}
	}

 

If the array from the top contains 10 11 12 51

I am only getting the last one (51) checked in the form.

 

Is there a way to have it so it checks all of the boxes in the array?

 

This sounds confusing - I hope I am explaining it OK.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.