Jump to content

[SOLVED] array help


dennismonsewicz

Recommended Posts

I have the following script that echoes out a checkbox like this:

 

<?php $checkbox_qry = mysql_query("SELECT * FROM has_had_projects WHERE project = '" . $results->project . "'")or die(mysql_error());
	$field = mysql_num_fields($checkbox_qry);
		while($row = mysql_fetch_assoc($checkbox_qry)) {													
			for($i = 2; $i < $field; $i++) {
			$names = mysql_field_name($checkbox_qry, $i);	
			$chk = $row[$names]==1?'checked="checked"':'';
			$numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);																	
			$title .= '<div><input type="checkbox" name="checkboxes[' . $names . ']" class="checkbox" id="' . $names . '" ' . $chk . ' value="1"  /> <label for="checkboxes[' . $names . ']">' . ucwords(str_replace($numbers, '', $names)) . '</label></div>';
			}
		echo $title;
} ?>

 

Is there anyway to save all of the names in an array and when the form is submitted the script will traverse through the array and print out the names of the checkboxes that weren't checked?

Link to comment
Share on other sites

Just so you know, if you use mysql_fetch_row instead of mysql_fetch_assoc, you don't need to use mysql_field_name.  You can just write $row[1] and $row[2] (or $row[$i]).

 

To solve your problem, what you can do is upon receiving the data (before you initially insert it into the database), create an array with all of the checked items, something like:

 

$checked_items = array('item1', 'item3', 'item4');

 

Then, serialize() and escape the array.  What serialize() does is turn your array into a string that can later be turned back into an array with unserialize().  The reason you want to do this is that you can't save an array into a database, but you can save it in its serialized form because serialize() returns a string representation.

 

$serialized_array = serialize($checked_items);

 

Make sure that before you put it in the database that you escape any quotes or mysql-unfriendly characters.

 

 // this step must be done after the mysql_connect() function
$serialized_array = mysql_real_escape_string($serialized_array);
// insert $serialized array into database

 

When you want to retrieve the array, just use unserialize() on that serialized string and it will reconstruct the array that you originally had.

 

 

As far as your question about checking check-boxes, it's much easier to do it by including Javascript. 

 

Here's an example:

 

<html>
<body>
<form name="myform">
<input type="checkbox" name="first" />
<input type="checkbox" name="second" />
</form>
</body>
<script language="Javascript" type="text/javascript">
document.myform.second.checked = true;
</script>
</html>

 

If you run this code, you'll see that the second checkbox is checked.  In my Javascript, since I've already printed out all of the checkboxes unchecked in the HTML, all I had to do was specify which checkbox I want checked. It would be very easy to use a PHP forloop to generate the "document.myform.WHATEVER.checked = true;" over the array that you unserialized, which contained the names of the boxes that were checked.

 

Hope that make sense.

Link to comment
Share on other sites

ok its really early for me lol... too many late nights burning the midnight oil! Gotta love pizza and Mt. Dew!

 

What I am trying to do is this:

 

I am printing out the checkboxes. And it checks the DB for a value of 1. If it is 1 then it checks the box. If it returns a 0 then it leaves the box unchecked. The reasoning behind this, is that I have built a custom CMS that allows project managers to go in an add/delete/edit projects and the checkboxes allows for the PM to see where a project is in the process. What happens is, is that a box is checked at a certain point in the project but if you go in and try to edit the checkbox and say, the PM made a mistake and the project isn't that far in the process and needs to try to backup and uncheck a box it is not updating the DB to set its checked value to 0.

 

Make sense? I need it when a user unchecks the box to update the value in the DB with a 0 if it was a 1.

Link to comment
Share on other sites

what I mean is something like

 

$myarray = array();
$i = 0;
while ($newrow = mysql_fetch_row($chkbox_query)) {
$myarray[$i][1] = $newrow[1];
$myarray[$i][2] = $newrow[2];
$i++;
}

 

Then, you'll have an array with everything you need, so it might be easier to work with the html outputting.  You can use a for loop or foreach loop to get the items back out.

Link to comment
Share on other sites

change

$title .= '<div><input type="checkbox" name="checkboxes[' . $names . ']" class="checkbox" id="' . $names . '" ' . $chk . ' value="1"  /> <label for="checkboxes[' . $names . ']">' . ucwords(str_replace($numbers, '', $names)) . '</label></div>'

to

$title .= '<div><input type="hidden" name="checkboxes[' . $names . ']" value="0"><input type="checkbox" name="checkboxes[' . $names . ']" class="checkbox" id="' . $names . '" ' . $chk . ' value="1"  /> <label for="checkboxes[' . $names . ']">' . ucwords(str_replace($numbers, '', $names)) . '</label></div>'

and tray to print_r($_POST['checkboxes']);

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.