Re: Selecting checkboxes based on SQL query
| From: | Fatih P. | Date: | Thu, 23 Feb 2012 19:08:48 +0000 |
| Subject: | Re: Selecting checkboxes based on SQL query | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-316768@lists.php.net to get a copy of this message | ||
On Thu, Feb 23, 2012 at 8:49 PM, Rick Dwyer <rpdwyer@earthlink.net> wrote:
> Hello all.
>
> I perform a SQL query like the following:
>
> $sql = 'select * from my_table where id="10"
>
> It returns the the following array for 3 records:
>
>
> Array ( [0] => Array ( [cb] => 2 ) [1] => Array ( [cb] => 6 ) [2] => Array
> ( [cb] => 1 ) )
>
> The values of CB in the above array are the values of html checkboxes on a
> page.
>
> input type="checkbox" name="cb[ ]" value="1"...
> input type="checkbox" name="cb[ ]" value="2"...
> input type="checkbox" name="cb[ ]" value="3"...
> input type="checkbox" name="cb[ ]" value="4"... etc
>
> If the above array's cb value matches the value of a checkbox on the page,
> I need the default state of the checkbox to be checked.... so I know I'm
> going to have some ternary logic in each html checkbox. But I don't know
> how to create a custom function from the above array to provide that logic.
> I've tried some tutorials, but with no success as the array I am receiving
> is not like those in the tutorials.
>
> Any help would be greatly appreciated.
>
> Thank you.
>
>
> --Rick
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
replace the name part as following
input type="checkbox" name="cb[put_the_id_here]" value="1".
when dumping into html from array
foreach ($sql_result as $value) {
if ($value == $selected) {
input type checkbox name=cb[$value['id']] value=$value['id']
checked=checked
} else {
input type checkbox name=cb[$value['id']] value=$value['id']
}
}
hope this helps