在php表中下拉菜单

时间:2015-12-03 21:08:14

标签: php mysql

我目前正在尝试显示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>';
    }
}

代码工作正常唯一的问题是,在每个下拉菜单中只有一个元素,我完全明白为什么它这样做但我想不出任何会返回下拉菜单中的所有元素每次该函数被调用,有没有人知道我该如何实现它?

4 个答案:

答案 0 :(得分:4)

你用return语句停止你的select循环!创建一个String变量(如$ optionsHtml)并将每个选项html标记为字符串并返回该字符串。

$optionsHtml = '';
while($record = mysql_fetch_array($mydata2)) {
    $optionsHtml .= '<option value = "' . $record['RowNumber'] . '">' . $record['RowNumber'] . '</option>';
}
return $optionsHtml;

答案 1 :(得分:0)

您只能从函数返回一次,所以最好的方法是返回数组,或者在您的情况下返回一个字符串。

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将从一个函数返回这个值,并且不再通过循环。这就是你需要像这样生成字符串然后返回整个字符串的原因。在某些情况下,您可以返回数组,然后在“主代码”中循环数组。

答案 2 :(得分:0)

问题是你在函数的早期返回,导致循环停止,只剩下第一条记录。

已经提出一种解决方案将结果附加到变量,但是这样你就不能重用该函数,因为变量名被硬编码到函数中,更好的方法是填充数组,返回它,并从中读取内容,这里是代码:

// 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,但它可以作为预期

答案 3 :(得分:0)

在我看来,所选择的方法在数据库上看起来不必要 - 在你通过主记录集的每次迭代中,你可以再次查询数据库,正如@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>";


?>