Small catalog problem

Thread Solved

Join Date: May 2006
Posts: 69
Reputation: mikki2 is an unknown quantity at this point 
Solved Threads: 1
mikki2 mikki2 is offline Offline
Junior Poster in Training

Small catalog problem

 
0
  #1
Mar 8th, 2008
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...
  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 52
Reputation: JeniF is an unknown quantity at this point 
Solved Threads: 5
JeniF's Avatar
JeniF JeniF is offline Offline
Junior Poster in Training

Re: Small catalog problem

 
0
  #2
Mar 8th, 2008
Place a hidden field in your script relating to the item in the record set that you are calling, and use something like this:
  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

  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

  1. <input type="hidden" name="ID" value="<?php echo $row_rs_ID['ID']; ?>" />
  2. <?php echo $row_rs_ID['ID']; ?>
I keep hitting "escape", but I'm still here!!!!
:}
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 69
Reputation: mikki2 is an unknown quantity at this point 
Solved Threads: 1
mikki2 mikki2 is offline Offline
Junior Poster in Training

Re: Small catalog problem

 
0
  #3
Mar 9th, 2008
ok thanks i'll go try it out
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 69
Reputation: mikki2 is an unknown quantity at this point 
Solved Threads: 1
mikki2 mikki2 is offline Offline
Junior Poster in Training

Re: Small catalog problem

 
0
  #4
Mar 11th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 569
Reputation: ryan_vietnow is an unknown quantity at this point 
Solved Threads: 71
ryan_vietnow's Avatar
ryan_vietnow ryan_vietnow is offline Offline
Posting Pro

Re: Small catalog problem

 
0
  #5
Mar 11th, 2008
just use the get method to retrieve the info from the previous page;

  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>";
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 66
Reputation: silviuks is an unknown quantity at this point 
Solved Threads: 11
silviuks silviuks is offline Offline
Junior Poster in Training

Re: Small catalog problem

 
0
  #6
Mar 12th, 2008
Originally Posted by mikki2 View Post
  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!"

}
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 69
Reputation: mikki2 is an unknown quantity at this point 
Solved Threads: 1
mikki2 mikki2 is offline Offline
Junior Poster in Training

Re: Small catalog problem

 
0
  #7
Mar 12th, 2008
Originally Posted by ryan_vietnow View Post
just use the get method to retrieve the info from the previous page;

  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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 69
Reputation: mikki2 is an unknown quantity at this point 
Solved Threads: 1
mikki2 mikki2 is offline Offline
Junior Poster in Training

Re: Small catalog problem

 
0
  #8
Mar 13th, 2008
@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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 569
Reputation: ryan_vietnow is an unknown quantity at this point 
Solved Threads: 71
ryan_vietnow's Avatar
ryan_vietnow ryan_vietnow is offline Offline
Posting Pro

Re: Small catalog problem

 
0
  #9
Mar 13th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 69
Reputation: mikki2 is an unknown quantity at this point 
Solved Threads: 1
mikki2 mikki2 is offline Offline
Junior Poster in Training

Re: Small catalog problem

 
0
  #10
Mar 13th, 2008
ok thanks alot...i guess thats 41 solved threads for you
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC