TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   dynamic dropdown not work (http://www.talkphp.com/absolute-beginners/2263-dynamic-dropdown-not-work.html)

sarmenhb 02-17-2008 04:37 AM

dynamic dropdown not work
 
heres the function im using

when i load the page the drop down doesnt show anything. so im wondering if there is a syntax error u can find because my ide isnt finding any.

this is how im typing the html out

<select><?php dyn_dropdown(tbl_status,status); ?></select>



Code:

function dyn_dropdown($tablename,$columnname) {


        $query = mysql_query("SELECT $columnname FROM $tablename");
       
        if(mysql_num_rows($query))
        {
        while($row = mysql_fetch_assoc($query))
        {
        $block = "<option>";
        $block .=  $row['$columnname'];
        $block .= "</option>";

        }
       
        }
        else {
        echo "<option>no data</option>";
        }

}



here is the table structure

tbl_status

Code:

CREATE TABLE `tbl_status` (
  `id`      int AUTO_INCREMENT NOT NULL,
  `status`  varchar(20) NOT NULL,
  /* Keys */
  PRIMARY KEY (`id`)
) ENGINE = InnoDB;

heres the data it contains

Code:

        id status                                                     
        20 open                                                       
        21 op


Andrew 02-17-2008 05:18 AM

After the while block, add in:
PHP Code:

echo $block

And also add in a period before the first equal sign within the while block.

sarmenhb 02-17-2008 06:15 AM

Quote:

Originally Posted by Andrew (Post 10904)
After the while block, add in:
PHP Code:

echo $block

And also add in a period before the first equal sign within the while block.

i put the echo and still doesnt work.

here is my function now

Code:

function dyn_dropdown($tablename,$columnname) {


        $query = mysql_query("SELECT $columnname FROM $tablename");
       
        if(mysql_num_rows($query))
        {
       
        while($row = mysql_fetch_assoc($query))
        {
                $options = $row['columnname'];
                echo "<option>$options</option>";

        }

        }
        else {
        echo "<option>no data</option>";
        }


SOCK 02-17-2008 07:15 AM

PHP Code:

<select><?php dyn_dropdown(tbl_status,status); ?></select>

You say this is how you're calling the function? Is that an example or is that your actual code? If so, you're passing (probably) undefined constants to the function, not values. Wrap those parameters in quotes.

This conditional probably doesn't work as you expect:
PHP Code:

if(mysql_num_rows($query)) { } 

mysql_num_rows() returns either the number of records returned in the resultset, or FALSE. It doesn't return TRUE as your conditional assumes. So if it returns anything other than FALSE, 0 or 1, that conditional statement breaks.

I'm also curious as to why you're not using the `id` column as the OPTION tag value, e.g.
PHP Code:

while ( $rowmysql_fetch_assoc($query) ) {
    echo 
"<option value={$row['id']}>{$row['columnname']}</option>";


Otherwise, how is the SELECT dropdown going to function and relate to your database? Speaking of which, how is this going to work if you use $columnname in the SELECT and $row['columnname'] in the output? You need to use $columnname in both instances, or use a hardcoded column name (no pun intended) in both.


All times are GMT. The time now is 08:27 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0