在 <select ..>(HTML、PHP、SQL 中使用变量作为大小属性的值

Using a variable for the value of size attribute in <select ..> (HTML, PHP, SQL)

我正在尝试使用 select 制作下拉菜单。内容是来自数据库的名称。我希望 select 的大小是我的 table tickets.

中条目的数量

我想要

的尺码
echo '<select name="Tickets" size=5>'; //this works fine, but I want the size to be relative

成为一个变量而不是常量值 5。到目前为止,我试过这个:

$query = mysql_query("SELECT * FROM tickets order by ID");
$numrows = mysql_num_rows($query);

//echo $numrows; Was just testing to see if it got the right number (it did).

echo '<select name="Tickets" size=$numrows>'; 

但这似乎不起作用。我如何实现这一目标?提前干杯!

您不能回显 ' 中的变量,它必须是 "

所以你可以改写成这样:

echo "<select name='Tickets' size=$numrows>"; 

或者这样:

echo '<select name="Tickets" size='.$numrows.'>';