Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-22-2012, 04:13 AM   PM User | #1
stevenmw
Regular Coder

 
stevenmw's Avatar
 
Join Date: Jun 2007
Location: OK
Posts: 449
Thanks: 26
Thanked 30 Times in 30 Posts
stevenmw is an unknown quantity at this point
Select Field Populated By MYSQL DB

I'm writing a script, and part of that script has to pull data out of a database and display it in an html select field.

Below is my code with. It all works fine. My issue is that instead of displaying all of the database table rows inside of ONE select field, a new select field is generated for every table row.

How can I alter the code to display all of the rows in ONE SINGLE select field? Any help is much appreciated.

PHP Code:
<?php
$username
="...";
$password="...";
$database="...";
$host=".....";
mysql_connect($host,$username,$password);
@
mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM news_cats";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while (
$i $num) {
$f1=mysql_result($result$i"cat");
echo 
"<select>";
echo 
"<option name=\"cat\">$f1</option>";
echo 
"</select>";
$i++;
}
?>

Last edited by stevenmw; 01-22-2012 at 04:16 AM..
stevenmw is offline   Reply With Quote
Old 01-22-2012, 04:20 AM   PM User | #2
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
Quote:
Originally Posted by stevenmw View Post
I'm writing a script, and part of that script has to pull data out of a database and display it in an html select field.

Below is my code with. It all works fine. My issue is that instead of displaying all of the database table rows inside of ONE select field, a new select field is generated for every table row.

How can I alter the code to display all of the rows in ONE SINGLE select field? Any help is much appreciated.

PHP Code:
<?php
$username
="...";
$password="...";
$database="...";
$host=".....";
mysql_connect($host,$username,$password);
@
mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM news_cats";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while (
$i $num) {
$f1=mysql_result($result$i"cat");
echo 
"<select>";
echo 
"<option name=\"cat\">$f1</option>";
echo 
"</select>";
$i++;
}
?>
You've approached this...strangely. Why are you using mysql_result, instead of mysql_fetch_assoc with a while loop for example? Kind of like this:
PHP Code:
while($info mysql_fetch_assoc($result)){
    
// Do something for this row

Means you don't have to keep a counter or anything. Also, it makes much more sense to just select cat in your query instead of retrieving all the fields for that table.

Regardless, your problem is your select tag echo is inside your while statement - so it gets echo'd every time you loop.
__________________
Useful function to retrieve difference in times
The best PHP resource
A good PHP FAQ
PLEASE remember to wrap your code in [PHP] tags.
PHP Code:
// Replace this
if(isset($_POST['submitButton']))
// With this
if(!empty($_POST))
// Then check for values/forms. Some IE versions don't send the submit button 
Quote:
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
BluePanther is offline   Reply With Quote
Users who have thanked BluePanther for this post:
stevenmw (01-22-2012)
Old 01-22-2012, 04:32 AM   PM User | #3
stevenmw
Regular Coder

 
stevenmw's Avatar
 
Join Date: Jun 2007
Location: OK
Posts: 449
Thanks: 26
Thanked 30 Times in 30 Posts
stevenmw is an unknown quantity at this point
Thanks. I'll work on some revisions.
stevenmw is offline   Reply With Quote
Old 01-22-2012, 05:38 AM   PM User | #4
stevenmw
Regular Coder

 
stevenmw's Avatar
 
Join Date: Jun 2007
Location: OK
Posts: 449
Thanks: 26
Thanked 30 Times in 30 Posts
stevenmw is an unknown quantity at this point
I came up with a very basic revision for test purposes.

PHP Code:
<?php
$username
="...";
$password="...";
$database="...";
$host="....";
mysql_connect($host,$username,$password);
@
mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM news_cats";
$result=mysql_query($query);
echo 
"<select>";
while (
$row mysql_fetch_assoc($result)) {
echo 
"<option>";
echo 
$row["cat"];
echo 
"</option>";
}
echo 
"</select>";
?>
Works great, thanks.
stevenmw is offline   Reply With Quote
Old 01-22-2012, 06:22 AM   PM User | #5
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
Quote:
Originally Posted by stevenmw View Post
I came up with a very basic revision for test purposes.

PHP Code:
<?php
$username
="...";
$password="...";
$database="...";
$host="....";
mysql_connect($host,$username,$password);
@
mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM news_cats";
$result=mysql_query($query);
echo 
"<select>";
while (
$row mysql_fetch_assoc($result)) {
echo 
"<option>";
echo 
$row["cat"];
echo 
"</option>";
}
echo 
"</select>";
?>
Works great, thanks.
I would suggest changing your query to SELECT cat FROM news_cats - no need to retrieve all the extra information if all you're using is the one field. Always select the fields you're going to use, and only use the * if you're going to use every single one
__________________
Useful function to retrieve difference in times
The best PHP resource
A good PHP FAQ
PLEASE remember to wrap your code in [PHP] tags.
PHP Code:
// Replace this
if(isset($_POST['submitButton']))
// With this
if(!empty($_POST))
// Then check for values/forms. Some IE versions don't send the submit button 
Quote:
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
BluePanther is offline   Reply With Quote
Old 01-22-2012, 08:00 AM   PM User | #6
stevenmw
Regular Coder

 
stevenmw's Avatar
 
Join Date: Jun 2007
Location: OK
Posts: 449
Thanks: 26
Thanked 30 Times in 30 Posts
stevenmw is an unknown quantity at this point
Thanks for the tip. I'm going to be using the id field soon so I just called it all at once.

Thanks again.
stevenmw is offline   Reply With Quote
Old 01-22-2012, 08:58 PM   PM User | #7
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
Quote:
Originally Posted by stevenmw View Post
Thanks for the tip. I'm going to be using the id field soon so I just called it all at once.

Thanks again.
SELECT id,cat FROM news_cats

__________________
Useful function to retrieve difference in times
The best PHP resource
A good PHP FAQ
PLEASE remember to wrap your code in [PHP] tags.
PHP Code:
// Replace this
if(isset($_POST['submitButton']))
// With this
if(!empty($_POST))
// Then check for values/forms. Some IE versions don't send the submit button 
Quote:
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
BluePanther is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:17 AM.


Advertisement
Log in to turn off these ads.