Jump to content

getting product list from two tables in the database.


Orionsbelter

Recommended Posts

Hi there i have a big issues which is starting to cause me lots of stress. I have two tables one which is products and contains the following below and the other which is additionalCategories which the contains is also below.

 

products

 

id

name

image1 

image2

image3

image4 

image5

description

department

category

subcategory 

Price

 

additonalCategories

 

        id 

departmentID 

categoryID 

subcategoryID

productID

 

i have made a products upload script to my site where i can add products too it saves the category and subcategory id's in the fields above then if i wish to add the product to additional categories i do so and again this adds all the data to the addiotnalCategories table.

 

however i can seems to be able to display this on the view products page i tried the below code but get no luck

 

<?php
session_start();
$username=$_SESSION['username'];
include_once"../includes/db_connect.php";
$cat=$_GET['cat'];
?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php 
$query("SELECT products.name AS name, products.price AS price, addtionalCategories.productID AS productID FROM products, addtionalCategories WHERE category='$cat' AND products.id = addtionalCategories.productID");
while($fetch=mysql_fetch_object($query)){
echo"$fetch->name";
}
?>
</body>
</html>

 

i need a way in which on the page it check the category id then finds all the products that are saved in the additionalCategories table then to find the data saved in the products table such as the name and price etc

 

please any help will be appreciated thank you for reading

 

Link to comment
Share on other sites

 

part of your code:

$query("SELECT products.name AS name, products.price AS price, addtionalCategories.productID AS productID FROM products, addtionalCategories WHERE category='$cat' AND products.id = addtionalCategories.productID");
while($fetch=mysql_fetch_object($query)){

 

read... and compare your code  to the examples

http://php.net/manual/en/function.mysql-query.php

Link to comment
Share on other sites

silly newbie mistake

 

<?php 
$query=mysql_query("SELECT products.name AS name, products.price AS price, addtionalCategories.productID AS productID FROM products, addtionalCategories WHERE category='1' AND products.id = addtionalCategories.productID");
while($fetch=mysql_fetch_object($query)){
echo"$fetch->name";
}
?>

 

however it still echos only the following id departmentID categoryID subcategoryID productID

Link to comment
Share on other sites

<body>
<?php 
$query("SELECT products.name AS name, products.price AS price, addtionalCategories.productID AS productID FROM products, addtionalCategories WHERE category='$cat' AND products.id = addtionalCategories.productID");
while($fetch=mysql_fetch_object($query)){
echo"$fetch->name";
}
?>

 

this is your full code? .... and what else have you tried?... what results are you expecting?

 

according to one of your posts:

however it still echos only the following id departmentID categoryID subcategoryID productID

 

I don't see anything like that in your code... only one echo"$fetch->name" ??

Link to comment
Share on other sites

 

 

my full code is

 

<?php
session_start();
$username=$_SESSION['username'];
include_once"../includes/db_connect.php";
$cat=$_GET['cat'];
?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php 
$query=mysql_query("SELECT products.name AS name, products.price AS price, addtionalCategories.productID AS productID FROM products, addtionalCategories WHERE products.category='1' AND products.id = addtionalCategories.productID");
while($fetch=mysql_fetch_object($query)){
echo"$fetch->name";
}
?>
</body>
</html>

 

i'm trying to display all my products by name from my mysql database however i two tables one which is products which contains the master category in a field called category and another table which is called additionalCategories which contains a field called productID which is the same as a record in the products table i use the id field in the products table and inset into additonalCategories to allow myself to add products to more than one categories however when i try join the too so i can display all products in the category from the products table and all products from additionalCategories.

 

however it isn't displaying anything. :(

 

 

Link to comment
Share on other sites

I did post ONLY your relevant code ... the one that is supposed to "display" your data.... and I still seeing only ONE echo there... if that piece of (incomplete) code is not displaying anything means that your query is not returning anything or is failing... or you are not connected to the DB... try this simple changes (very dirty... just to help you to start) :

 

$query = "SELECT products.name, 
                             products.price, 
                             addtionalCategories.productID 
                    FROM products, addtionalCategories 
                    WHERE products.category='1' 
                         AND products.id = addtionalCategories.productID";


$query = mysql_query($query) or die("error : " . mysql_error());
while( $fetch = mysql_fetch_object($query)){
echo "Product Name : "  . $fetch->name;
echo "<br />Product Price   : "  . $fetch->price;
       // etc... etc...etc...

}

 

see what you get from this

 

Link to comment
Share on other sites

if you replace this line:

$query = mysql_query($query) or die("error : " . mysql_error());

 

with this:

$query = mysql_query($query) or die("error : " . mysql_error());
$num_rows = mysql_num_rows($query);
echo "Rows Returned by the Query : " . $num_rows;

 

what do you get?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.