gpt4 book ai didi

php while循环输出为每个项目产生选择

转载 作者:行者123 更新时间:2023-12-03 23:06:56 24 4
gpt4 key购买 nike

我知道我的编码有误,但似乎找不到更正它的方法。目的是显示一个下拉列表,其中填充了来自 mysql 数据库的结果。它当前显示每个地址的下拉列表。我知道为什么会这样,但似乎无法纠正它。 echo 应该在 while 循环之外吗?还是位置正确但其他地方错误?如果有人可以检查它并告诉我哪里出了问题,我将不胜感激。非常感谢。

<?php
$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>';

}

?>

最佳答案

如果您的目标是让一个下拉菜单包含所有选项,那么您需要将 echo 的 select 的打开和关闭标记放置在 outside 的 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>';

关于php while循环输出为每个项目产生选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6734169/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com