Links on MySQL output list

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dv_evan
Forum Commoner
Posts: 42
Joined: Wed Apr 09, 2008 8:23 am

Links on MySQL output list

Post by dv_evan »

Hello Everyone,

I am a newbie and I did tried searching this topic for help before asking for help. Also forgive me if I am posting in the wrong forum.
Here is my problem:

I have a Mysql database with people's details, such as name,address,contacts, etc.

I would like to run through this table and display all the names in a vertical list. I have already done this.

Now from this list of names I would like viewers to click on the names which will bring up the entire profile(record) of this clicked name from the MySQL table. This I need advice on how to accomplish this.
eg.
John Doe
Tom Dick
Cindy Sue
I would like when you click on Tom Dick the records for Tom Dick in the database (address/contact/etc) to come up.

I'll appreciate all help.
Thanks
Dave
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Links on MySQL output list

Post by califdon »

I will assume that your database table has a primary key field (an ID number, for example). That's absolutely essential because it's the only way to identify what record you want to display the details for. In the query that you use to get the names for your vertical list, you must select the names AND the key field, something like this:

Code: Select all

$sql = "SELECT lastname, firstname, id FROM xxxxxx";
Then when you echo each line for your list, you format it either as an HTML hyperlink (anchor) or as an HTML span with an onClick property--in either case, calling another PHP script using the appropriate key field data as a URL parameter. That probably sounds complicated to you at this point, but it's really easier to do than to describe. You probably now have a loop that extracts rows from your query result set and prints them, right? This isn't much more complicated, except that you need the name AND the key field for each entry. It will look something like this, roughly:

Code: Select all

...
$sql = "SELECT lastname, firstname, id FROM xxxxxx";
$result = mysql_query($sql);
while($row=mysql_fetch_array($result)) {
   echo "<a href='details.php?key=".$row['id'].">".$row['firstname']." ".$row['lastname']."</a><br />";   
}
or the last line could be like:

Code: Select all

  echo "<span onClick='window.location=\"details.php?key=".$row['id'].";\">".$row['firstname']." ".$row['lastname']."</span><br />";
Now you need to write details.php script, which will look for a GET parameter named 'key':

Code: Select all

if(isset($_GET['key']) $key=$_GET['key'];
and then it will generate a query to find the record identified with $key and format the display for the browser.

Notice that if you have a lot of names in the database, your first page with the vertical list is going to be really long. This is not normally done this way. Instead, it's usually desirable to ask the user to enter a name or part of a name and have the database search for matching records.
dv_evan
Forum Commoner
Posts: 42
Joined: Wed Apr 09, 2008 8:23 am

Re: Links on MySQL output list

Post by dv_evan »

Thanks Willie Nelson,

I will try this out and post a feed back soon.

Your help is highly appreciated.
Thanks
Dave
Post Reply