Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Coding Forum


You are currently viewing our Coding Forum as a guest. Please register to participate.
Login



Reply
Simple Problem Here...
Old 12-28-2005, 10:20 PM Simple Problem Here...
Brian07002's Avatar
Defies a Status

Posts: 2,140
Name: ...
Location: ...
Trades: 0
Hi,

I am trying to make the following script work a little bit differently. The code below will output a picture from mysql db. It does work, but...I need to be able to access the 'catalog' table as well as the 'products' table in mysql db. How can I accomplish this? The 'products' table is currently being used in the following script, but I need to also be able to access a row in the 'catalog' table in the script below. How can this be done?

P.s: Today IS MY BIRTHDAY

Here's the code:

Code:
<?

// Connect to the database
mysql_connect ('localhost', 'user', 'pass') ;
mysql_select_db ('db_name'); 

// Edit this number to however many links you want displaying
$num_displayed = 1 ;
$category = '165' ;

// Select random rows from the database
$result = mysql_query ("SELECT * FROM products WHERE cid=$category ORDER BY RAND() LIMIT $num_displayed"); 

// For all the rows that you selected
while ($row = mysql_fetch_array($result)) 

{
// Display them to the screen...

echo "<a href=\"http://www.sample.com/index.php?p=product&id=" . $row["pid"] ."&parent=" . $row["cid"] . "\"> <img src=\"displaythumb.php?image=" . $row["image_url"] . "\" border=1 alt=\"" . $row["title"] . "\">
<br>
$row[title]
</a>" ;
}

?>
Thanks again for your help!
-Brian
__________________
Made2Own

Please login or register to view this content. Registration is FREE

Last edited by Brian07002; 12-28-2005 at 11:54 PM..
Brian07002 is online now
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-29-2005, 06:03 AM
someguy's Avatar
Extreme Talker

Posts: 151
Location: UK
Trades: 0
Happy Birthday Brian.

What are you trying to do with the catalog table? Is there a relation between the two tables?
someguy is offline
Reply With Quote
View Public Profile
 
Old 12-29-2005, 06:24 AM
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
you will need to use a "LEFT JOIN" but without knowing the structures and PK/FK details it's difficult to be more specific.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-29-2005, 11:44 AM
Brian07002's Avatar
Defies a Status

Posts: 2,140
Name: ...
Location: ...
Trades: 0
Hi,

Thank you both for both replies... (Chrishirst & Someguy)

Let me first reply to Someguy since I can add more to his post...

Code:
Happy Birthday Brian
Thank you!

Code:
What are you trying to do with the catalog table? 
Is there a relation between the two tables?
Yes, there is a relation between the two, and that is they are in the same database. What I am trying to do with the catalog table is, I am trying use a row from that table called 'cid' from the catalog table in the same script that originally only uses the products table. Here's a diagram that I hope will explain a little better. ( PNG Format )

Thanks you for your time!
-Brian
Attached Images
File Type: png diagram.PNG (28.6 KB, 9 views)
__________________
Made2Own

Please login or register to view this content. Registration is FREE
Brian07002 is online now
Reply With Quote
View Public Profile
 
Old 12-29-2005, 12:59 PM
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
something like this (just guessing the catalog fieldnames)
You are only using 4 fields from the products table and I have no idea what fields are required from catalog so
Code:
"SELECT pid,cid,title,image_url,catalog.fieldname FROM products LEFT JOIN catalog ON products.cid = catalog.id WHERE products.cid=$category ORDER BY RAND() LIMIT $num_displayed;"
add in more catalog.fieldname if required and I assumed that products.cid would be PK and catalog.id will be FK

BTW

PK = Primary Key
FK = Foreign Key

( Not tested 'cos I couldn't be bothered to recreate the db structure )
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-29-2005, 03:05 PM
Brian07002's Avatar
Defies a Status

Posts: 2,140
Name: ...
Location: ...
Trades: 0
Hi Chrishirst,

I've tried what you supplied me with but I couldn't get it working so, I've done some checking in my database at the structure, and this is what I can tell you.

All I need to do is SELECT a ROW (fieldname) from two different tables and use them both in the following script. One ROW (fieldname) from the catalog table is called: cid and one from ROW from the products table is called: pid

Here's the original script again:

Code:
<?

// Connect to the database
mysql_connect ('localhost', 'user', 'pass') ;
mysql_select_db ('db_name'); 

// Edit this number to however many links you want displaying
$num_displayed = 1 ;
$category = '165' ;

// Select random rows from the database
$result = mysql_query ("SELECT * FROM products WHERE cid=$category ORDER BY RAND() LIMIT $num_displayed"); 

// For all the rows that you selected
while ($row = mysql_fetch_array($result)) 

{
// Display them to the screen...

echo "<a href=\"http://www.sample.com/index.php?p=product&id=" . $row["pid"] ."&parent=" . $row["cid"] . "\"> <img src=\"displaythumb.php?image=" . $row["image_url"] . "\" border=1 alt=\"" . $row["title"] . "\">
<br>
$row[title]
</a>" ;
}

