我试图打印这个数组但是它不起作用......组合框是空的。如何在组合框中仅打印名称的属性,同时在选项的值中获取NumClient?
在我的数据库中是客户端的表有30条记录然后我想获取id(NumClient)和名称(Name)并将它们放在组合框中但我只想打印Name和id(NumClient) )我想在选项的标签中添加属性值,例如
<option value="$array["NumClient"]">$array["Name"]</option>
<option value="1">Layana</option>
有什么想法吗?
<?php
$conn = mysql_connect("localhost", "root","") or die(mysql_error());
$db = mysql_select_db("bd_musique") or die(mysql_error());
$query="SELECT * from client";
$result = mysql_query($query) or die(mysql_error());
echo $query;
$array=array();
$i=0;
while ($rows = mysql_fetch_array($result)){
$array=array($i,"NumClient"=>$rows["NumClient"], "Name"=>$rows["Name"]);
$i++;
}
mysql_close($conn);
?>
<select class="input-small" name="Client">
<?php
foreach($array as $b){
print("<option value='$array[$b]['NumClient']'>".$array[$b]["Name"]."</option>"." <br>");
}
?>
/*
foreach($array as $cle=> $b){
echo("<option value='$b'>".$cle.$b."</option>"."<br>");
}
*/
</select>
干杯
答案 0 :(得分:1)
为什么不是这样的,而不是有3个不同的数组,你可以使用循环来检索结果集
while ($rows = mysql_fetch_array($result)){
$array[] = array(
'NumClient' => $rows["NumClient"],
'Name' => $rows["Name"]
);
}
//Print output
print '<select'>;
foreach($array as $value) {
print("<option value={$value['NumClient']}>{$value['Name']}</option>");
}
print '</select>';
这将解决问题,在字符串中使用{}&#34;&#34;允许您打印对象/数组
<强>更新强>
我做了本地测试,这是代码
$array[] = array(
'NumClient' => 1,
'Name' => 'Juan'
);
$array[] = array(
'NumClient' => 2,
'Name' => 'Luis'
);
$array[] = array(
'NumClient' => 3,
'Name' => 'Maria'
);
print '<select>';
foreach($array as $value) {
print("<option value='{$value['NumClient']}'>{$value['Name']}</option>");
}
print '</select>';
这是实际输出 ![渲染组合框] [1]
请务必查看您的数据来源,也许您在
之前做了其他事情这里的结果是i.stack.imgur.com/ilL6y.png
答案 1 :(得分:1)
这是解决方案,最后我做到了:
<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("dbmusique", $conn);
$query = "SELECT * from client";
$result = mysql_query($query, $conn) or die(mysql_error());
$num= mysql_num_rows($result);
if ($num> 0) {
while ($row = mysql_fetch_assoc($result)) {
$array[] = array(
'NumClient' => $row["NumClient"],
'Name' => $row["Name"]
);
}
}
?>
<form name="form" action="next.php" method="get">
<select name="numClient">
<?php
foreach($array as $value) {
print("<option value=\"{$value['NumClient']}\">{$value['Name']}</option>");
}
?>
</select>
<input type="submit" value="Send" />
</form>
next.php:
<?php
$NumClient=$_GET["numClient"];
echo $NumClient;
?>