[PHP] Selecting checkboxes based on SQL query
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
Re: [PHP] Selecting checkboxes based on SQL query
On Thu, Feb 23, 2012 at 7:49 PM, Rick Dwyer rpdw...@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 How about this: input type=checkbox name=cb[ ] value=1 ?php if(in_array(array('cb' = 1))) echo checked; ?... input type=checkbox name=cb[ ] value=2 ?php if(in_array(array('cb' = 2))) echo checked; ?... input type=checkbox name=cb[ ] value=3 ?php if(in_array(array('cb' = 3))) echo checked; ?... input type=checkbox name=cb[ ] value=4 ?php if(in_array(array('cb' = 4))) echo checked; ?... etc - Matijn -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Selecting checkboxes based on SQL query
On Thu, Feb 23, 2012 at 8:07 PM, Matijn Woudt tijn...@gmail.com wrote: On Thu, Feb 23, 2012 at 7:49 PM, Rick Dwyer rpdw...@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 How about this: input type=checkbox name=cb[ ] value=1 ?php if(in_array(array('cb' = 1))) echo checked; ?... input type=checkbox name=cb[ ] value=2 ?php if(in_array(array('cb' = 2))) echo checked; ?... input type=checkbox name=cb[ ] value=3 ?php if(in_array(array('cb' = 3))) echo checked; ?... input type=checkbox name=cb[ ] value=4 ?php if(in_array(array('cb' = 4))) echo checked; ?... etc - Matijn Uh, this should of course be in_array(array('cb' = 1, $sqlArray)) - Matijn -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Selecting checkboxes based on SQL query
On Thu, Feb 23, 2012 at 8:49 PM, Rick Dwyer rpdw...@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
Re: [PHP] Selecting checkboxes based on SQL query
I should have been more explicit in my description... The SQL command that returns the array is not the same one that creates the checkboxes they are two different sql queries and I would prefer to keep them that way. I actually have it working for a form submit with a custom function I got off the web. But the custom function relies on the array it's working on to look like this: [cb] = Array ( [0] = 1 [1] = 6 [2] = 2 [3] = 4 [4] = 3 ) So, to use my existing function, how do I get the following array to look like the above one: Array ( [0] = Array ( [cb] = 2 ) [1] = Array ( [cb] = 6 ) [2] = Array ( [cb] = 1 ) ) --Rick On Feb 23, 2012, at 2:08 PM, Fatih P. wrote: On Thu, Feb 23, 2012 at 8:49 PM, Rick Dwyer rpdw...@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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Selecting checkboxes based on SQL query
On Thu, Feb 23, 2012 at 9:30 PM, Rick Dwyer rpdw...@earthlink.net wrote: I should have been more explicit in my description... The SQL command that returns the array is not the same one that creates the checkboxes they are two different sql queries and I would prefer to keep them that way. I actually have it working for a form submit with a custom function I got off the web. But the custom function relies on the array it's working on to look like this: [cb] = Array ( [0] = 1 [1] = 6 [2] = 2 [3] = 4 [4] = 3 ) So, to use my existing function, how do I get the following array to look like the above one: Array ( [0] = Array ( [cb] = 2 ) [1] = Array ( [cb] = 6 ) [2] = Array ( [cb] = 1 ) ) --Rick On Feb 23, 2012, at 2:08 PM, Fatih P. wrote: On Thu, Feb 23, 2012 at 8:49 PM, Rick Dwyer rpdw...@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 read the manual!
Re: [PHP] Selecting checkboxes based on SQL query
On 02/23/2012 11:30 AM, Rick Dwyer wrote: So, to use my existing function, how do I get the following array to look like the above one: Array ( [0] = Array ( [cb] = 2 ) [1] = Array ( [cb] = 6 ) [2] = Array ( [cb] = 1 ) ) $d = results from SQL query $a = array(); foreach ($d AS $r) $a[] = $r['cb']; -- Jim Lucas http://www.cmsws.com/ http://www.cmsws.com/examples/ http://www.bendsource.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php