views:

335

answers:

1
<?php

// query db

    $sql = "SELECT category.id as category_pk, category_id, products.* from category, products WHERE products.id = category.category_id AND category.id = category_id"; // from category get id
    if ($result = mysql_query($sql)) {
      if (mysql_num_rows($result)) {
        $row = mysql_fetch_assoc($result);
        echo "<h2>" . $row['category_id'] . "</h2>";
     } else {
     echo "Something is wrong!";
      }
    }

?>
    <div class="datadivleftcontent">

<?php
// query db

    $sql = "SELECT product_image, products.* FROM product_image, products WHERE product_image.product_id = products.id AND product_image.product_id = products.id"; // from products get id
    if ($result = mysql_query($sql)) {
      if (mysql_num_rows($result)) {

     while ($row = mysql_fetch_assoc($result)) { //initial the loop

        echo "<h4>" . $row['name'] . "</h4>"; // echo date results as format "h4"
     echo "<p>" . $row['description'] . "</p>"; // echo content as p format "body text"
     echo "<h5>Measurements: " . $row['measurements'] . "</h5>"; // echo content as p format "body text"

echo "<a href='img/products/{$row['filename']}' rel='lightbox' title='{$row['name']}'>"; 

echo "<img src='img/products/thumbnails/{$row['filename']} 'alt='{$row['name']}' title='{$row['name']}' width='173' height='136'></a>";



}
     } else {
     echo "Something is wrong!";
      }
    }

?>

Hi,

Firstly many thanks for all your help and support it is very much appreciated. I am finding this quiet difficult trying to sort out, I am not so familiar with backend mysql,php programming but i am getting by thanks to your help.

OK this is what I’ve done so far:

The DB:
category:
id (pk, auto increment)
category_id (index)

products:
id (fk to category_id, cascade)
name (index)
description
measurements

product_image:
product_id (fk to name, cascade)
filename
thumbnail

Now, I have menu (list item) were you can select the type of category linking to product_range.php:

<?php

    // query db
    $sql = mysql_query ("SELECT * FROM `category` ORDER BY ID ASC");
    // echo linked result from db
   if($resource and !is_bool($resource)) {
    $row = mysql_fetch_assoc($resource);
 }
    while ($row = mysql_fetch_assoc($sql)){

    echo "<a href='product_range.php?id={$row['category_id']}' "; echo ($fullpath == "product_range.php?category_id={$row['category_id']}") ? 'id="select"' : ''; echo ">" . ($row['category_id']) . "</a>";
} 
?>

This is the code for the product_range.php

<?php include ("include/head.php"); ?>

<?php // db connect
$link = mysql_connect('localhost', 'root', 'password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo '';
mysql_select_db("wp");
?>

<body>

<?php include ("include/header.php"); ?>

<div class="wrapper">

<?php include ("include/topnav.php"); ?>

<!-- data div -->
<div class="datadiv">

<div class="datadivleft">

<?php

// query db
if (isset($_GET['id'])) {
    $sql = "SELECT category.id as category_pk, category_id, products.* from category, products WHERE products.id = category.category_id AND category.id = ?" . mysql_real_escape_string($_GET['id']) . "'"; // from category get id
    if ($result = mysql_query($sql)) {
      if (mysql_num_rows($result)) {
        $row = mysql_fetch_assoc($result);
        echo "<h2>" . $row['category_id'] . "</h2>";
     } else {
     echo "Something is wrong!";
      }
    }
  }
?>
    <div class="datadivleftcontent">

<?php
// query db
if (isset($_GET['id'])) {
    $sql = "SELECT products_image.* FROM products_image, products WHERE products_image.id = products.name AND products_image.id = ?" . mysql_real_escape_string($_GET['id']) . "'"; // from products get id
    if ($result = mysql_query($sql)) {
      if (mysql_num_rows($result)) {

     while ($row = mysql_fetch_assoc($result)) { //initial the loop

        echo "<h4>" . $row['name'] . "</h4>"; // echo date results as format "h4"
     echo "<p>" . $row['description'] . "</p>"; // echo content as p format "body text"
     echo "<h5>Measurements: " . $row['measurements'] . "</h5>"; // echo content as p format "body text"

echo "<a href='img/products/{$row['filename']}' rel='lightbox' title='{$row['name']}'>"; 

echo "<img src='img/products/thumbnails/{$row['filename']} 'alt='{$row['name']}' title='{$row['name']}' width='173' height='136' /></a>";


}
     } else {
     echo "Something is wrong!";
      }
    }
  }
?>


</div>


</div>

</div>
<!-- end -->

<?php include ("include/showcase.php"); ?>

<?php include ("include/btmcontent.php"); ?>

</div>

<?php include ("include/footer.php"); ?>

</body>
</html>

Now at present i cannot get any data echoed from the DB i,ve tried sourcing out the syntax as mention, I must be doin something wrong?

+1  A: 

EDIT:

Hi, Thanks for your reply, the * is a typo I missed it's not in the actual source. Also for picking up. Everything is working as it should, but here it is: I have a table named filename consisting filename_id (foreign key) image (containing the image.jpg) and thumbnail (containing the image.jpg) the filename_id relates to products filename (index). So the images relates to the products and category selected by the user. I get all the data with 1 image related (products filename field) echoed but do not get the images related from the filename table. Hope some of this makes some sense. – John 10 mins ago

Ok so fist you need to get products and categories:

SELECT category.id as category_pk, category_id, products.* from category, products WHERE products.id = category.category_id AND category.id = ?;

the "?" is the category id youve gotten/sanitized form your request.

This gets you all your products for a given category and all the related category info for each product.

Then for your images you are goin to issue have to issue another query since you have multiple images so foreach product you do:

SELECT filename.* FROM filename, products WHERE filename.filename_id = products.filename AND filename.filename_id = ?;

This i very DB heavy because your going to query for images for each product... Another way to do it would be to query for all images joined with the products and category and then us some php to get everythoing orgainzed into sructure you can use with your output functions for the html. That way you only do a single query.

Also if its not to late you may want to revise your schema... this is very confusing! I would have done something like the following:

category:
id (pk auto increment)
name (index mul or unique)

product:
id (pk, auto increment)
category_id (fk to category.id)
name (index multi)
description
measurements

product_image:
id (pk, auto increment)
product_id (fk to product)
image_file (image filename)
thumbnail_file (thumbnail filename)


your missing the ending quote on your src. you also need to close that image tag... and why do you have a * in the tag? (corrected below):

echo "<img src='img/products/thumbnails/{$row['filename']}' 'alt='{$row['name']}' title='{$row['name']}' width='156' height='109' /></a>";

Aside form that what is the eaxct problem you are having? can you not find the images? are you getting the wrong names/categories. etc.?

also this is kind of ugly in my opinion... you might want to make use of sprinf here like so:

  echo sprintf('<img src="img/production/thumbnails/%s" alt="%s" title="%s" width="156" height="109" /></a>',
    $row['filename'],
    $row['name'],
    $row['name']
  );
prodigitalson
Hi, Thanks for your reply, the * is a typo I missed it's not in the actual source. Also for picking up.Everything is working as it should, but here it is: I have a table named filename consisting filename_id (foreign key) image (containing the image.jpg) and thumbnail (containing the image.jpg) the filename_id relates to products filename (index). So the images relates to the products and category selected by the user.I get all the data with 1 image related (products filename field) echoed but do not get the images related from the filename table.Hope some of this makes some sense.
John
see my edits above.
prodigitalson
Hi,Firstly many thanks for all your help and support it is very much appreciated. I am finding this quiet difficult trying to sort out, I am not so familiar with backend mysql,php programming but i am getting by thanks to your help.OK eddited the top what I’ve done so far:
John