TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-17-2008, 04:37 AM   #1 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
sarmenhb is on a distinguished road
Default 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
__________________
no signature set
sarmenhb is offline  
Reply With Quote
Old 02-17-2008, 05:18 AM   #2 (permalink)
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default

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.
Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew is offline  
Reply With Quote
Old 02-17-2008, 06:15 AM   #3 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
sarmenhb is on a distinguished road
Default

Quote:
Originally Posted by Andrew View Post
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>";
	}
__________________
no signature set
sarmenhb is offline  
Reply With Quote
Old 02-17-2008, 07:15 AM   #4 (permalink)
The Acquainted
 
Join Date: Nov 2007
Posts: 154
Thanks: 31
SOCK is on a distinguished road
Default

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.
__________________
I reject your reality, and substitute my own.
SOCK is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:23 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design