Page 1 of 1

dynamic checkbox dilema (how to $_GET dymanic names?!)

Posted: Fri Sep 01, 2006 1:37 am
by robster
Hi there, I draw part of my form by taking dynamic info directly from the database. Here is an example:

Code: Select all

//for the GROUPING
			//================
			mysql_select_db($dbname);
			$sql = "SELECT * FROM grouping ORDER BY group_id ASC";
			$content = mysql_query($sql);
			$Xcontent = mysql_fetch_array($content);	
			$cShowMax = mysql_num_rows($content);
			
			for ($y=1; $y<=$cShowMax; $y++)
			{ 
				$group_id = $Xcontent["group_id"];
				$group_name = $Xcontent["group_name"];
				$group_type = $Xcontent["group_type"];
				
				
				echo "<td align = \"center\"><input name=\"$group_name\" type=\"checkbox\" value=\"$group_id\" checked /><br />$group_name</td>";
				
				
			$Xcontent = mysql_fetch_array($content);
			}
			mysql_free_result($content);	
			?>

This works a treat and draws a whole bunch of checkboxes (pre-checked) and when I refresh the form, yes, there are all the names in the url. Here is an example url (trimmed down to only show the checkbox data):

Code: Select all

&Hulabaloo=13&Beauty+Products=14&Beauty+Services=15&Beauty+Rent=16&Cutting=17&Colour=18&Training=19&Treatments=20&Makeup=25&Makeup+Service=26
What I want to be able to do is, whilst the form is drawn with above loop, check if the checkbox is ticked or not (so I can uncheck it or check it depending on its status last time through).

Whilst I could setup a whole bunch of $_GET's this won't really get me anywhere as the user can change those group names at will, including deletion or adding of new ones (as many as they'd like).

How can I get around this? I really feel very stuck!!!

:|

All ideas greatly appreciated,


Rob

Posted: Fri Sep 01, 2006 1:41 am
by RobertGonzalez
First things first, you are not referencing a numerical index in the for loop. You are also reading the $content result into the same array twice: once in the loop and once before it.

As it relates to the checkboxes... are you getting this information from the Database or from user input? How are you checking for whether the checkboxes are checked or not?

Posted: Fri Sep 01, 2006 1:52 am
by robster
OK, I've fixed it so I have a numerical index, I am now putting $group_id into the name of the checkbox. I presume that's what you meant.
Here's how it looks now, and its output below:

Code: Select all

//for the GROUPING
			//================
			mysql_select_db($dbname);
			$sql = "SELECT * FROM grouping ORDER BY group_id ASC";
			$content = mysql_query($sql);
			$Xcontent = mysql_fetch_array($content);	
			$cShowMax = mysql_num_rows($content);
			
			for ($y=1; $y<=$cShowMax; $y++)
			{ 
				$group_id = $Xcontent["group_id"];
				$group_name = $Xcontent["group_name"];
				$group_type = $Xcontent["group_type"];
				
				
				echo "<td align = \"center\"><input name=\"$group_id\" type=\"checkbox\" value=\"1\" checked /><br />$group_name</td>";
				
				
			$Xcontent = mysql_fetch_array($content);
			}
			mysql_free_result($content);

Code: Select all

&13=1&14=1&15=1&16=1&17=1&18=1&19=1&20=1&25=1&26=1
Imagine 1 means yes, active, and in the future, 0 will mean no, inactive.


RE: the $content result, if I don't put it at the start then in the loop it doesn't move ahead through the array, it just sits on the first result and repeats it on every lap through the loop. It's how I've always done it, and if I'm doing it wrong I'd love to learn what it is I'm doing. I tried removing it and it did just as I described, the first content repeated itself.
are you getting this information from the Database or from user input? How are you checking for whether the checkboxes are checked or not?
The info comes from MQSQL to draw into the form. The user can then uncheck whatever they don't want and submit the form. The idea is, the form submits and re-sends the post data, this time, the form checks/unchecks itself dependant on the post data.

It's a technique I've used for a long time with single choice form widgets with much success (radio buttons, drop downs etc) and pre-set checkboxes. The problem is, now I have dynamic checkboxes I have no idea how to handle the info and check the names of what has been sent, as I'll never really know as it's dynamic from the database.

Puzzled and confused...


Rob

Posted: Fri Sep 01, 2006 2:34 am
by RobertGonzalez

Code: Select all

<?php
//for the GROUPING
//================
/* Error check things like this... often */
if (!mysql_select_db($dbname))
{
    die('Could not select the ' . $database . ' database: ' . mysql_error());
}

/* Run a query and check for a good result */
$sql = 'SELECT * FROM grouping ORDER BY group_id ASC';
if (!$content = mysql_query($sql))
{
    die('Could not execute the grouping query: ' .mysql_error());
}

/* Set some vars and values */
$Xcontent = array();
$Xcontent_count = 0;
while ($Xcontent[] = mysql_fetch_array($content))
{
    $Xcontent_count = count($Xcontent);
}

/* If we have a result */
if ($Xcontent_count > 0)
{
    /* Loop it babay, yeah */
    for ($i = 0; $ i < $Xcontent_count; $i++)
    {
        /* This would be an excellent place to check for a 1 or 0 */
        $group_id = $Xcontent[$i]["group_id"];
        $group_name = $Xcontent[$i]["group_name"];
        $group_type = $Xcontent[$i]["group_type"];
                               
        echo '<td align = "center"><input name="' . $group_name . '" type="checkbox" value="' . $group_id . '" checked /><br />' . $group_name . '</td>';
    }

    mysql_free_result($content);
}
?>

Posted: Fri Sep 01, 2006 3:38 am
by Jenk
RE: checking for checked check boxes check checking check.

If they are in the $_* s'global, they are checked. If they are not set, they were not checked.

Posted: Fri Sep 01, 2006 5:53 pm
by feyd
I'd suggest sticking them into a known location, specifically..

Code: Select all

echo '<td align = "center"><input name="choices[]" type="checkbox" value="' . $group_name . '" checked="checked" /><br />' . $group_name . '</td>';
or similar.