Jump to content

[SOLVED] populating two listboxes with same data source


vaibhavint

Recommended Posts

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>";


?>

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

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.