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?
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">
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");
//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.
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.
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?
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.
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:
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"); }
$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>
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?
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.