php while loop output producing select for each item
I know I have coded this incorrectly, but cannot seem to find a way to correct it. The aim, is to have a dropdown displayed populated with results from mysql database. It is currently displaying a dropdown for every address. I know why this is happening but cannot seem to correct it. Should the echo be out of the while loop? or is the placement correct but it is wrong somewhere else? would be gratful if someone could check it and inform me where i AM GOING WRONG WITH IT. Many thanks.
<?php开发者_C百科
$customer = mysql_real_escape_string( $_GET["customer"] );
$con = mysql_connect("localhost","root","");
$db = "sample";
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($db, $con);
$query_rs_select_address2 = sprintf("SELECT * FROM company_com where idcode_com = '$customer'");
$rs_select_address2 = mysql_query($query_rs_select_address2, $con) or die(mysql_error());
$row_rs_select_address2 = mysql_fetch_assoc($rs_select_address2);
$totalRows_rs_select_address2 = mysql_num_rows($rs_select_address2);
while ($row_rs_select_address2 = mysql_fetch_assoc($rs_select_address2))
{
$address=$row_rs_select_address2['address1_com']. " ". $row_rs_select_address2['address2_com']. " ". $row_rs_select_address2['address3_com']. " ". $row_rs_select_address2['town_com']. " ". $row_rs_select_address2['postcode_com'];
echo '<select name="customer">'.'<option value="">Select delivery address</option>'.'<option value="address">'.$address.'</option>'.'</select>';
}
?>
If your goal is to have a single drop-down will all of the options, then you need to place the echo for the open and close tags of the select outside of the while and leave the option tags in the while:
// also put first option on the outside as you *don't* want to repeat that.
echo '<select name="customer"><option value="">Select delivery address</option>';
while ($row_rs_select_address2 = mysql_fetch_assoc($rs_select_address2))
{
// only place the code in here you want repeated for every value in the query
/*
have you considered concatenating in the query?
Basically:
select concat( address1_com, ' ', address2_com, ' ', address3_com, ' '
town_com, ' ', postcode_com ) as full_address From...
Sometimes the greater number of records means slower execution.
Of course, you should benchmark to be sure.
*/
$address= $row_rs_select_address2['address1_com']. " ".
$row_rs_select_address2['address2_com']. " ".
$row_rs_select_address2['address3_com']. " ".
$row_rs_select_address2['town_com']. " ".
$row_rs_select_address2['postcode_com'];
// notice I swapped out $uid for address. You want to have each option
// reflect a different value so that the POST gives a uid to the server
// You can probably get a uid from the primary key of the company_com
// table.
echo '<option value="$uid">'.$address.'</option>';
}
echo '</select>';
Try this:
echo '<select name="customer">';
echo '<option value="">Select delivery address</option>';
while ($row_rs_select_address2 = mysql_fetch_assoc($rs_select_address2))
{
$address=$row_rs_select_address2['address1_com']. " ". $row_rs_select_address2['address2_com']. " ". $row_rs_select_address2['address3_com']. " ". $row_rs_select_address2['town_com']. " ". $row_rs_select_address2['postcode_com'];
echo '<option value="address">'.$address.'</option>';
}
echo '</select>';
the while
loop produces an select
element every time.
you should put select
outside the while
echo "<select>";
while(/* while statement */){
echo "<option></option>";
}
echo "</select>";
Change your while to this
echo '<select name="customer">';
echo '<option value="">Select delivery address</option>';
while ($row_rs_select_address2 = mysql_fetch_assoc($rs_select_address2))
{
$address=$row_rs_select_address2['address1_com']. " ". $row_rs_select_address2['address2_com']. " ". $row_rs_select_address2['address3_com']. " ". $row_rs_select_address2['town_com']. " ". $row_rs_select_address2['postcode_com'];
echo '<option value="address">'.$address.'</option>'
}
echo '</select>';
Try something like this:
echo '<select name="customer">';
while ($row_rs_select_address2 = mysql_fetch_assoc($rs_select_address2))
{
$address=$row_rs_select_address2['address1_com']. " ". $row_rs_select_address2['address2_com']. " ". $row_rs_select_address2['address3_com']. " ". $row_rs_select_address2['town_com']. " ". $row_rs_select_address2['postcode_com'];
echo '<option value="">Select delivery address</option>'.'<option value="address">'.$address.'</option>';
}
echo '</select>';
You were making different for each (row in your table), hope this helps :).
精彩评论