Index page then clickable results page

A

Anonymous

Guest
I'm new at coding but to me this is a basic simple problem, even though I've asked a few people and they look at me like I have two heads.

What I want to do is have an index page generated from a MySQL table. I have the table made and am using this code to make the NAMES on the index page:

<?php
$dbh=mysql_connect ("localhost", "******", "******") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("********",$dbh);
$sql = "SELECT * From photos";
$result = mysql_query($sql, $dbh) or die(mysql_error());
while ($newArray = mysql_fetch_array($result)){
$Name = $newArray['Name'];
echo "<strong>$Name</strong> <br><br>";
}
?>

This works fine but here is my problem:

I want to make each name clickable so that when clicked the next page will be a page where the information for that singular name is displayed. So that when each name is clicked on the index page, the same page will come up with whatever the information is for that name. This way if I have 50 names, I won't have 50 HTML pages, only the one page that is used over and over.

Thanks,

:?:
 
germanic42 said:
I want to make each name clickable so that when clicked the next page will be a page where the information for that singular name is displayed. So that when each name is clicked on the index page, the same page will come up with whatever the information is for that name. This way if I have 50 names, I won't have 50 HTML pages, only the one page that is used over and over.

So create another PHP page, e.g. page2.php, and then output the names on the first page like this:

Code:
<?
echo "<a href="page2.php?name=" . $Name . "">$Name</a>"
?>

Then on page2.php, use $_GET['name'] to find out which name was passed and then output the data for that name. I trust you can assemble the requisite SQL yourself.
 
Then on page2.php, use $_GET['name'] to find out which name was passed and then output the data for that name.

Thanks for your help.

I've been able to complete the index page but I'm having trouble finding documentation on the &_GET variable.

What I've been able to do on the data page so far is:

<?php
$dbh=mysql_connect ("localhost", "*****", "*******") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("********",$dbh);
$sql = "SELECT Name, Info, Comment, Letter, Photo From photos";
$result = mysql_query($sql, $dbh) or die(mysql_error());
while ($newArray = mysql_fetch_array($result)){
$Name = $newArray['Name'];
$Info = $newArray['Info'];
$Comment = $newArray['Comment'];
$Letter = $newArray['Letter'];
$Photo = $newArray['Photo'];
echo "$Name<br>$Info<br>$Comment<br>$Letter<br>$Photo<br><br><br></a>";
}
?>

As you can see I'm stuck on the $_GET variable, as to how to use it and it's placement and an "IF" arguement if it is needed.

Thanks,
 
Code:
$sql = 'SELECT * From photos where photo_id = "'.$_GET['phto_id'].'"'; 
$result = mysql_query($sql, $dbh) or die(mysql_error()); 
while ($newArray = mysql_fetch_array($result)){ 
$Name = $newArray['Name']; 
$Info = $newArray['Info']; 
$Comment = $newArray['Comment']; 
$Letter = $newArray['Letter']; 
$Photo = $newArray['Photo']; 
echo "$Name<br>$Info<br>$Comment<br>$Letter<br>$Photo<br><br><br></a>"; 
}

you need a WHERE clause in your sql statement. I would also suggest to use an autoincremented index in your database.
 
The code works exactly as you've printed it. You say I would need a WHERE clause. What would that do (since the code works as is)?

Also could you recommend a good book on the subject.

I have PHP and MYSQL Web Development Second Edition by Welling and Thomson and SAMS Teach Yourself PHP, MySQL and Apache by Meloni.

Thanks,
 
If you want to get info only on a specific record instead of all of them you need the where clause.
 
Thanks, I'll use that on the new web page I'm currently working on. I didn't need that on the last one.
 
make one thing clear
know the integration of php with other langues too
php5 is the real fun 8)
 
Back
Top