Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-09-2012, 12:09 AM   PM User | #31
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 848
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
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.
LearningCoder is offline   Reply With Quote
Old 11-09-2012, 03:36 AM   PM User | #32
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 848
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
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.
LearningCoder is offline   Reply With Quote
Old 11-09-2012, 05:20 AM   PM User | #33
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 11-09-2012, 11:11 AM   PM User | #34
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 848
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
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.
LearningCoder is offline   Reply With Quote
Old 11-09-2012, 01:26 PM   PM User | #35
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 848
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
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 02:22 PM..
LearningCoder is offline   Reply With Quote
Old 11-09-2012, 08:56 PM   PM User | #36
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 11-09-2012, 09:13 PM   PM User | #37
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 848
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
Old 11-09-2012, 11:13 PM   PM User | #38
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 11-09-2012, 11:54 PM   PM User | #39
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 848
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
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 12:41 AM..
LearningCoder is offline   Reply With Quote
Old 11-10-2012, 12:08 AM   PM User | #40
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 11-10-2012, 01:10 AM   PM User | #41
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 848
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
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.
LearningCoder is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:42 PM.


Advertisement
Log in to turn off these ads.