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
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. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.