User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 427,322 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,928 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 4326 | Replies: 2
Reply
Join Date: Jul 2006
Posts: 6
Reputation: tzan2833 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
tzan2833 tzan2833 is offline Offline
Newbie Poster

populate a select box using PHP

  #1  
Aug 28th, 2006
Dear all

I want to populate a few drop down boxes using PHP. However i have tried many examples i found from the web but the select box does not get any values. Below i have one example i have tried.

**********************************************
<?php
$link = mysql_connect("localhost", "root", "password") or die("Could not connect: " . mysql_error());
mysql_select_db('db', $link) or die (mysql_error());

$query = "SELECT last_name FROM main_author order by last_name";

$result = mysql_query($query) or die("Couldnt' execute query.");
$num = mysql_numrows($result);
?>

<html>
<body>
<form name="test" id="test" method="post">
<select value="" size="1" name="test2">
<option>all</option>
<? $i=0; while ($i<$num)
{$authors=mysql_result($result,$i,"last_name");
echo "<option>$authors</option>";
$++; }
mysql_close(); ?>
</select>
</body>
</html>
********************************************

any suggestions would be more than appreciated

Many thnx

Cheers
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2006
Posts: 32
Reputation: StatiX is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 2
StatiX's Avatar
StatiX StatiX is offline Offline
Light Poster

Re: populate a select box using PHP

  #2  
Aug 28th, 2006
Originally Posted by tzan2833 View Post
Dear all

<?php
$link = mysql_connect("localhost", "root", "password") or die("Could not connect: " . mysql_error());
mysql_select_db('db', $link) or die (mysql_error());

$query = "SELECT last_name FROM main_author order by last_name";

$result = mysql_query($query) or die("Couldnt' execute query.");
$num = mysql_numrows($result);
?>

<html>
<body>
<form name="test" id="test" method="post">
<select value="" size="1" name="test2">
<option>all</option>
<? 

$i=0; while ($i<$num) {

$row = mysql_fetch_assoc($result);

echo "<option>$row['last_name']</option>";

$i++; }
mysql_close(); ?>
</select>
</body>
</html>
********************************************

Try that and see if it works
Just loop thru the result using the array and print them

StatiX
Reply With Quote  
Join Date: Jul 2006
Location: Remunj
Posts: 207
Reputation: pritaeas is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 27
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Posting Whiz in Training

Re: populate a select box using PHP

  #3  
Sep 1st, 2006
you also need

<option value="something">Something</option>
"Premature optimization is the root of all evil."
Donald Knuth / Tony Hoare
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 2:45 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC