Jump to content

[SOLVED] Listing mysql results in a <select> or checkbox item


Anzeo

Recommended Posts

Hi,

 

 

 

I'm currently developping my own user system and so far I've completed my registration and profile but now I'm stuck on a problem at my edit file. I do'nt have a clue on how to insert mysql entries, like the birthday of the user back into the drop-down list (<select>) or options they choosed back into the checkboxes.

 

Does anyone know how to do this? I do know how to put mysql entries back into textfields but I can't figure out how to do this for the other form items.

 

TIA

Well, my users use a list like this

 

<select name="Name" id="ID">
<option value="0" selected> </option>
...

 

To insert their birthdate in my db. Now when you click edit profile I'd like to have their birthday show in the lists so that it shows like:

 

[2] / [9] / [1960]

 

with the '[]' representing the <select> lists.

 

The same goes for the checkboxes. The user can select some option to adjust the site to his likes and when editting your profile I want the option you've chosen to be selected and the ones you didn't to be deslected

 

perhaps

<?php
function dropDown ($name, $min, $max, $current) {
    $str = "<select name='$name'>\n";
    $opts = range($min, $max);
    foreach ($opts as $opt) {
        $sel = $opt==$current ? 'selected' : '';
        $str .= "<option $sel> $opt</option>\n";
    }
    $str .= "</select>\n";
    return $str;
}

$dob = '2/9/1960';
list ($m, $d, $y) = explode('/', $dob);

echo dropDown('month', 1, 12, $m), ' / ',  dropDown('day', 1, 31, $d) , ' / ',  dropDown('year', 1907, 2007, $y);
?>

i'd do:

<?php
        $sql = "SELECT * FROM your_table";
        $query = mysql_query($sql) OR die(mysql_error());

        echo "<form action=\"\" method=\"post\">\n";
        echo "<select name=\"drop_down\">\n";
        while($row = mysql_fetch_array($query)){
                echo "<option value=\"{$row['cloumn1']}\"". (($row['column1'] == $_POST['drop_down']) ? (" SELECTED") : ("")) .">{row['column2']}\n";
        }
        echo "<input type=\"submit\" value=\"Submit\">\n";
        echo "</form>\n";
?>

Thanks for your replies. I can't check if their working right now as I'm at school, but I'll be able to try the scripts by sunday.

 

@boo_lolly: does your script checks if that's the selected option? I can't realy tell, as I'm currently learning PHP myself and have only 'mastered' the basics this far.

 

I appreciate your time and help

 

Grtz,

Anzeo

Thanks for your replies. I can't check if their working right now as I'm at school, but I'll be able to try the scripts by sunday.

 

@boo_lolly: does your script checks if that's the selected option? I can't realy tell, as I'm currently learning PHP myself and have only 'mastered' the basics this far.

 

I appreciate your time and help

 

Grtz,

Anzeo

 

yes it does =) this is the part that checks to see if it's selected:

(($row['column1'] == $_POST['drop_down']) ? (" SELECTED") : ("")) 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.