Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating Select Boxes w/ functions 2

Status
Not open for further replies.

zpetersen

MIS
Dec 29, 2003
58
US
Im creating a page that utilzes forms. In the forms there are a number of combo/drop down boxes. I will be using the same lists in many forms so I wanted to create a function... well.. this is my first time trying this and I'm having a little trouble. Here is my code:

Code:
function listbldg ()
{
   $dbconnect = mysql_connect($hostname,$username,$password) or die("Connetion to database failed!");
   $dbname = "mis";
		mysql_select_db($dbname,$dbconnect);
		$bldgdata = mysql_query("select * from list_bldg"); 
		while ($data1 = mysql_fetch_object($bldgdata)) 
			{ 
			echo"<option value=\"".$data1->bldg."\">".$data1->bldg."</option>\n"; 
			} 
 
}

And the portion of the web page:

Code:
<tr>
<td>Building</td>
<td>
<select name="bldg">

<?php listbldg() ?>

</select>
</td>
</tr>

All I end up with is a blank select box w/no options. That says to me that Im not getting any data from the db..but I can't spot the problem. Any help would be great, thanks! :)
 
General recommendation:
All MySQL command statements that could fail should have the or die() clause. The mysql_select_db() could fail (e.g.typo), so could the query mysql_query() because of faulty SQL.
However, your SQL statement is very basic and should not be the cause.

Questions:
Is listbldg() ever executed? Put a "footprint" statement [echo("exec listcode");] in the code to see if it ever gets called.
Is the function defined in the same file?
Is the function within the PHP context?
 
Where are the values for $hostname,$username and $password coming from? They probably should be declared as global in your function.

Code:
function listbldg ()
{
   global $hostname,$username,$password;
   $dbconnect = mysql_connect($hostname,$username,$password) or die('Connection to database failed!');
   $dbname = 'mis';
   mysql_select_db($dbname,$dbconnect);
   $bldgdata = mysql_query('select * from list_bldg');
   $tmp = array();
   while ($data1 = mysql_fetch_object($bldgdata))
     $tmp[] = '<option value="'.$data1->bldg.'">'.$data1->bldg.'</option>';
   echo implode("\n",$tmp)."\n";
}

A few notes:

I use single quotes where ever I can when I want to avoid escaping double quotes. I think it makes the code cleaner and easier to understand. Also, I put all of the output into a temporary array and use one echo to output it all. Don't know if it is any better, but it's the way I've been programming lately. :)

When you have one statement in any looping or test statement, you don't need the curly braces "{ }" around the one statement.

Ken
 
OK, Ken, I used your code and it works great. Now I guess the question is.. What did I do wrong?

What are the rules for calling variables in a function? I looked over the PHP manual and I couldnt extract any "useful" info on that topic.

Nevertheless, thanks for the help guys!
 
In PHP variables inside a function are local variables. To make them default to the global variables by the same name a "global" statement is necessary. That means inside a function $host is its own variable unless the function calls global $host;

For MySQL connection parameters i usually use the define() syntax to define them as constants. That makes them available anywhere without needing to think about scope.
Variables are for things that are variable - hostname, username, password are usually constant and do not change in the script or are not manipulated.

 
Thanks for the info DRJ478! I guess I have some code to clean up after reading that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top