[英]drop down menu in php table
我目前正在嘗試顯示存在於php表中的下拉菜單的所有屬性,我創建的表的代碼是下面的一個
while($record = mysql_fetch_array($availablesitsdata)) {
echo "<form action=selectsits.php enctype=multipart/form-data method=post>";
echo "<tr>";
echo "<td>" . $record['Zone'] . "</td>";
$nameofzone = $record['Zone'];
echo "<td>" . "<select type=text name=sit>" . tickets_Num($nameofzone) . " </select></td>";
echo "<td>" . "<input type=submit name=update value=update > </td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
每次創建表的新行時,還會在它之間創建一個新的下拉菜單,每次調用一個函數ticket_Num,函數的代碼如下所示。
Function tickets_Num($nz){
include 'sql_querys.php';
mysql_select_db("theater",$con);
$sql2 = "SELECT `RowNumber` FROM `seat` WHERE `Zone` = '".$nz."' ";
$mydata2 = mysql_query($sql2,$con);
while($record = mysql_fetch_array($mydata2)) {
return '<option value = "' . $record['RowNumber'] . '">' . $record['RowNumber'] . '</option>';
}
}
代碼工作正常唯一的問題是,在每個下拉菜單中只有一個元素,我完全明白為什么它這樣做但我想不出任何會返回下拉菜單中的所有元素每次功能是有人知道我怎么能實現這個?
你用return語句停止你的select循環! 創建一個String變量(如$ optionsHtml)並附加每個選項html標記字符串並返回字符串。
$optionsHtml = '';
while($record = mysql_fetch_array($mydata2)) {
$optionsHtml .= '<option value = "' . $record['RowNumber'] . '">' . $record['RowNumber'] . '</option>';
}
return $optionsHtml;
您只能從函數返回一次,因此最好的方法是返回數組或在您的情況下返回一個字符串。
Function tickets_Num($nz){
$result = '';
include 'sql_querys.php';
mysql_select_db("theater",$con);
$sql2 = "SELECT `RowNumber` FROM `seat` WHERE `Zone` = '".$nz."' ";
$mydata2 = mysql_query($sql2,$con);
while($record = mysql_fetch_array($mydata2)) {
$result .= '<option value = "' . $record['RowNumber'] . '">' . $record['RowNumber'] . '</option>';
}
return $result;
}
編輯:代碼的解釋基本上是當你輸入while循環時你有一個命令返回wich將從一個函數返回這個值而不再通過循環。 這就是你需要像這樣生成字符串然后返回整個字符串的原因。 在某些情況下,您可以返回數組,然后在“主代碼”中循環數組。
問題是你在函數的早期返回,導致循環停止,這使你只留下第一個記錄。
已經提出了一種解決方案將結果附加到變量的解決方案,但是這樣就無法重用該函數,因為變量名稱被硬編碼到函數中,更好的方法是填充數組,返回它,並從中讀取它的內容,這是代碼:
// Filling the array
Function tickets_Num($nz)
{
$options = array();
include 'sql_querys.php';
mysql_select_db("theater",$con);
$sql2 = "SELECT `RowNumber` FROM `seat` WHERE `Zone` = '".$nz."' ";
$mydata2 = mysql_query($sql2,$con);
while($record = mysql_fetch_array($mydata2)) {
$options[] = '<option value = "' . $record['RowNumber'] . '">' . $record['RowNumber'] . '</option>';
}
return $options;
}
// Reading the array
$tickets = tickets_Num($nz);
foreach ($tickets as $value)
{
echo $value.'<br>';
}
這樣,函數不依賴於數組的名稱,因為您可以看到讀取變量名為$tickets
而函數使用的數組是$options
但它按預期工作。
在我看來,選擇的方法似乎在數據庫上不必要地沉重 - 在每次迭代通過主記錄集時,你可以再次查詢數據庫,正如@Alfwed指出的那樣,使用數組(我不知道我做了什么這是他的意思。)
此外,你的HTML是無效的-有一個表單封裝表行〜它必須是被全表單元格內的封裝或必須封裝整個表。
<?php
/* Include your database reference & choose the db - only needs to be done once */
include 'sql_querys.php';
mysql_select_db("theater",$con);
/* Supporting functions */
function tickets_Num(){
global $con;/* use a global reference to the $con object */
$sql = "select `rownumber` from `seat`;";
$res = mysql_query( $sql, $con );
$tmp=array();
while( $rs = mysql_fetch_object( $res ) ) {/* add records to array with pre-formatted `option` */
$tmp[ $rs->rownumber ][]='<option value = "' . $rs->rownumber . '">' . $rs->rownumber;
}
return $tmp;
}
/* Find the particular fields in results array */
function get_menu_items( $id=false, $arr=array() ){
return ( $id && is_array( $arr ) && !empty( $arr ) && array_key_exists( $id, $arr ) ) ? $arr[ $id ] : false;
}
/* Create a dropdown menu */
function dropdown( $name=false, $options=array() ){
$tmp=array();
$tmp[]="<select name='{$name}'>";
$tmp[]=implode( PHP_EOL, $options );
$tmp[]="</select>";
return implode( PHP_EOL, $tmp );
}
/* Query the db to get all rownumbers. Store in an array to be reused frequently */
$atmp=call_user_func('tickets_Num');
/*
By adding the rownumbers to the array you drastically reduce the number of queries
you make to the database - so if your main query has 100 rows that would be 101
queries you would have made using the original approach.
*/
/* Begine your html output */
echo "<table>";
/* Loop through your main recordset */
while( $record = mysql_fetch_array( $availablesitsdata ) ) {
$nameofzone = $record['Zone'];
/*
To be valid html a form cannot straddle parts of a table ( or other disjointed html elements )
so it must either encompass the entire table or be contained within a tablecell entirely.
*/
echo "<tr>
<td>{$record['Zone']}</td>
<td>
<form action='selectsits.php' enctype='multipart/form-data' method='post'>
" . dropdown( 'sit', get_menu_items( $nameofzone, $atmp ) ) . "
<input type='submit' value='update' />
</form>
</td>
</tr>";
}
echo "</table>";
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.