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

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 01-07-2013, 05:50 PM   PM User | #1
rgEffects
New Coder

 
Join Date: Aug 2012
Posts: 76
Thanks: 22
Thanked 0 Times in 0 Posts
rgEffects is an unknown quantity at this point
Three Table query sort

I have 3 simple tables linking information. Lets call them product, category and assignment. These tables tie information together. For example a set of data like this:

product
id, name
1, small toy
2, middle sized toy
3, big toy

category
id, class
1, boy
2, girl

assignment
id, productID, ClassID
1, 1, 1
2, 2, 2
3, 2, 1
4, 3, 2

The result here is that the big toy is for girls, the middle sized toy is for a boy or a girl, and the small toy is for a boy.

No I can't figure out how to generate a list that doesn't end up with duplicates. I'm getting this:
  • boy
    • small toy
  • boy
    • middle sized toy
  • girl
    • big toy
  • girl
    • middle sized toy


What I want is this:
  • boy
    • small toy
    • middle sized toy
  • girl
    • big toy
    • middle sized toy

Here's the code:
PHP Code:

<?php
$sql 
"SELECT *
FROM product, category, assignment
WHERE class.id = assignment.classID
AND product.id = assignment.productID
ORDER BY category.id
"

     
$result mysql_query($sql);
     echo 
'<ul>';
    if (
mysql_num_rows($result) > 0) {
        while (
$rec mysql_fetch_assoc($result)) {
            echo 
'<li />' $rec['class'], ' <ul><li />'$rec['name'],
                        
'<ul>';
        }
    }
    echo 
'</ul>';
}
?>
I've been looking for a way to keep the category from repeating that still allows the product to repeat. Any ideas would be appreciated.

Last edited by rgEffects; 01-08-2013 at 06:50 PM.. Reason: Problem Solved
rgEffects is offline   Reply With Quote
Old 01-08-2013, 02:29 PM   PM User | #2
Ctechinfo
New Coder

 
Join Date: Sep 2012
Posts: 88
Thanks: 3
Thanked 3 Times in 3 Posts
Ctechinfo is an unknown quantity at this point
bear in mind I am a MySQL newb.. But I believe you have to joing tables to do the search as you are trying to do..

http://dev.mysql.com/doc/refman/5.0/en/join.html
Ctechinfo is offline   Reply With Quote
Old 01-08-2013, 03:37 PM   PM User | #3
TFlan
New Coder

 
Join Date: Dec 2012
Location: USA
Posts: 82
Thanks: 3
Thanked 17 Times in 17 Posts
TFlan is an unknown quantity at this point
I haven't tested this, but I hope, if it doesn't work canned, that you get the idea

PHP Code:
<?php
$sql 
"SELECT * FROM product, category, assignment WHERE class.id = assignment.classID AND product.id = assignment.productID ORDER BY category.id"
$result mysql_query($sql);
if(
is_resource($result)){
    
$results = array();
    while(
$rec mysql_fetch_assoc($result)) {
        if(
array_key_exists($rec['class'], $results)){
            
array_push($results[$rec['class']], $rec['name']);
        }else{
            
$results[$rec['class']] = array($rec['name']);
        }
    }
    echo 
'<ul>';
    foreach(
$results as $class => $names){
        echo 
'<li>'.$class.'<ul>';
        foreach(
$names as $name){
            echo 
'<li>'.$name.'</li>';
        }
        echo 
'</ul></li>';
    }
    echo 
'</ul>';
}
?>

Last edited by TFlan; 01-08-2013 at 07:01 PM..
TFlan is offline   Reply With Quote
Users who have thanked TFlan for this post:
rgEffects (01-08-2013)
Old 01-08-2013, 04:09 PM   PM User | #4
hinch
Regular Coder

 
hinch's Avatar
 
Join Date: Sep 2005
Location: UK
Posts: 921
Thanks: 25
Thanked 79 Times in 79 Posts
hinch is on a distinguished road
SELECT * FROM category OUTER JOIN assignment ON category.id=assignment.ClassID OUTER JOIN product ON assignment.productID=product.ID GROUP BY category.id

something like that i don't really ahve time to run up a test
__________________
A programmer is just a tool which converts caffeine into code

My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
My hobby: http://www.angel-computers.co.uk
My life: http://www.furious-angels.com
hinch is offline   Reply With Quote
Old 01-08-2013, 06:49 PM   PM User | #5
rgEffects
New Coder

 
Join Date: Aug 2012
Posts: 76
Thanks: 22
Thanked 0 Times in 0 Posts
rgEffects is an unknown quantity at this point
Except for three missing ';' TFlan got it. Thanks very much. I was missing the second part of the loop...
rgEffects is offline   Reply With Quote
Old 01-08-2013, 07:02 PM   PM User | #6
TFlan
New Coder

 
Join Date: Dec 2012
Location: USA
Posts: 82
Thanks: 3
Thanked 17 Times in 17 Posts
TFlan is an unknown quantity at this point
Apologies, wrote that on the fly.. didn't see those. Fixed my post.

Happy to help
TFlan is offline   Reply With Quote
Users who have thanked TFlan for this post:
rgEffects (01-08-2013)
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 07:28 PM.


Advertisement
Log in to turn off these ads.