php code to check boxes based on user's mysql data?

Discussion in 'PHP' started by jeffhale, Jul 5, 2009.

  1. #1
    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.
     
    jeffhale, Jul 5, 2009 IP
  2. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Well to return data from a database you use a SELECT 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.
     
    wd_2k6, Jul 5, 2009 IP
  3. zeronese

    zeronese Peon

    Messages:
    83
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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>';
    ?>
    PHP:
    hope this helps:)
     
    zeronese, Jul 5, 2009 IP
  4. Edgar_fox

    Edgar_fox Active Member

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    86
    #4
    Is there any simplier methods? because this one looks a bit hard, i mean too much mess
     
    Edgar_fox, Jul 6, 2009 IP
  5. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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.
     
    wd_2k6, Jul 6, 2009 IP
  6. jeffhale

    jeffhale Active Member

    Messages:
    56
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    58
    #6
    thank you all!
     
    jeffhale, Jul 6, 2009 IP