Forum Moderators: coopster
I have a database full of items with detailed information about each. I have one page with a list of all the items, which each link to detailed information about the item. What I would like to do is automatically generate new links and detail pages each time something is added to the database.
So far what I have is the list of items using a loop so that new items are automatically added. I would like to use one php file for the item details page, which would display unique information according to the item's id. How can I make each item on the list a link that would display a unique details page?
if i understand correctly you want to have one php page (template) that shows product info depending on which product they clicked on.
something like
www.site.com/product.php?product=1345
Is there some kind of unique identifier in your database that references one specific product (primary key)? Sounds silly btu you would be surprised how many times they aren't there.
In your loop where you write your product list (wasn't sure if they were links already) you can write the variable onto the end of the url or write the link if they aren't already linked.
make any sense? hit me with your questions ;)
You don't say what scripting language or database you're using, but if it is PHP and MySQL, there is a tutorial on the MySQL website about building a database driven site. It should help you on the way...
[mysql.com...]
Hope this helps!
Is there some kind of unique identifier in your database that references one specific product (primary key)?
Yes, every item has a unique id.
www.site.com/product.php?product=1345
That's the type of link I want. I thought maybe it could be done by linking to product.php and include a command in the link telling it to get the id (something like product.php?id=$_GET['id']). I know that doesn't work, but it's the idea I'm going for.
You don't say what scripting language or database you're using, but if it is PHP and MySQL, there is a tutorial on the MySQL website about building a database driven site.
I'm using php4 with mySQL. I looked around on both of the official sites for php and mySQL, as well as many searches through google, but I'm having trouble finding explanations specific to doing this. I see links like this all the time, so I'm sure there must be some site that shows an example of how to do it.
It's at: [phpbuilder.com...]
May I suggest downloading the first four chapters of this ebook at:
sitepoint.com/books/
It is free and it had a chapter that shows the PHP/MYSQL commands to retrieve and display the data detail you want on your individual web pages. I tried this and it work for me.
I have also done something similar using multidimensional arrays in php.
Hope this helps!
Akogo
[edited by: jatar_k at 9:30 pm (utc) on May 6, 2003]
[edit reason] not free, delinked [/edit]
On the page that you list all your product links you will need to do a database query to select all(or some) of the products' ids. Then loop through the recordset that was returned to build your links.
Example:
$query = "SELECT prod_id,prod_name FROM products";
$result = mysql_query($query);while ($i = mysql_fetch_array($result)){
print <<<END
<a href="/show_products.php?prod_id=$i[prod_id]">$i[prod_name]</a><br />
END;
}
products.php:
$query = "SELECT * FROM products WHERE prod_id = $_POST['prod_id']";
$result = mysql_query($query);
while ($i = mysql_fetch_array($result)){
// Display info here //
}
Maybe that'll help you picture the concept better. Dynamic site design is a nice tool to have in your bag. Have fun!
Edit reason: Fixed two code blunders
[edited by: Birdman at 11:01 pm (utc) on May 6, 2003]
parse error, unexpected '{'
for this line:
while ($i = mysql_fetch_array($result){
I changed that line on both the links page and the results page to this:
while ($i = mysql_fetch_array($result) ) {
That works for the links page, but I get this error now for the results page:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
I'm not sure what's causing that error... (?)
means that something is wrong with the query itself.
try changing this
$query = "SELECT * FROM products WHERE prod_id = $_POST['prod_id']";
to
$query = "SELECT * FROM products WHERE prod_id = $_GET['prod_id']";
since i figure you are passing the prod_id via $_GET not $_POST
A good way to troubleshoot your queries is to print the $query variable . Then you can see if your $prod_id is set. You can copy/paste the $query var into phpMyAdmin to see what happens and get a better explanation.
MySQL Manual: Tutorial Introduction [mysql.com]
Perhaps I should read it again too ;0