Inserting a check in checkbox from array value


Posted on 16th Feb 2014 07:03 pm by admin

Hello all,
I have a bit of code that works for a select option box, but does not work for my checkboxes. I need multiple selections on this code and I really don't like the look of multiple select option box. The code does insert the word checked on the first value in the array, but only that one box even if there are multiple values brought from the database. This works good:<select name="time">" .
$times = array("12AM" => "0:00", "1AM" => "1:00", "2AM" => "2:00", "3AM" => "3:00", "4AM" => "4:00", "5AM" => "5:00", "6AM" => "6:00",
"7AM" => "7:00", "8AM" => "8:00", "9AM" => "9:00", "10AM" => "10:00", "11AM" => "11:00", "12PM" => "12:00", "1PM" => "13:00",
"2PM" => "14:00", "3PM" => "15:00", "4PM" => "16:00", "5PM" => "17:00", "6PM" => "18:00", "7PM" => "19:00", "8PM" => "20:00",
"9PM" => "21:00", "10PM" => "22:00", "11PM" => "23:00");
foreach ($times as $k => $v)
{
if ($v == $time){
$select = " selected";
}
else
{
$select = " ";
}

echo "<option value="" .$v . """ . $select . ">" . $k . "</option>";
}
echo "</select>";
Why doesn't this:$formats = array("Discussion" => "Discussion,", "Basic Text Study" => "Basic Text Study,", "It Works Study" => "It Works Study,",
"IP Study" => "IP Study,", "Topic" => "Topic,", "Step Study" => "Step Study,", "Tradition Study" => "Tradition Study,",
"Candlelight" => "Candlelight,", "Speaker" => "Speaker,", "Birthday" => "Birthday,", "Men" => "Men,",
"Women" => "Women,", "Varies" => "Varies,", "Other" => "Other,");
echo "<table>";
//set 3 to 4 of you want 4 columns. Set it to 5 if you want 5, etc
$numcols = 3; // how many columns to display
$numcolsprinted = 0; // no of columns so far
//Query for display.php
foreach($formats as $k => $v)
{
if ($numcolsprinted == $numcols)
{
echo "</tr>n<tr>n";
$numcolsprinted = 0;
}
if ($v == $format){
$select = " checked";
}
else
{
$select = " ";
}
// output row from foreach
echo "<td><input type="checkbox" name="format[]" value="" . $v . """ . $select . ">" . $k . "</td>n";
// bump up row counter
$numcolsprinted++;
} // end while loop
$colstobalance = $numcols - $numcolsprinted;
for ($i=1; $i<=$colstobalance; $i++) {
echo "<td></td>n";
}
Thanks for any help.

No comments posted yet

Your Answer:

Login to answer
193 Like 45 Dislike
Previous forums Next forums
Other forums

remove trailing slash from array
I have the following array which contains trailing slashes I need to remove. What is the best way to

mysql query with single quotes in a variable
$sitedetails = "INSERT INTO vars (address, sitename, description, ownername, theme) VALUES ('$u

LSB (PHP 5.3) problem with static value!
hello,

i'm having a problem. static::$text variable gets lost at some point. can someone plea

Need help to identify this error please
:confused:Can anyone help me tell what this error message means? Maybe tell me where to look to fix

I face problems to extract data from one table and insert it in another one
Hi,

For modifications, I have to extract data from one table and insert it in another one

help connecting a form to php and then emailing the form
Ok. So, I made a form, and I need help to where when submitted, I get an email with the submitted de

Date/Time and Checkbox
Hi Everyone,

I am trying to figure out how to insert the current date and tim

urldecode question
How would I format this line of code properly?

<?php echo urldecode($_GET['Title']); ?

finding key position of specified value in multi-dimensional array
Code: $openedfile = fopen($tfc_file4, "r");
$rc = 1;
while(!feof($openedfile))
{<

mysqli_fetch_assoc returns multiple arrays, how to return a single array
Hello, I am using mysqli_fetch_assoc which is returning multiple rows, so it looks like:

Code

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash