Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Display Results from SQL with a foreach loop

Posted on 2009-06-28
3
Medium Priority
?
753 Views
Last Modified: 2013-12-13
Hey Guys,

I am having an issue with the following SQL Statement. I want to populate my drop down form box with information from my database.

Pretty much I want to have as many options as there are rows in my <select> drop down </select>

What I need pulled from the database is the following <option value="$rows['page_id']>' . $rows['page_title'] . '</option>";

Here is my code:
$sql = "Select * from content";
	//echo $sql . "<br>";
	
	$result= mysql_query($sql);
	
	$num_rows = mysql_num_rows($result);
	$rows = mysql_fetch_array($result);
	//echo "Total rows found is : " . $num_rows;
	
	for($i = 1; $i <= $num_rows; $i++) {
		echo $i;
		foreach ($rows as $page) {
			//echo $page;
			$cmspages = '<option value="' . $rows['page_id'] . '">' . $rows['page_title'] . '</option>';
		}
	}

Open in new window

0
Comment
Question by:George-TCC
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 24734056
this should do better:
$sql = "Select * from content";
      //echo $sql . "<br>";
      
      $result= mysql_query($sql); 
      $i = 0;
      
      $num_rows = mysql_num_rows($result);
      //echo "Total rows found is : " . $num_rows;
      
      while ($row = mysql_fetch_assoc($result))
      {
         $i++;
         $cmspages = '<option value="' . $row['page_id'] . '">' . $rows['page_title'] . '</option>';
         echo $cmspages;
      }

Open in new window

0
 
LVL 7

Expert Comment

by:szewkam
ID: 24734072
well, the easiest way to loop through data set from db is while.
I'm not pretty sure this is what you want, but try :)
<?
$sql = "Select * from content";
        //echo $sql . "<br>";
        
        $result= mysql_query($sql);
        
        $num_rows = mysql_num_rows($result);
        //echo "Total rows found is : " . $num_rows;
        
        while ($rows = mysql_fetch_array($result)
                        //echo $page;
                        $cmspages = '<option value="' . $rows['page_id'] . '">' . $rows['page_title'] . '</option>';
        }

Open in new window

0
 

Author Closing Comment

by:George-TCC
ID: 31597803
Great it works perfectly!

Thanks for that! Enjoy the points - Well earned! :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question