Checkbox Array Question - Iteration

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I am developing a page where people check off items for which they would
like to volunteer. For now, this page has around 50 checkboxes on it, but it
could conceivably change frequently and grow much larger than the current 50
items once it goes live.

I have all of the checkboxes arranged into nice little arrays and named
things like name="kitchen[waitstaff]" and name="kitchen[cook]",
name="professional[paint]". I take the checked boxes, serialize them, and
store them in columns in MySQL that bear the same name as the array (e.g.,
kitchen, professional). All of this works fine and I'm very happy with it.

Now, when the person revisits the page to update his/her selections, I query
the database and unserialize the column data. I then search the resulting
array and if the name of the checkbox is found in the array, I echo
"checked" to select the checkbox. See below:

<snip>
	while($row = mysql_fetch_array($curRecord)){
		$vid=$row["vid"];
		$pID=$row["pID"];
		$kitchen = unserialize($row["kitchen"]);
		$kitchenevt = unserialize($row["kitchenevt"]);
		$profesional = unserialize($row["professional"]);
		.
		.
		.	
	}

	//BUNCHA' IRRELEVENT STUFF CUT OUT OF HERE

	<span class="bodyBold">General Kitchen Help</span><br>
	<?php
		echo "<input type=\"checkbox\" name=\"kitchen[waitstaff]\"";
		if(in_array("waitstaff",$kitchen)){
			echo "checked";
		}
		echo ">&nbsp;<span
class=\"body\">Waiter/Waitress</span><br>";
	

		echo "<input type=\"checkbox\" name=\"kitchen[cook]\"";
		if(in_array("cook",$kitchen)){
			echo "checked";
		}
		echo ">&nbsp;<span class=\"body\">Cook</span><br>";
		.
		.
		.
		//AND SO ON FOR EVERY SINGLE CHECKBOX.
	?>

</snip>
	
So, having set up a sample section of checkboxes on my page using the logic
in the snippet above, I am wondering if there is a much more efficient way
to look at the arrays that get returned from the database, check to see if
the given checkbox is contained therein, then, if it is, echo "checked". The
solution snippet above works just fine, but whenever I find myself
contemplating doing the same thing over and over in code, that's obviously a
flag for investigating an iterative solution.

Right now, though, I'm struggling with trying to noodle out whether setting
up some sort of iterative function would be more work than simply doing what
I'm doing now. Obviously, iteration is always great for handling repetitive
work, but I'm just not having much luck coming up with a solution.

I am aware that this is probably slightly off-topic since it isn't a direct
php/db question, but I was hoping somebody might be kind enough to offer up
suggestions as to how I might construct such a solution anyway.

Thanks in advance for you time.


Rich

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux