Project Management Software - Debt Consolidation - Bollywood India forum movie reviews - Debt Consolidation - Debt Consolidation

PDA

View Full Version : php code to check boxes based on user's mysql data?


jeffhale
Jul 5th 2009, 2:14 pm
Does anyone now how to take a user's info from a database and display it in checkboxes, with the checkboxes corresponding to certain variables either checked or not, depending on the user's data?

I'm rather new to PHP and mysql.
Any help is much appreciated.

wd_2k6
Jul 5th 2009, 5:29 pm
Well to return data from a database you use a SELECT (http://www.tizag.com/mysqlTutorial/mysqlquery.php)query
and then you get this returned data and echo it out in a form as normal, and you can use an if statement to decide whether to check a box or not.

You should get used to running querys to query your database and echoing out data from a database as they take a simple format and they are easy to get used to.

zeronese
Jul 5th 2009, 8:29 pm
If you are looking for some sample code, let me give you an example of what wd_2k6 tried to explain (if he does not mind :) )
I will suppose that your database has yes for checked and no for not checked (could be anything else such as 1 or 0)
So you would do something like this
<?php
//database connection
$query = “select values from your_table where your_condition";
$result = mysql_query($query);

echo '<form action="" method="post">';
while($row = mysql_fetch_array($result)){
if($row['value1'] == "yes")
echo '<input name="value1" type="checkbox" value="" checked />Value1';
else
echo '<input name="value1" type="checkbox" value="" />Value1';

// do the same thing for the other values
.
.
.
.

}//end while
echo '</form>';
?>
hope this helps:)

Edgar_fox
Jul 6th 2009, 3:30 am
Is there any simplier methods? because this one looks a bit hard, i mean too much mess

wd_2k6
Jul 6th 2009, 5:14 am
Course I don't mind :)

@Edgar_fox this is the basic standard way to take information out of a database, you retrieve it and loop through it, there's nothing else to it. Once you understand this concept it's easy.

jeffhale
Jul 6th 2009, 9:33 pm
thank you all!