943,616 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1348
  • PHP RSS
Mar 8th, 2008
0

Small catalog problem

Expand Post »
hi, i am trying to make a simple catalog website without checkout. the user clicks on a product he want and it's displaced on another page...simple. the first page (the one with the product list) has the following code...
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. $sql = "SELECT * FROM products";
  3. $result = mysql_query($sql);
  4. if (!$result)
  5. {
  6. echo "Error - query: $sql - " . mysql_error();
  7. exit;
  8. }
  9.  
  10. while ($row = mysql_fetch_array($result)) {
  11. echo "<p class='rc_textAreatext'><a href='viewprod.php?id=" . $row['prod_name']. "'>"
  12. . $row['prod_name'] . "</a> - " . $row['description'] .
  13. "&nbsp;&nbsp;&nbsp;$" .$row['price'] . "</p>";
  14. }
  15. ?>

on the next page (viewprod.php) i have no idea what to put on the page to call up the product clicked. i know i read it in my books but i cant find it anywhere...can someone help?
Thanks

p.s. sorry if this is a dumb question
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
mikki2 is offline Offline
68 posts
since May 2006
Mar 8th, 2008
0

Re: Small catalog problem

Place a hidden field in your script relating to the item in the record set that you are calling, and use something like this:
php Syntax (Toggle Plain Text)
  1. <a href="viewprod.php?ID=<?php echo $rs['ID']; ?>"target="_blank"><strong>View</strong></a>
on your viewprod.php get the value of the item passed in the URL

php Syntax (Toggle Plain Text)
  1. $colname_rs_ID = "-1";
  2. if (isset($_GET['ID'])) {
  3. $colname_rs_ID = (get_magic_quotes_gpc()) ? $_GET['ID'] : addslashes($_GET['ID']);
  4. }
  5. mysql_select_db($database, $conn);
  6. $query_rs_ID = sprintf("SELECT * FROM your_db_table WHERE ID = %s", $colname_rs_ID);
  7. $rs_ID = mysql_query($query_rs_ID, $conn) or die(mysql_error());
  8. $row_rs_ID = mysql_fetch_assoc($r_ID);
  9. $totalRows_rs_ID = mysql_num_rows($rs_ID);
then show the values from the record set on the screen

php Syntax (Toggle Plain Text)
  1. <input type="hidden" name="ID" value="<?php echo $row_rs_ID['ID']; ?>" />
  2. <?php echo $row_rs_ID['ID']; ?>
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
JeniF is offline Offline
52 posts
since Aug 2007
Mar 9th, 2008
0

Re: Small catalog problem

ok thanks i'll go try it out
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
mikki2 is offline Offline
68 posts
since May 2006
Mar 11th, 2008
0

Re: Small catalog problem

i just got around to using the code. i sorry i dont really understand the code. first, why do you need $colname_rs_ID = "-1";, why is it -1? And second, arent hidden fields used to get data submitted from a form not to output it?

Thanks again.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
mikki2 is offline Offline
68 posts
since May 2006
Mar 11th, 2008
0

Re: Small catalog problem

just use the get method to retrieve the info from the previous page;

php Syntax (Toggle Plain Text)
  1. //I suppose you have the connection here....
  2. $id=$_GET['id'];
  3. $sql = "SELECT * FROM products
  4. WHERE id='$id'";
  5. $result = mysql_query($sql);
  6. if (!$result)
  7. {
  8. echo "Error - query: $sql - " . mysql_error();
  9. exit;
  10. }
  11.  
  12. $id=mysql_result($result,0,"id");
  13. $name=mysql_result($result,0,"prod_name");
  14. $desc=mysql_result($result,0,"description");
  15. $price=mysql_result($result,0,"price");
  16.  
  17. echo "ID:".$id."<br>";
  18. echo "NAME:".$name."<br>";
  19. echo "DESCRIPTION:".$desc."<br>";
  20. echo "PRICE:".$price."<br>";
Reputation Points: 28
Solved Threads: 71
Posting Pro
ryan_vietnow is offline Offline
578 posts
since Aug 2007
Mar 12th, 2008
0

Re: Small catalog problem

Click to Expand / Collapse  Quote originally posted by mikki2 ...
PHP Syntax (Toggle Plain Text)
  1. while ($row = mysql_fetch_array($result)) {
  2. echo "<p class='rc_textAreatext'><a href='viewprod.php?id=" . $row['prod_name']. "'>"
  3. . $row['prod_name'] . "</a> - " . $row['description'] .
  4. "&nbsp;&nbsp;&nbsp;$" .$row['price'] . "</p>";
  5. }
when you build the link for your product detail page, use $row['prod_id'] (or how your id column is named in your DB) instead of $row['prod_name'].

after that, in your product page do:
if(isset($_GET['id']))
{
$id = $_GET['id'];
// show details for this ID
}
else
{
echo "Sorry!!! We don't have this product!"

}
Reputation Points: 10
Solved Threads: 15
Junior Poster in Training
silviuks is offline Offline
95 posts
since Apr 2006
Mar 12th, 2008
0

Re: Small catalog problem

just use the get method to retrieve the info from the previous page;

php Syntax (Toggle Plain Text)
  1. //I suppose you have the connection here....
  2. $id=$_GET['id'];
  3. $sql = "SELECT * FROM products
  4. WHERE id='$id'";
  5. $result = mysql_query($sql);
  6. if (!$result)
  7. {
  8. echo "Error - query: $sql - " . mysql_error();
  9. exit;
  10. }
  11.  
  12. $id=mysql_result($result,0,"id");
  13. $name=mysql_result($result,0,"prod_name");
  14. $desc=mysql_result($result,0,"description");
  15. $price=mysql_result($result,0,"price");
  16.  
  17. echo "ID:".$id."<br>";
  18. echo "NAME:".$name."<br>";
  19. echo "DESCRIPTION:".$desc."<br>";
  20. echo "PRICE:".$price."<br>";
Looks easy enough. I'm currently reading "Sam's Teach Yourself PHP and Mysql in 24 hour". i'll checkout your code soon. Thanks
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
mikki2 is offline Offline
68 posts
since May 2006
Mar 13th, 2008
0

Re: Small catalog problem

@ryan_vietnow
i have tried the code out but i have one question. Whats the point of adding the "0" to ($result,0,"id"); and the other lines? i erased the "0" when i was testing the code and there seemed to be no difference. when i changed the number to 5 or something else i get the folloting error:

Warning: mysql_result() [function.mysql-result]: Unable to jump to row 5 on MySQL result index 3 in D:\AppServ\www\Study_Practice\Test_Sites\Shopping Cart\showitem.php on line 22

i'm just wondering is all. from what i get, the point of the "0" is to tell mysql which row to start searching. if i'm mistaken pls correct me
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
mikki2 is offline Offline
68 posts
since May 2006
Mar 13th, 2008
0

Re: Small catalog problem

yes,that is correct,"0" is the row(which means first)
where the result of the query will be displaying.

if I put there 1,it means that the data from the 2nd row will be displayed.
Reputation Points: 28
Solved Threads: 71
Posting Pro
ryan_vietnow is offline Offline
578 posts
since Aug 2007
Mar 13th, 2008
0

Re: Small catalog problem

ok thanks alot...i guess thats 41 solved threads for you
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
mikki2 is offline Offline
68 posts
since May 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: problem solved sort of
Next Thread in PHP Forum Timeline: Dynamic content and printer pages





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC