Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

mySQL PHP dropdown loop

Codes here !

Moderators: macek, egami, gesf

mySQL PHP dropdown loop

Postby Johnny010 » Mon Sep 17, 2012 6:38 am

Good afternoon.

I have been trying to get an HTML dropdown box with the values being passed over via POST to anoter .php

The dropdown box is meant to be filled with the data from a mySQL table, but after trawling online for about 4 hours, I have given up and now have made my way here :).

The mySQL db is called jonnossi_Site and the table with the data (1 column, varchar(15)) is called characters.

The code I have so far (I am quite the noob at this so please ignore the bad coding :P :

Code: Select all
<?php
include "../sys/security.php";   

$FLTRpass=$_POST['FLTRpass'];

If($FLTRpass!="FLTRpass") {die ("incorrect password");}

?>

<HTML>
<BODY>
FILTER OPTIONS <br>
<form action="filtereddata.php" method="post">
<select name="charname">
<? include "connect.php";
$result=mysql_query("SELECT * FROM characters");
 
while($row=mysql_fetch_array($result))
{
// Here is where I can not seem to get the contents of the table 'characters' to be echoed in to the drop down box. I want them in the form of <option=$result[0]>$result[0]</option>......etc....etc...
}
?>
</select>

<input type="submit" value="Filter the Data">

</form>

</BODY>
</HTML>


Thank you for your time :)
Johnny010
New php-forum User
New php-forum User
 
Posts: 3
Joined: Mon Sep 17, 2012 6:28 am

Re: mySQL PHP dropdown loop

Postby TomCornar » Tue Sep 18, 2012 5:50 am

hi,

Try this,

Code: Select all
<select name="charname" value="Select" size="1">
<?php

    $sql = "SELECT * FROM characters"; 
    $result = mysql_query($sql) or die (mysql_error()); 
    while ($row = mysql_fetch_array($result))
    {
            $value=$row["your column name"];
            $options.="<OPTION VALUE=\"$value\">".$value;
    }
    ?>
            <option>
            <?php echo $options ?>
            </option>
        </select>

Hope this helps

Regards,
Tom Cornar
Trinay Technology Solutions
http://www.trinay.com
User avatar
TomCornar
New php-forum User
New php-forum User
 
Posts: 25
Joined: Fri Sep 14, 2012 12:06 am

Re: mySQL PHP dropdown loop

Postby Johnny010 » Tue Sep 18, 2012 9:05 am

Thats great thanks. Using \ was something I hadnt thought of :D !
Johnny010
New php-forum User
New php-forum User
 
Posts: 3
Joined: Mon Sep 17, 2012 6:28 am


Return to mySQL & php coding

Who is online

Users browsing this forum: Google [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.