?>
One thing I forget to mention was that in the above SELECT statement from where the WHERE statement begins, I need that as is so that it reads cid=$category, but it doesn't seem to work as is because the cid ROW (fieldname) doesn't exist in the products table as the script currently states. The cid ROW (fieldname) exists in the catalog table NOT the products table. But I DO need the products table to get the actual image, and the catalog table to get the category in which that product exists in.

The url part of the script (at the very bottom of the above code) needs to be changed so that the part that says: . $row["pid"] is from the products table & . $row["cid"] is from the catalog table.

Hope you understand this time.
Thanks again!
-Brian
__________________
Made2Own

Please login or register to view this content. Registration is FREE

Last edited by Brian07002; 12-29-2005 at 04:08 PM..
Brian07002 is online now
Reply With Quote
View Public Profile
 
Old 12-29-2005, 06:02 PM
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Ok we are a bit adrift on terminology due to the way the script is coded.
a row in SQL terminology is the entire record (all fields), what you need to retrieve is one field from the row (known as a column). It's the much loved use of $row["column"] with php programmers that throws it into confusion.

so you need the products.pid column and the category.cid column along with products.title and products.image_url

to be able to accomplish this there needs to be a column that exists in the products table that can be used as a relational key to the category table. This is the PK/FK thing.
In a well designed normalised table structure for a product catalog system this should exist otherwise you would not be able to show your products in categorised groups.

BTW your output buffering is messed up and the gzip handler is throwing an error
Quote:
Warning: ob_start(): output handler 'ob_gzhandler' conflicts with 'zlib output compression' in [include file removed] on line 1
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-29-2005, 06:15 PM
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
and
HAPPY BIRTHDAY Brian
Attached Images
File Type: jpg Spongebob_Cake.jpg (68.4 KB, 4 views)
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-29-2005, 06:36 PM
Brian07002's Avatar
Defies a Status

Posts: 2,140
Name: ...
Location: ...
Trades: 0
Hi Chrishirst,

I am certain about what I am seeing, it's just how I am explaining it...Let me try to explain this better...

Ok, there's a product.pid column -- there's also a category.cid column ...You have it correct...But how do I connect these two together?

Edit: Chrishirst, Did you notice if that error with the Gzip Handler is still showing up?

Code:
EDIT: THANKS ALOT CHRISHIRST!
-Brian
__________________
Made2Own

Please login or register to view this content. Registration is FREE

Last edited by Brian07002; 12-29-2005 at 06:45 PM..
Brian07002 is online now
Reply With Quote
View Public Profile
 
Old 12-29-2005, 06:57 PM
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
there will be a column in the products table that tells the system which category the product is in. That's the one needed.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-29-2005, 07:05 PM
Brian07002's Avatar
Defies a Status

Posts: 2,140
Name: ...
Location: ...
Trades: 0
Hi Chirshirst,

That would be
Code:
pid
so the link would be products.pid correct?

-Brian
__________________
Made2Own

Please login or register to view this content. Registration is FREE
Brian07002 is online now
Reply With Quote
View Public Profile
 
Old 12-29-2005, 08:02 PM
Brian07002's Avatar
Defies a Status

Posts: 2,140
Name: ...
Location: ...
Trades: 0
Hi Chrishirst,

Let me ask you this...

How should the SELECT STATEMENT be defined? Could you just explain with the changeable information in brackets like so?

Code:
"SELECT [table] FROM [column] LEFT JOIN [column.id] 
ON products.pid =  WHERE cid=$category ORDER BY 
RAND() LIMIT $num_displayed;"
Thank you
-Brian
__________________
Made2Own

Please login or register to view this content. Registration is FREE
Brian07002 is online now
Reply With Quote
View Public Profile
 
Old 12-29-2005, 08:20 PM
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
sounds like that might be the product ID.

If you email your structures of the products and category tables I'll see if I can figure out what is what . forumusername at candsdesign.co.uk
It's getting near the wrong side of 1am here so the brain is fading somewhat
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-29-2005, 09:45 PM
Brian07002's Avatar
Defies a Status

Posts: 2,140
Name: ...
Location: ...
Trades: 0
I sent you png's and the actual exports...Dis-regard the png's I know they're no help, but I thought you might like the screens n-e-way.

-Brian
__________________
Made2Own

Please login or register to view this content. Registration is FREE
Brian07002 is online now
Reply With Quote
View Public Profile
 
Old 12-30-2005, 09:30 PM
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Code:
$sql = "SELECT pid,title,image_url,catalog.cid,catalog.name FROM products LEFT JOIN catalog ON products.cid = catalog.cid WHERE products.cid=$category ORDER BY RAND() LIMIT $num_displayed;"
This works,
You can add in other columns into the fieldlist from either table using the tablename.column syntax. If the column is from the primary table (products here) you can just use column provided the column name does not exist in both tables.
The display code is the same syntax ($row["columnname"])

BTW if you want to test in the phpadmin GUI just replace the variables with hardcoded values.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to Simple Problem Here...
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.60775 seconds with 13 queries