Forum Moderators: coopster
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
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++;
}
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>";
}
}
$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>";
}
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++;
}
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>";
}
$sql = "SELECT * FROM table";
$rows = mysql_query($sql);
print '<pre>';
while (row = mysql_fetch_array($rows)) {
print_r($row);
}
print '</pre>';
?>
<select name="optionList">
// The name of our submit button is 'Submit':
if (isset($_POST['Submit'])) {
print '<pre>';
print_r($_POST);
print '</pre>';
}