Forum Moderators: coopster

Message Too Old, No Replies

automatically making unique links with php

question from a beginner

         

Gertrude

8:36 pm on May 6, 2003 (gmt 0)

10+ Year Member



What I want to do seems very basic, but I haven't had any luck finding it explained clearly. Since I'm not sure what doing this is called, I'm having a bit of trouble even looking it up. If anyone would explain this or point me to another source explaining how to do it I would appreciate it!

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?

jatar_k

8:45 pm on May 6, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hello Gertrude,

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 ;)

dmorison

8:48 pm on May 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Gertrude,

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!

jatar_k

8:51 pm on May 6, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



php in title ;)

Gertrude

9:13 pm on May 6, 2003 (gmt 0)

10+ Year Member



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.

olwen

9:18 pm on May 6, 2003 (gmt 0)

10+ Year Member



I found an article on Phpbuilder about "Building Dynamic Pages With Search Engines in Mind" invaluable.

It's at: [phpbuilder.com...]

akogo

9:20 pm on May 6, 2003 (gmt 0)

10+ Year Member



Gertrude:

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]

Birdman

9:31 pm on May 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Gertrude,

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]

Gertrude

10:48 pm on May 6, 2003 (gmt 0)

10+ Year Member



Birdman, your example worked for the links page. I got an error on the results page though:

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... (?)

jatar_k

10:57 pm on May 6, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>>supplied argument is not a valid MySQL result resource

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

Gertrude

11:16 pm on May 6, 2003 (gmt 0)

10+ Year Member



No, it still doesn't work. I played around with changing things and the only thing that seems to work without getting an error is taking out the "WHERE prod_id = $_POST['prod_id']" completely. Of course, when I do that, the page I get isn't unique to the link I clicked.

Birdman

11:21 pm on May 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry to get you messed up with my poor example. I'm sure jatar_k is correct on the $_GET part. On my host's server, I am able to just use the variable($prod_id).

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.

Gertrude

11:31 pm on May 6, 2003 (gmt 0)

10+ Year Member



phpMyAdmin tells me there is an error in my SQL syntax... I'll look it up

Gertrude

11:56 pm on May 6, 2003 (gmt 0)

10+ Year Member



Ok, I got it.

Instead of this:

WHERE id=$_GET['id']

I had to use:

WHERE id='{$_GET['id']}'

Seems like I always have to type out more code for my php than everyone else :/ Thanks for the help everyone!

grahamstewart

12:12 am on May 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To help catch errors in the SQL in future you could change the query line to this..

$result = mysql_query($query)
or die( 'SQL ERROR: '.mysql_error() );

which will stop the script dead in its tracks and report the error returned from MySql.

Birdman

12:16 am on May 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Graham, You're right and I need to start using that method. I always like to do things the hard way :)

It looks like the cause of the error was the quotes. I assumed(wrongly of course) that the prod_id would be an INT, where it was probably a VARCHAR. If it's a string, you need the quotes.

Gertrude

12:27 am on May 7, 2003 (gmt 0)

10+ Year Member



Birdman, my id is an INT. Why is there a difference with a VARCHAR though?

jatar_k

12:31 am on May 7, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



in the sql query you dont need to put quotes around an int but you have to put them around strings/chars/dates

Birdman

12:40 am on May 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here is some good reading to get you rolling with MySQL:

MySQL Manual: Tutorial Introduction [mysql.com]

Perhaps I should read it again too ;0