vaibhavint Posted April 28, 2007 Share Posted April 28, 2007 hi all, I am working over a project dealing with personal savings. There are two listboxes in the form ... and the user has to select two distinct name from them. The data source (mysql table name) is same for both of them. The problem is that only one listbox gets populated ... and the other one remains empty. I have used while loop instead of for ...but it doesn't work. Here is the code : <?php $conn=mysql_connect("localhost","root") or die ("Unable to connect to MySQL server."); $db = mysql_select_db("savings") or die ("Unable to select requested database."); $query = "SELECT * FROM names "; $result = mysql_query($query); echo "<html>"; echo "<head>"; echo "</head>"; echo "<body>"; echo "<form>"; echo "<center>"; echo "<br>"; echo "<br>"; echo " KVP Number "; echo "<input name='kvpnum' type='text'>"; echo "<br>"; echo "<br>"; echo "Select First Name"; echo "<select name='name1'>"; for($i = 0; $i < mysql_num_rows($result); $i++) { $row = mysql_fetch_array( $result ); echo "<option> $row[name] </option> <br>"; } echo "</select>"; echo "Select Second Name"; echo "<select name='name2'>"; for($i = 0; $i < mysql_num_rows($result); $i++) { $row = mysql_fetch_array( $result ); echo "<option> $row[name] </option> <br>"; } echo "</select>"; echo "</form>"; echo "</body>"; echo "</html>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/49040-solved-populating-two-listboxes-with-same-data-source/ Share on other sites More sharing options...
MadTechie Posted April 28, 2007 Share Posted April 28, 2007 the first box uses fetch until the last record.. so does the second.. (by then its on the last record) try this <?php $conn=mysql_connect("localhost","root") or die ("Unable to connect to MySQL server."); $db = mysql_select_db("savings") or die ("Unable to select requested database."); $query = "SELECT * FROM names "; $result = mysql_query($query); $TheList = ""; for($i = 0; $i < mysql_num_rows($result); $i++) { $row = mysql_fetch_array( $result ); $TheList .= "<option> $row[name] </option> <br>"; } echo "<html>"; echo "<head>"; echo "</head>"; echo "<body>"; echo "<form>"; echo "<center>"; echo "<br>"; echo "<br>"; echo " KVP Number "; echo "<input name='kvpnum' type='text'>"; echo "<br>"; echo "<br>"; echo "Select First Name"; echo "<select name='name1'>"; echo $TheList; echo "</select>"; echo "Select Second Name"; echo "<select name='name2'>"; echo $TheList; echo "</select>"; echo "</form>"; echo "</body>"; echo "</html>"; ?> **UNTESTED Quote Link to comment https://forums.phpfreaks.com/topic/49040-solved-populating-two-listboxes-with-same-data-source/#findComment-240258 Share on other sites More sharing options...
vaibhavint Posted April 28, 2007 Author Share Posted April 28, 2007 Thanks MadTechie...... Quote Link to comment https://forums.phpfreaks.com/topic/49040-solved-populating-two-listboxes-with-same-data-source/#findComment-240310 Share on other sites More sharing options...
MadTechie Posted April 28, 2007 Share Posted April 28, 2007 can you please click solved Quote Link to comment https://forums.phpfreaks.com/topic/49040-solved-populating-two-listboxes-with-same-data-source/#findComment-240358 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.