Jump to content

Displaying Relevant Data


stu1040

Recommended Posts

Hi All.

What should I be looking for if I want to display all relevant data from a clicked link.

 

There are 5 cities. So if I click ‘birmingham’ then everything about birmingham would display, london and so forth on one page called ‘locations.php’

 

The links work fine below and display one city when clicked, when I know there are 3 of each in the database.

 

I was thinking in the lines of a ‘while loop’ or similar! Wrong direction?

 

locations landing page

$id = mysql_real_escape_string(trim($_GET['id']));  
$data = mysql_query("SELECT location FROM cities WHERE id = '$id' ")  
or die(mysql_error()); $info = mysql_fetch_array( $data ); { 
Print "".$info['location'] .""; } 

 

I have 5 x city links from this, works fine.

$data = mysql_query("SELECT id, location FROM cities GROUP BY location ORDER BY id ASC") 
OR DIE (mysql_error());  while($info = mysql_fetch_array($data)) {  
Print "<a href=locations.php?id=".$info['id']."'>".$info['location'] ."</a><br />"; }  

Link to comment
https://forums.phpfreaks.com/topic/254669-displaying-relevant-data/
Share on other sites

For the love of god space out your code.  Why are your line breaks completely random?

 

Your code, fixed:

 

$id = mysql_real_escape_string(trim($_GET['id']));  
$data = mysql_query("SELECT location FROM cities WHERE id = '$id' ") or die(mysql_error()); 
$info = mysql_fetch_array( $data ); 
{ 
  Print "".$info['location'] .""; 
} 



$data = mysql_query("SELECT id, location FROM cities GROUP BY location ORDER BY id ASC") OR DIE (mysql_error());  
while($info = mysql_fetch_array($data)) {  
  Print "<a href=locations.php?id=".$info['id']."'>".$info['location'] ."</a><br />"; 
}  

Now that it's formatted properly, you can see that your first block has a print() statement completely on its own inside an unnecessary set of curly braces.  Why?  There's no reason for it to be like that.  You need a while loop there, which you might have thought you HAD already since you had the braces. 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.