user3397260
user3397260

Reputation: 281

drop down menu in php table

i am currently trying to show all the attributes of drop down menus that exist in a php table that i created the code of the table is the one below

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>";

each time a new row of the table is created a new drop down menu is also created between it rows and a function ticket_Num is called each time , the code of the function is the one below

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>';
    }
}

the code works fine the only problem is that in each drop down menu there is only one element , i quite get it why it does that but i cant think of anything that will return all the elements in the drop down menus each time the function is called , does anyone have an idea how can i implement this ?

Upvotes: 3

Views: 245

Answers (4)

Professor Abronsius
Professor Abronsius

Reputation: 33823

To my mind the approach chosen seems unnecessarily heavy on the database - on each iteration through the main recordset you query the database again when you could, as was pointed out by @Alfwed, use an array ( I'm not sure what I have done here is what he meant however. )

Also, your html is not valid - there is a form encapsulating a table row ~ it must be either be wholly encapsulated within a table cell or it must encapsulate the entire table.

<?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>";


?>

Upvotes: 0

Achraf Almouloudi
Achraf Almouloudi

Reputation: 746

The issue is that you're returning early in the function, which causes the loop to stop, which leaves you only with the first record.

A solution has been proposed to append the results to a variable, but that way you couldn't reuse the function because the variable name is hard-coded into the function, a better approach is to fill up array, return it, and read from it's contents, here's the code:

// 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>';
}

This way the function isn't dependent on the name of the array, as you can see the reading variable is named $tickets while the array the function used was $options but it works as expected.

Upvotes: 0

silly
silly

Reputation: 7887

You are stop your select loop with a return statement! create a String variable (like $optionsHtml) and append every option html tag the string and return the string.

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

Upvotes: 4

Standej
Standej

Reputation: 753

You can only return one time from a function so best way is to return array or in your case a string.

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;
}

Edit: Explanation of your code is basicly when you enter in while loop you have a command return wich will return this value from a function and will not go anymore thru loop. This is why you need to generate string like this and then return whole string. In some cases you can return array and then you loop array in your "main code".

Upvotes: 0

Related Questions