Passing values to another PHP page
I have a listProducts.php page that querys a table in my phpMyAdmin database. It takes the information and puts it out onto the page using a while loop. Here is the code:
PHP Code:
<?php
include("db.inc.php");
$db=open_db("test");
$sql="Select image, name, description, price from ss02_products";
$result=query_db($sql);
?>
<table summary="">
<tr>
<th></th><th>Name</th><th>Description</th><th>Price</th>
</tr>
<?php while($row = get_row_array($result)){ ?>
<tr>
<td><a href="productInfo.php"><img alt="<? echo $row['name'];?>" src="<? echo $row['image'];?>" width="200" height="200"/></a></td>
<td><a href="productInfo.php"><? echo $row['name'];?></a></td>
<td><? echo $row['description'];?> </td>
<td>$<? echo $row['price'];?> </td>
</tr>
<?php } ?>
</table>
My question is this: How do I pass a value to another PHP page that way I can have it dynamically bring up just that product info, instead of putting them all on the page. Its kinda like a link to a more detail product info page.
I was thinking that I need some sort of count variable that I incrememnt each time through the loop that corresponds to the productID in my phpMyAdmin table. How would I pass this variable to another PHP page which will then use it in an SQL statement that will get the info for that productID from the table in the Database. How would I put it into an HREF tag to send it to that page?
Something like
<a href="productInfo.php?index=<?php echo $i++ ?>" > Some product<a/>
What do you think? SHould i do it this way or should I possible try something with Smarty or a Session or cookie?
Thanks for the help.
Re: Passing values to another PHP page
I got this working. What do you think?
Here is my listProducts.php which just lists each product and a short description in a table.
PHP Code:
<?php
include("db.inc.php");
$db=open_db("test");
$sql="Select image, name, shortdescription, price from ss02_products";
$result=query_db($sql);
$i=1;
?>
<table summary="">
<tr>
<th></th><th>Name</th><th>Description</th><th>Price</th>
</tr>
<?php while($row = get_row_array($result)){ ?>
<tr>
<td><img alt="<? echo $row['name'];?>" src="<? echo $row['image'];?>" width="200" height="200"/></td>
<td><a href="productInfo.php?id=<?php echo $i++ ?>"><? echo $row['name'];?></a></td>
<td><? echo $row['shortdescription'];?> </td>
<td>$<? echo $row['price'];?> </td>
</tr>
<?php } ?>
</table>
Here is my productInfo.php page that gets an index, then querys the table to get that products info.
PHP Code:
<?php
include("db.inc.php");
$index = $_SERVER['QUERY_STRING'];
$db=open_db("test");
$sql="Select image, name, description, price from ss02_products where $index";
$result=query_db($sql);
$row = get_row_array($result);
?>
<html>
<head>
<title><? echo $row['name'];?></title>
</head>
<body>
<table summary="">
<tr>
<th></th><th>Name</th><th>Description</th><th>Price</th>
</tr>
<tr>
<td><img alt="<? echo $row['name'];?>" src="<? echo $row['image'];?>" width="200" height="200"/></td>
<td><a href="productInfo.php?index=<?php echo $i++ ?>"><? echo $row['name'];?></a></td>
<td><? echo $row['description'];?> </td>
<td>$<? echo $row['price'];?> </td>
</tr>
</table>
</body>
</html>
Re: Passing values to another PHP page
Does each produce have an ID in the table. If so, add this to the feilds in your select querry and append that to the URL.
Re: Passing values to another PHP page
By the way, phpMyAdmin is a frontend for MySQL, so it's a "MySQL database" :)