Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 3 of 3 FirstFirst 123
Results 31 to 41 of 41

Thread: Storing prices

  1. #31
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    925
    Thanks
    76
    Thanked 29 Times in 29 Posts
    Thank you very much for the clarification.

    I will now go ahead and fill in the data! Can't wait to get this thing working, going to be so awesome!

    Kind regards,

    LC.

  2. #32
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    925
    Thanks
    76
    Thanked 29 Times in 29 Posts
    AWESOME! Just ran the query and it returned all my images associated with the value I put in!

    What I noticed is that the descriptions are all the same but I might want to have different descriptions for the same product type.

    Can I move my product_details field into the product_images table and add different descriptions for each product? Then change the query to select I.product_details as well as the I.imgName?

    Kind regards,

    Lc.

  3. #33
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,650 Times in 4,612 Posts
    Of course.

    But this assumes that you won't have multiple images per product_details. If you will, then another DB reorganization is in order.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  4. #34
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    925
    Thanks
    76
    Thanked 29 Times in 29 Posts
    My plan is when they click on a link product link, I want to show a heading underneath say "PET HOUSING" with a brief description of bird houses in general, maybe going into a little detail about the timber used etc. Then I want to display all the product images relating to that product, and that's it. When they click another product, it changes the content to that product.

    I'm currently having issues with my query.

    This is my set up:
    PHP Code:
    <?php 

    if (isset($_GET['page']) && $_GET['page'] == "products") {

       if (isset(
    $_GET['order'])){
          
          switch (
    $_GET['order']) {
             case 
    'Bench':
                require(
    "core/get_products.php");
             break;
             
             case 
    'Bin Stores':
             
             break;
          }
          
       }
       else{
          echo 
    "display all products";
       }
    }




    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>
    At the very top of this file I include a script init.inc.php which determines which content to generate (for the separate pages of the site). When I click on the product link, it generates a file called product_template.htm, which is here:

    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=Bench">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=Fence"><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=Table"><li>Tables</a></li>
    		</ul>
    	</div>
    	
    	
    	
    </div>
    This is loaded in as a template. As you can see, I click on a product link which passes 2 values back to index.php. At the very top of index.php, I also have this bit of code (to determine if there were 2 variables passed in):

    PHP Code:
    if (isset($_GET['page']) && $_GET['page'] == "products") {

       if (isset(
    $_GET['order'])){
          
          switch (
    $_GET['order']) {
             case 
    'Bench':
                require(
    "core/get_products.php");
             break;
             
             case 
    'Bin Stores':
             
             break;
          }
          
       }
       else{
          echo 
    "display all products";
       }

    If/else and Switch working fine. As you can see from the switch, I require another file (get_products.php) which is where I am going to query the database. Here is that file:
    PHP Code:
    <?php

    //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.");
    $query mysql_query("SELECT P.*, I.imgName FROM products AS P, product_images AS I WHERE P.productID = I.productID AND P.product_name = {$_GET['order']}") or die("error selecting records");

    echo 
    "<pre>";
    print_r(mysql_fetch_array($query));
    echo 
    "</pre>";

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

    $full_dir dirname(__FILE__);
    $dir_files scandir($full_dir);
    $image_directory "core/".$dir_files[6]."/";

    while (
    $row mysql_fetch_array($query)) {
          
          
    $product_array[$count] = array(
                           
    "productID" => $row['productID'],
                           
    "productName" => $row['product_name'],
                           
    "productDetails" => $row['product_details'],
                           
    "productPrice" => "&pound;".$row['product_price']
                        );
          
    $count++;
    }

    ?>
    Ignore anything after the query, that needs to be changed a little I think and also my code doesn't reach that far!

    The query line keeps executing the die() part. Not sure what is wrong with it. I know that P.product_name needs to match the value which we pass in so it knows which images to select. I have echo'd out the $_GET['order'] value inside my get_products.php page so it can't be that.

    I'm really stumped, would appreciate if you could help me further.

    Kind regards,

    LC.

  5. #35
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    925
    Thanks
    76
    Thanked 29 Times in 29 Posts
    Ok, if I hardcode say 'Bench' into the query, it works great. When I use the $_GET['order'] or when I save that value to a variable and try to use that variable in the query, it executes the die() part....:s

    EDIT: It was my syntax for the variable should have been: '{$product}' instead of just {$product} on it's own.

    I've just spotted a potential issue. All the product prices are the same. How can I deal with this? Can Iput the product_price column in the product_images table and give each product it's own price value?

    Kind regards,

    LC.
    Last edited by LearningCoder; 11-09-2012 at 03:22 PM.

  6. #36
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,650 Times in 4,612 Posts
    Again, yes. There is nothing that ssys WHERE you MUST put information.

    Put it where it makes the most sense.

    But I say again: If you will ever have a single product_details that has multiple images, then you need to reorganize the DB again...this time with 3 tables.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  7. #37
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    925
    Thanks
    76
    Thanked 29 Times in 29 Posts
    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 10:19 PM.

  8. #38
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,650 Times in 4,612 Posts
    You *MUST* put an ORDER BY on your SELECT query.

    And simplify your code! And add in debug capability!

    Code:
    $sql = "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 ";
    if ( $product != "" ) { $sql .= " AND p.product_name = '{$product}'"; }
    $sql .= " ORDER BY P.product_name";
    
    // comment out debug after it starts working
    echo "<hr/>DEBUG SQL: " . $sql . "<hr/>\n";
    
    $query = mysql_query( $sql ) or die("error selecting records: " . mysql_error() );
    Remember, I don't use PHP. But then follow with something like this:
    Code:
    $priorProduct = "";
    while ($row = mysql_fetch_array($query)) 
    {
        $productName = $row["product_name"];
        if ( $priorProduct != $productName )
        {
            echo "<h3>Product: $productName </h3>\n";
            echo $row["productDetails"] . "<br/>\n";
            
            $priorProduct = $productName;
        }
        ... output product_price with the <img> ...
    }
    See it? You only output the product name and details ONCE when the value changes as you go through the loop.

    I don't know why you are dumping all the stuff into an array instead of just writing it as you go. But if you still want to do that, then you can either put blank values in for product name and details except for first row of each product or you can use the logic I show above as you pull the data out of the array.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  9. #39
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    925
    Thanks
    76
    Thanked 29 Times in 29 Posts
    Your way seems a lot better. I'm doing unnecessary work by saving it to the array.

    I will have a play about with the code now and see what I can do.

    EDIT: Awesome, got it working!

    Kind regards,

    LC.
    Last edited by LearningCoder; 11-10-2012 at 01:41 AM.

  10. #40
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,650 Times in 4,612 Posts
    Usually, simpler is going to be faster, anyway. So if you don't need the array, don't use it. Good luck.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  11. #41
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    925
    Thanks
    76
    Thanked 29 Times in 29 Posts
    I knew at the time I didn't need it so I'm not really sure why I carried on with it.

    Thank you for all your support!

    Kind regards,

    LC.


 
Page 3 of 3 FirstFirst 123

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •