Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.