我有两个数组存储每个人的名字和姓氏。我需要将它们组合在一起并将它们全部添加到选项标签中。我特别遇到这一行:echo "<option value = '". $name ."'>". $name . "</option>";
<?php
$servername = "localhost";
$username = "u";
$password = "p";
$database = "d";
$con = mysqli_connect($servername,$username,$password,$database);
if ($con->connect_error) {
die("Connection failed " . $con->connect_error);
}
$sql = "select first from employee";
$sql1 = "select last from employee";
$run = mysqli($con,$sql);
$run1 = mysqli($con,$sql1);
$arr = mysqli_fetch_array($run);
$arr1 = mysqli_fetch_array($run1);
$it = getIterator($arr1);
foreach ($arr as $value) {
$name = $value . ' ' . $it;
echo "<option value = '". $name ."'>". $name . "</option>";
}
答案 0 :(得分:-1)
双引号内的PHP变量已被评估&#39;,因此您可以使用以下内容简化您的行:
echo "<option value ='$name'>$name</option>";
如果您保证两个数组都匹配记录,那么您可以使用$ key从每个数组中选择值。
foreach($arr as $key => $value){
$name = $arr[$key]. ' ' . $arr2[$key];
echo "<option value = '". $name ."'>". $name . "</option>";
}
我的建议是只进行1次查询并将两个字段合并为1列
$sql = "SELECT CONCAT(first,' ',last) AS name FROM employee";
$query = mysqli($con,$sql);
while($user = mysqli_fetch_assoc($query)) {
$name = $user['name'];
echo "<option value='$name'>$name</option>";
}
答案 1 :(得分:-1)
这是通常的做法。
$sql = "select first, last from employee"; //select both first and last with one query
$result = mysqli_query($con,$sql); //execute the query
while ($row = mysqli_fetch_array($result)) { //loop over the result set
// there are various ways to combine the names, this is one example
$name = $row['first'] . ' ' . $row['last'];
echo "<option value=\"$name\">$name</option>";
}
以这种方式在while循环定义中完成提取,因为mysqli_fetch_array()
仅从结果中提取一行。这样做将继续获取,直到它遍历每一行。
答案 2 :(得分:-1)
为什么不创建可重复使用的功能,所以每次你想要这样做,你都可以调用它。
这是我用来创建动态下拉列表的函数。您需要根据自己的需要进行修改,但这比每次需要时都要好。
/**
* Selects a set of records and returns a string of HTML <option></option> tags of the selected values. This is to be used inside of <select></select> tags.
*
* @param string $_IncomingSql The sql query that you want to execute.
* @param string $_SelectedValue OPTIONAL PARAMETER - The selected item on load. Based on Value.
* @param string $_FirstValueName OPTIONAL PARAMETER - Add custom item. Usually "Select". This will set the name="" attribute that will be apended to the custom <option> element.
* @param string $_FirstValue OPTIONAL PARAMETER - Add custom item. Usually "0". This will set the value="" attribute that will be appended to the custom <option> element.
*/
function gfOption($con, $_IncomingSql, $_SelectedValue=0, $_FirstValueName ='', $_FirstValue=0)
{
$Result = mysqli_query($con, $_IncomingSql);
if ($Result)
{
$Str = '';
$I=0;
if ($_FirstValue!='' or $_FirstValueName !='')
{
$Str.= '<option value="'.$_FirstValue.'" name="'.$_FirstValueName.'">' . $_FirstValueName . '</option>';
}
$C=0;;
while ($row = mysqli_fetch_array($Result, MYSQL_BOTH))
{
$I++;
$C= count($row);
if ($C===1)
{
$Str.= '<option value="'.$row[0].'" name="'.$row[0].'" ';
}
elseif($C===2)
{
$Str.= '<option value="'.$row[0].'" name="'.$row[1].'" ';
}
if ($_SelectedValue ==$row[0])
{
$Str.= 'Selected="selected"';
}
if ($C===1)
{
$Str.= '>'.$row[0].'</option>';
}
elseif($C===2)
{
$Str.= '>'.$row[1].'</option>';
}
}
return $Str;
}
return '0';
}
然后你会把它称为:
<?PHP $Sql = "Select ID, Name from Table order by id;"; ?>
<select id="Select1">
<?php echo gfOption($Sql, 0, "Select", "0") ?>
</select>