Jump to content

how to generate a product details page from selecting on a general page


Recommended Posts

I need to create a new page when an item is selected with <a href>. So a new page would open for the item, or any other item. I am not sure how to pass that data to a new page where all the details for the product will be extracted from the database. Can this be done with only PHP? I know how to do it with a form, but a form has a submit and action, not so with a link.

php is well suited for this task

 

create href links by using unique values, the post id can be this value

 

echo "<a href='script_name.php?id=$id'>title id more</a>":

 

On the script page use a $_GET['id'], a mysql query for that id, and display the results you want.

 

I am having trouble passing ['id'] from the original page to the phpscript.php page, i checked to make sure mysql_query works, and it does.

but $_GET is not doing anything

 

I guess I just don't know how to get the ['id'] to pass or my SELECT is not written properly, the more I think about it the more I am confused

 

help! PLEASE

OK, so this line is on my page of various products

echo "<p class=\"name\"><a href='display_product.php?id=$id'>".$name."</a></p>";

(of course I have the entire script for mysql query and variables)

this is the actual page

http://www.mywebsitepaid.com/installed/index.php

 

I repaired my 404error, it was a stupid mistake of subcategories

 

on the page to display the details of $id, I recopied most of the previous code from initial page

 

     

  $records = "SELECT * FROM items";    <-------- not sure how to make this grab the $id from previous page

 

      $query_records = mysql_query($records);

      $num_records = mysql_num_rows($query_records);

     

      if ($num_records == 0) {

        echo "The menu is closed for now.";

        }

 

  $row = mysql_fetch_assoc($query_records);

 

I removed other lines that kept showing error, can you explain how to use $_GET['id'] to create a query for that item?

I am very new to php/mysql

 

You can access the value that you pass through GET via the $_GET super global. You would use it like any other PHP array. So for example, you could do

$id = $_GET['id'];
echo $id;//would echo the id of the product in the database

 

Now what you want to do is use this value in the query. Without knowing the exact column name, I can't give you the exact code, but I can guess what it looks like

$id=$_GET['id'];
$query = "SELECT * FROM items WHERE id=$id";
...

 

 

Now, you should also remember to sanitize your input so you protect against sql injections. Since $id should always be a number, you could probably get away with simply casting it into an int using a PHP cast or the intval function like so

$id = intval($_GET['id']);
$query = "SELECT * FROM ...";

 

yes I tried that and still nothing, so i copied your code and entered it, and still nothing

here is what i have before entering what you mentioned. I realize there are shorter ways to do this but I am new and need to learn

 

   $id = $_GET['id'];
echo $id;//would echo the id of the product in the database
  
  
      
  $records = "SELECT * FROM items";   //when I enter the "WHERE id=$id"; I get errors on line 16 and 22 and $num_records error is TRUE and displays "the menu is closed for now."
  
      $query_records = mysql_query($records);
      $num_records = mysql_num_rows($query_records);
      
       if ($num_records == 0) {
        echo "The menu is closed for now.";
        }

  $row = mysql_fetch_assoc($query_records);
    //define variables  
      $name = ($row['prod_name']); 
      
      $category = ($row['prod_cat']);
        if($category == 'alarm'){
          $category = 'Security System';
          }elseif($category == 'alarm/start'){
            $category = 'Alarm with Remote Start';
              }else{
                $category = 'Keyless Entry System';}
      
      $description = ($row['prod_descrip']);
                                        
      $noimg = FALSE;
      
        $img1a  = ($row['img1a']);
      $img1 = ($row['prod_img1']);
        if ($img1 == NULL){
          $img1 = $noimg;}
          else{
            $img1 = '<img class="img1this" src="images/products/'.$img1.'" alt="'.$img1a.'" />';}
            
      
         
        $img2a  = ($row['img2a']);
      $img2 = ($row['prod_img2']);
        if($img2 == NULL){
          $img2 = $noimg;
          }else{
            $img2 = '<img class="imgsm" src="images/products/'.$img2.'" alt="'.$img2a.'" />';}
                 
        $img3a  = ($row['img3a']);
      $img3 = ($row['prod_img3']);
        if($img3 == NULL){
          $img3 = $noimg;
          }else{
            $img3 = '<img class="imgsm" src="images/products/'.$img3.'" alt="'.$img3a.'" />';}
                         
        $img4a  = ($row['img4a']);
      $img4 = ($row['prod_img4']);
        if($img4 == NULL){
          $img4 = $noimg;
          }else{
            $img4 = '<img class="imgsm" src="images/products/'.$img4.'" alt="'.$img4a.'" />';}
            
      $msrp = ($row['prod_msrp']);
      $sale = ($row['prod_sale']);
      $active = ($row['prod_active']);
      
    

