2

I have a select php script and the portion of it is:

  while($row = mysqli_fetch_array($result))
  {         
  echo "<option>" . $row['firstname'] . $row['lastname'] . "</option>
  }                 
echo "</select>";                       
echo "</div>";

the problem are the rows, I am wanting them to appear in the option as "John Doe" the way it is now is "JohnDoe"

I have tried changing it to: . $row['firstname'] . "&nbsp;" . $row['lastname'] . but that would cause problems later on in the program where it wouldn't read the name as John Doe it would read it as John Doe for some reason. (extra space)

4

8 回答 8

1

您可以输出文字空间:

echo "<option>" . $row['firstname'] . " " . $row['lastname'] . "</option>"

另请记住,您可以设置允许显示任何输出的value=""属性:<option>

echo "<option value=\" . $row['firstname'] . " " . $row['lastname'] . \">Something Different</option>"
于 2013-07-29T18:32:04.900 回答
1

有什么问题$row['firstname'] . " " . $row['lastname']...?(这是一个反问。你应该这样做)。

于 2013-07-29T18:32:39.777 回答
1

如果名字字段可能为空,您可能需要进行一些修剪以确保您不会在姓氏上插入前导空格。

   echo "<option>" . trim(trim($row['firstname']) .' '. $row['lastname']) . "</option>";
于 2013-07-29T18:34:59.607 回答
0
echo "<option>" . $row['firstname']." ".$row['lastname'] . "</option>
于 2013-07-29T18:32:06.960 回答
0
echo "<option>$row[firstname] $row[lastname]</option>";

应该对你很好。(注意引号的位置。)

另外,也许,为了以后的识别,你应该在你的选项中添加一个“价值”标签?

于 2013-07-29T18:34:05.197 回答
0

您的代码的问题$row['lastname']是直接附加到$row['firstname']. 要解决这个问题,只需在两者之间放置一个空格:

echo "<option>".$row['firstname']." ".$row['lastname']."</option>";

于 2013-07-29T18:35:12.457 回答
0

您需要稍后在程序中修复它并首先回显您想要的空间。

在字符串中搜索双空格并将其替换为一个空格:

$str ='John  Doe'
$str = str_replace("  ", " ", $str);
于 2013-07-29T18:35:42.937 回答
0

或者,在您的 MySql 查询中,您可以执行类似的操作。

$sql = "SELECT CONCAT(`firstname`, ' ', `lastname`) AS `fullname` FROM `TABLENAME`";

你的 PHP 循环看起来像这样

 while($row = mysqli_fetch_array($result))
 {         
     echo "<option>" . trim($row['fullname']) . "</option>"; //just incase, we trim it
 } 
于 2013-07-29T18:44:14.950 回答