I have a simple database. I want to list the user name on the left and show the user's detail on the right. When I click on another name on the left, the details will be changed to the corresponding user on the right.
It should look something like this:
------------------------------------------------------------
|Smith, John...| Name: John Smith
|Hesster, Joe..| Age: 10
|Johns, Zoe....| Grade: 5
|..................| Address: 123 Heaven's Court, Angel, Utopia
|..................|
-------------------------------------------------------------
You get the idea...
I have no problem of listing the names on the left, but, with limited experience in PHP, I have not figured out how to do the details part. I know I would like to use $PHP_SELF and may be $_GET. Any suggestions?
Dynamic page from SQL query
Moderator: General Moderators
- becky-atlanta
- Forum Commoner
- Posts: 74
- Joined: Thu Feb 26, 2009 6:26 pm
- Location: Atlanta, GA
Re: Dynamic page from SQL query
Well depends on if you have a unique way to identify the people in your database. If each Person in your database has lets say an ID then this could be done very simply.
When you out put the users names from your database, you could have the href link include the id. For example,
if User Smith, John has an id of lets say 1, then when you hyperlink his name do something like
<a href="yourpage.php?id=1">Smith, John</a> This will be the html output you would want so the php code should be something like
echo '<a href="yourpage.php?id="' . $id . '">' . $username . '</a>';
Then when you click on it you get the id in the url that you can use the $_GET to grab the id so you can search the related info in your database.
hope that helps.
When you out put the users names from your database, you could have the href link include the id. For example,
if User Smith, John has an id of lets say 1, then when you hyperlink his name do something like
<a href="yourpage.php?id=1">Smith, John</a> This will be the html output you would want so the php code should be something like
echo '<a href="yourpage.php?id="' . $id . '">' . $username . '</a>';
Then when you click on it you get the id in the url that you can use the $_GET to grab the id so you can search the related info in your database.
hope that helps.
- becky-atlanta
- Forum Commoner
- Posts: 74
- Joined: Thu Feb 26, 2009 6:26 pm
- Location: Atlanta, GA
Re: Dynamic page from SQL query
Thank you very much for the tips, Matthew.
I found something on Query_string shortly after I posted, so that helped me to think in the direction you pointed out. It's really not as hard as I thought. This is very useful for many application. I appreciate your time.
Here is how I did it...
I found something on Query_string shortly after I posted, so that helped me to think in the direction you pointed out. It's really not as hard as I thought. This is very useful for many application. I appreciate your time.
Here is how I did it...
Code: Select all
mysql_connect($hostname, $username, $password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
$query = "SELECT * FROM registration ORDER BY camper_lastname, camper_firstname";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo "<a href=\"".$_SERVER["PHP_SELF"]."?id=".$row['id']."\" class=\"nav-top\">".$row['camper_lastname'].", ".$row['camper_firstname']."</a><br>\n";}
?>
<!-- InstanceEndEditable --></td>
<td width="570" class="body2-table-right"><!-- InstanceBeginEditable name="EditRegion4" -->
<?php
if (isset($id)) {
$queryone = "SELECT * FROM registration WHERE id=$id";
$resultone = mysql_query($queryone) or die(mysql_error());
$row = mysql_fetch_array($resultone);
echo "<table width=\"100%\">\n";
echo "<tr>\n";
echo "<td width=\"20%\"><p>Parent:</p></td>";
echo "<td width=\"80%\"><p>".$row['parent_lastname'].", ".$row['parent_firstname']."</p></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td width=\"20%\"><p>Address:</p></td>";
echo "<td width=\"80%\"><p>".$row['address'].", ".$row['city'].", ".$row['state']." ".$row['zip']."</p></td>\n";
.
.
.