Forum Moderators: coopster

Message Too Old, No Replies

To much?

multi checkbox

         

Gruessle

6:23 am on Mar 10, 2005 (gmt 0)

10+ Year Member



Hi
I am over my head here.

I am trying to generate checklists from an array and at the same time display the previously checked once as checked.

I have to use $states[] to get the array but I can't figure out how when name is already a variable ($abbrev).

$a is a list of States from the db like this:
$qstate = mysql_query("SELECT State FROM programs WHERE (id = $selectedid)") or die ("Could not read data because ".mysql_error());

$a=mysql_fetch_array($qstate);

==========================

$i = 1;
foreach ($usstates as $abbrev => $full)
{
if ($a[$abbrev])
{
echo "<INPUT type='checkbox' name='$abbrev' checked}> $abbrev";
} else {
echo "<INPUT type='checkbox' name='$abbrev'> $abbrev";
}
$i++;
}

Thanx
Dennis

Gruessle

6:39 am on Mar 10, 2005 (gmt 0)

10+ Year Member



Here is the select box version and this doesn't work either.

if ($selectedid) {
$qstate = mysql_query("SELECT State FROM programs WHERE (id = $selectedid)")
or die ("Could not read data because ".mysql_error());
$a=mysql_fetch_array($qstate);
}
$i = 1;
foreach ($usstates as $abbrev => $full) {
if ($a[$abbrev])
{
echo "<option value='$abbrev' selected>$abbrev</option>";
} else {
echo "<option value='$abbrev'>$abbrev</option>";
}
$i++;
}

Gruessle

7:37 am on Mar 10, 2005 (gmt 0)

10+ Year Member



Still working on this, have found one more solutions but I can't get it to work.
Have the feeling I am doing the same thisg wrong on each one of them.

Here it is:

if ($selectedid) {
$qstate = mysql_query("SELECT State FROM programs WHERE (id = $selectedid)")
or die ("Could not read data because ".mysql_error());
$a=mysql_fetch_array($qstate);
}

$i = 1;
foreach ($usstates as $abbrev => $full)
{
if (array_key_exists($abbrev, $a))
{
echo "<option value='$abbrev' selected>$abbrev</option>";
}
else
{
echo "<option value='$abbrev'>$abbrev</option>";
}
}

Gruessle

9:35 pm on Mar 10, 2005 (gmt 0)

10+ Year Member



Maybe I am not getting an answer on this because I posted more then once?

Still looking for help on this - Thanx

coopster

11:31 pm on Mar 10, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'll often use something similar to this ...
$selectedOption = (isset($_POST['optionList']))? $_POST['optionList'] : ''; 
$optionList = '';
foreach ($usstates as $abbrev => $full) {
$optionList .= "<option value=\"$full\"";
if ($full === $selectedOption) {
$optionList .= ' selected="selected"';
}
$optionList .= ">$abbrev</option>";
}

Gruessle

5:44 am on Mar 11, 2005 (gmt 0)

10+ Year Member



Thank you I will try that right now but I got it working some funny way.

Take a look: I have to implode then explode then I have everything 2 times in there so I have to do array_unique. And I guess that was my main problems. For everything else I found many solutions. I guess I still don't understand what I get back from mySQL with mysql_fetch_array

$a=mysql_fetch_array($qstate);
$a = implode(',', $a);
$a = explode(',', $a);
$a = array_unique($a);
}

if (key_values_intersect($abbrev,$a))
{
$r = 1;
foreach ($a as $selabbrev)
{
echo "<option value='$selabbrev' selected>$selabbrev</option>";
$r++;
}
}
$i = 1;
foreach ($usstates as $abbrev => $full)
{
echo "<option value='$abbrev'>$abbrev</option>";
$i++;
}

Gruessle

6:12 am on Mar 11, 2005 (gmt 0)

10+ Year Member



I like yours better and tried to get it to work but I don't understand.
I have seen this type of code before, and believe that it must be very usefull, could you explain this to me?

I have two arrays
$a -> which is a few states from the database
$usstates -> all states
then I have
$abbrev and $full
$full is the full name of the state from $usstates
$abbrev is the 2 letter code name of each state from $usstates

Now what is?
$selectedOption
and what is?
$_POST['optionList']

$selectedOption = (isset($_POST['optionList']))? $_POST['optionList'] : '';
$optionList = '';
foreach ($usstates as $abbrev => $full) {
$optionList .= "<option value=\"$full\"";
if ($full === $selectedOption) {
$optionList .= ' selected="selected"';
}
$optionList .= ">$abbrev</option>";
}

coopster

11:32 am on Mar 11, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Think of a result set being returned from the database as a "spreadsheet" of data. There are columns and rows and you loop through them, row-by-row, processing each row with your fetch statements. A nice way to understand how data is being returned from a database call is to use PHP's print_r() [php.net] function. It will much easier to read if you output the data in HTML <pre> tags too.
$sql = "SELECT * FROM table"; 
$rows = mysql_query($sql);
print '<pre>';
while (row = mysql_fetch_array($rows)) {
print_r($row);
}
print '</pre>';
?>

The example I showed is merely that, an example. The $selectedOption is a variable that is being set to store the option that the user selected in the form and $_POST['optionList'] is the actual form value being returned. Of course, the select option in your HTML would have to be named "optionList" like this
<select name="optionList">

A nice way to understand how data is being returned from your forms is to once again use PHP's print_r() [php.net] function.
// The name of our submit button is 'Submit': 
if (isset($_POST['Submit'])) {
print '<pre>';
print_r($_POST);
print '</pre>';
}