?>

 

MOD EDIT: code tags added

I wish so much to be a good php scriptor, but the books are horrible and many outdated, I have 7 books on php and they just suck. almost all of them make gross assumptions and focus on layout. If someone has a good book in mind I would be eternally grateful. Online is also a mixed bag.

I have done insert, POST and a few other items and regularly use includes and other simple functions

 

I am lost with arrays and creating functions - none of the titles I own explain very well.

Maybe I'll just have to be here a lot.

I am a much better electronics installer -

When you click on one of the links, does the browser's address bar show an expected value for the display_product.php?id=???

 

The error you mentioned "when I enter the "WHERE id=$id"; I get errors on line 16" is because the $id variable is empty in the query. Is the code you posted all the code between where you are setting $id = $_GET['id'] and where you are using $id in the query?

yes, the id number does appear in the url as the last item

 

and yes, this is the code between setting and using --- above the code i tried session

below the code I have my header and rest of page

 

i tried echo on $id within the page and nothing

 

both pages have virtual the same script except $_GET function as you instructed and some variables of how to echo what in my page -- rest is the same

 

i copied and paste the code that was posted

ENTIRE PRODUCT PAGE CODE

<?php
session_start();
ob_start(); ?>
<?php
  require('scripts/dbconnect.php');
// this section found in body of page - continuation of script above header

   $id = $_GET['id'];
echo $id;//would echo the id of the product in the database
  
  
      
  $records = "SELECT * FROM items WHERE id=$id";  
  
      $query_records = mysql_query($records);
      $num_records = mysql_num_rows($query_records);
      
       if ($num_records == 0) {
        echo "The menu is closed for now.";
        }

  $row = mysql_fetch_assoc($query_records);
    //define variables  
      $name = ($row['prod_name']); 
      
      $category = ($row['prod_cat']);
        if($category == 'alarm'){
          $category = 'Security System';
          }elseif($category == 'alarm/start'){
            $category = 'Alarm with Remote Start';
              }else{
                $category = 'Keyless Entry System';}
      
      $description = ($row['prod_descrip']);
                                        
      $noimg = FALSE;
      
        $img1a  = ($row['img1a']);
      $img1 = ($row['prod_img1']);
        if ($img1 == NULL){
          $img1 = $noimg;}
          else{
            $img1 = '<img class="img1this" src="images/products/'.$img1.'" alt="'.$img1a.'" />';}
            
      
         
        $img2a  = ($row['img2a']);
      $img2 = ($row['prod_img2']);
        if($img2 == NULL){
          $img2 = $noimg;
          }else{
            $img2 = '<img class="imgsm" src="images/products/'.$img2.'" alt="'.$img2a.'" />';}
                 
        $img3a  = ($row['img3a']);
      $img3 = ($row['prod_img3']);
        if($img3 == NULL){
          $img3 = $noimg;
          }else{
            $img3 = '<img class="imgsm" src="images/products/'.$img3.'" alt="'.$img3a.'" />';}
                         
        $img4a  = ($row['img4a']);
      $img4 = ($row['prod_img4']);
        if($img4 == NULL){
          $img4 = $noimg;
          }else{
            $img4 = '<img class="imgsm" src="images/products/'.$img4.'" alt="'.$img4a.'" />';}
            
      $msrp = ($row['prod_msrp']);
      $sale = ($row['prod_sale']);
      $active = ($row['prod_active']);
      
    

?>
<?php $title="Car Electronics Installation 12 Volt"; ?>

<?php require('req/header.php'); ?>
<body>
<div id="wrapper-wrap">
  <div id="wrapper">
    <?php require('req/banner.php'); ?>
    <?php require('req/nav.php'); ?>
   
      <div class="clear"></div>
      <!-- end searches -->
      <div id="body">
     <h1>       </h1>
     
     <?php
          
     //display on page
   echo '<div class="thisitem">';                                                           
      echo '<p class="cat">'.$category.'</p>';
      echo $img1; 
      echo $img2;
      echo $img3;
      echo $img4; 
      echo '<div class="sidethisitem">';                      
      echo '<p class="namethis">'.$name.'</p>';
      echo '<p class="descripthis">'.$description.'</p>';
      echo '<p class="msrp">Reg. $'.$msrp.'<span class="sale">Sale $'.$sale.'</span></p>';
      echo '</div></div>'; 
                                
