Thread: Storing prices
View Single Post
Old 11-09-2012, 09:13 PM   PM User | #37
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
Thank you.

Done a lot of work since then and I've got it working!.....nearly

When I load the products page at first, I display ALL of the products, when they click a link, it displays just those images which is good.

Only problem is, I have 1 description for ALL of the bench images for example, but I only want it to be displayed once.

This is what I want:

1) user clicks specific product
2) generate <h2> - product_name
3) display the description (for example, "Our benches are made from bla bla bla, treated and stained bla bla").

That is it.

The problem is, when product.template.htm first loads, it displays ALL of my products, and I'm not sure how to display it like so:

Bench
Description
image1
image2
image3
image4
image5

Tables
Description
image1
image2
image3
image4
image5

etc

Here is my code:
index.php
PHP Code:
<?php 

if (isset($_GET['page']) && $_GET['page'] == "products") {
   
   
$get_values = array("Benches","Tables","Fencing","Bird Housing","Planters","Gates","Bin Stores","Decking","Sheds","Pet Housing");
   
   if (isset(
$_GET['order'])){
     
      if(
in_array($_GET['order'],$get_values)){
         require(
"core/get_products.php");
      }
      else{
         
header("Location: index.php?page=products");
      }
   
       
   }
   else{
      require(
"core/get_products.php");
   }
}

include(
"core/init.inc.php");

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?php echo "Gardenable - ".$title?></title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
<script type="text/javascript" src="js/clock.js"></script>
</head>

<body>
<div id="container">

   <div id="header">
      <img src="images/gardenable2.fw.png" alt="Gardenable Logo" title="Gardenable" id="logo" border="0" />
      
      <div id="navigation_div">
         <img src="images/flowerbed.fw.png" alt="Navigation Image" id="flowerbed_img" border="0" />
         <ul>
             <li><a href="?page=home">Home</a></li>
             <li><a href="?page=about">About</a></li>
             <li><a href="?page=products">Products</a></li>
             <li><a href="?page=contact">Contact</a></li>
             <li><a href="?page=find">Find Us</a></li>
         </ul>
      </div>
   </div>
   
   <div id="content">
     
     <?php include($include_page); ?>
   
   </div>
   
   
   
   <div id="footer">
   
   
   </div>
   
</div>
<p id="pageviews"><?php echo "Page Hits: ".$page_views?></p>
</body>
</html>
Click product link, goes reloads index.php, order is not set so we go into get_products.php which is here:
PHP Code:
<?php
//if the code gets this far, we know that the user is viewing the product page and has clicked a link to view. save value to variable.
$product = isset($_GET['order']) ? $_GET['order'] : "";
//connect to server, select db, query table.
$connection mysql_connect("localhost","root","") or die("Error connecting to server.");
$database mysql_select_db("gardenable",$connection) or die("Error selecting the database.");

//setup variables ready to be used with the database data.
$count 0;//used to iterate through $product_array to store results.
$products = array();//array to store database data.

$full_dir dirname(__FILE__);
$dir_files scandir($full_dir);
$image_directory "core/".$dir_files[6]."/";//$dir_files holds the folder name where the images are located.

if($product == ""){
$query mysql_query("SELECT P.productID, P.product_name, P.product_details, I.product_price, I.imgName 
                      FROM products AS P,product_images AS I 
                      WHERE P.productID = I.productID"
) or die("error selecting ALL records");              
}
else {
$query mysql_query("SELECT P.*, I.imgName, I.product_price 
                      FROM products AS P, product_images AS I 
                      WHERE P.productID = I.productID AND P.product_name = '{$product}'"
) or die("error selecting records");
}

while (
$row mysql_fetch_array($query)) {
                           
                           
$products[$count] = array(
                               
"productID" => $row['productID'],
                               
"productName" => $row['product_name'],
                               
"productDetails" => $row['product_details'],
                               
"productPrice" => "&pound;".$row['product_price'],
                               
"productImage" => "<img src='{$image_directory}{$row['imgName']}' alt='{$row['imgName']}' title='{$row['product_name']}' id='{$row['product_name']}{$count}' />",
                               
"imageName" => $row['imgName']
                           );
                           
$count++;
                      }

$count 0;//reset count so it can be used again but this time for the purpose of incrementing the individual product div elements attributes.
?>
As you can see, if the $_GET['order'] value variable is equal to an empty string, we select every image where the ids match in both which gets ALL my images and if it isn't equal to an empty string we select everything which matches. Great, this works good.

I save all the data I need into an array. Then in my products.template.htm, I have this code:

PHP Code:
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. 
   The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, 
   content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as 
   their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy.</p>
   
<div id="products_wrapper">
    <div id="product_menu_div">
        <ul>
           <li><a href="?page=products&order=Benches">Benches</a></li>
           <li><a href="?page=products&order=Bin Stores">Bin Stores</a></li>
           <li><a href="?page=products&order=Bird Housing"><li>Bird Housing</a></li>
           <li><a href="?page=products&order=Decking"><li>Decking</a></li>
           <li><a href="?page=products&order=Fencing"><li>Fencing</a></li>
           <li><a href="?page=products&order=Gates"><li>Gates</a></li>
           <li><a href="?page=products&order=Pet Housing"><li>Pet Housing</a></li>
           <li><a href="?page=products&order=Planters"><li>Planters</a></li>
           <li><a href="?page=products&order=Sheds"><li>Sheds</a></li>
           <li><a href="?page=products&order=Tables"><li>Tables</a></li>
        </ul>
    </div>
    
    <?php
    
    $heading 
= (isset($heading)) ? $heading "";
    
$brief = (isset($brief)) ? $brief "";
    
    echo 
"<h1 id='product_heading'>{$heading}</h1>";
    echo 
"<p id='product_description'>{$brief}</p>";
    
    if (isset(
$products)){
    
       echo 
"<div id='product_holder'>";   
            
              foreach (
$products as $product_info){
                  
                  echo 
"<div class='prod'>";
              
                  echo 
"<a href='{$image_directory}{$product_info['imageName']}'>".$product_info['productImage']."</a>";
                  echo 
$product_info['productPrice'];
           
                  echo 
"</div>";
              
                  
$count++;
    
              }
           
           
           echo 
"<hr id='last' />";
       
       echo 
"</div>";
    }
    
    
?>
    
</div>
Not sure if you can understand what I was trying to explain right at the top of this post but when ALL the images are being displayed, I want it to display the product name and description once then all the images associated, when the product name changes i.e, there are no more bench images, I want it to print another heading let's say 'Tables' and the description once, then all the images associated.

Do you know how I can add that functionality to what I already have done?

Hope I have explained well enough!

Kind regards,

LC.

Last edited by LearningCoder; 11-09-2012 at 09:19 PM..
LearningCoder is offline   Reply With Quote