echo $id;//would echo the id of the product in the database 
     ?>
     
         
          <div id="right">
              
              
          </div><!-- end right -->
          
          <div class="clear"></div>
      </div><!-- end body -->
      
  </div><!-- end wrapper -->
    <?php require('req/footer.php'); ?>
   
</div>
</body>
</html>

you can go to this page and click any title http://www.mywebsitepaid.com/installed/index.php

error is displayed in #fff on same bg so highlight it with mouse

 

MOD EDIT: code tags added.

if you are getting that "menu is closed" error it means that the database is unable to find any rows with that specific id. Are you sure id is the correct column name? How are you building the ids on the other page (IE in the links, where does the id come from). There has to be a disconnect somewhere, because this is a very simple procedure, one I have done many times with much success using almost the exact same code that I posted.

If the URL contains an ?id=xx value, but your php code is not receiving it, either something is preventing the value from reaching your script or something is overwriting it.

 

What does the following code show, when added immediately after the first <?php tag in display_product.php -

 

echo "<pre>";
echo "GET:";
print_r($_GET);
echo "</pre>";

 

Beyond doing the above to see what the $_GET array contains, it would take seeing all your code and any included/required files on the display_product.php page to be able to help you.

I have one table

/*

 

table items

 

id

prod_name

prod_cat

prod_descrip

prod_img1   

prod_img2

prod_img3

prod_img4

prod_msrp

prod_sale

prod_active

date

*/

 

  id is generated automatically

 

well, if I define $id with 1 of 5 choices, literally 1,2,3,4 or 5  $id = 2; item 2 will display

then the correct item displays

 

while i was typing I saw your message and did what you asked

GET:Array

(

)

 

 

am i mistaken for writing this?

  $query_records = mysql_query($records);

      $num_records = mysql_num_rows($query_records);

     

 

gave you the entire code for the page except some html for display and the css

dbconnect is all constant for conencting to db which is working

 

 

//Set the database access info as constants

DEFINE ('DB_USER', '');

DEFINE ('DB_PASSWORD', '');

DEFINE ('DB_HOST', '');

DEFINE ('DB_NAME', '');

 

//Make the connection

$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die (' admin_connect issue --Could not connect to MySQL: ' . mysql_error());

 

 

//Select the database

$db_select = @mysql_select_db (DB_NAME) OR die ('Could not select the database: ' . mysql_error());

 

this is all i have on the main page

 

<?php

// this section found in body of page - continuation of script above header 
  
  $records = "SELECT * FROM (items)";  
  
      $query_records = mysql_query($records);
      $num_records = mysql_num_rows($query_records);
      
       if ($num_records == 0) {
        echo "The menu is closed for now.";
        }
        
  while($row = mysql_fetch_assoc($query_records)){
    //define variables  
    
    $id = ($row['id']);
      $name = ($row['prod_name']); 
      
      $category = ($row['prod_cat']);
        if($category == 'alarm'){
          $category = 'Security System';
          }elseif($category == 'alarm/start'){
            $category = 'Alarm with Remote Start';
              }else{
                $category = 'Keyless Entry System';}
      
      $description = ($row['prod_descrip']);
      
        $img1a  = ($row['img1a']);
      $img1 = ($row['prod_img1']);
        if ($img1 == NULL){
          $img1 = "no image";}
          else{
            $img1 = '<img class="img1" src="images/products/'.$img1.'" alt="'.$img1a.'" />';}
            
      
      
     // $img2 = ($row['prod_img2']);
     // $img3 = ($row['prod_img3']);
      //$img4 = ($row['prod_img4']);
      $msrp = ($row['prod_msrp']);
      $sale = ($row['prod_sale']);
      $active = ($row['prod_active']);
      
      
     //display on page
     echo '<div class="item">'; 
      echo $img1;                        
      echo "<p class=\"name\"><a href='display_product.php?id=$id'>".$name."</a></p>";
      echo '<p class="descrip">'.$description.'</p>';
      echo '<p class="msrp">Reg. $'.$msrp.'<span class="sale">Sale $'.$sale.'</span></p>';
      echo '<p class="cat">'.$category.'</p>';
      echo '</div>';
      }

?>

plus the dbconnect , css, and html files

 

MOD EDIT: code tags added.

display_product.php?=x (the links on your live page) is not the same as display_product.php?id=x (the correct links that your current code would produce and which would be needed to cause $_GET['id'] to exist.)

 

Has that live site been updated with your current code?

 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.