Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Very new to PHP so please be patient.

I've got an SQL query that pulls products from a table and I need to display them in categories:

category 1
product 1
product 2
product 3
category 2
product 1
product 2
product 3

etc...

I've followed a few tutorials/posts but cant get the data displaying.

Thanks for any help.

So Far I have this:

<?PHP
$sql = "
    SELECT DISTINCT
        JVPCX.`category_id`,
        JVP.`product_id`,
        JVP.`product_sku`,
        JVP.`product_s_desc`
    FROM
        `jos_vm_product_category_xref` JVPCX
    INNER JOIN `jos_vm_product` JVP
        ON JVPCX.`product_id` = JVP.`product_id`
";
	
$res = mysql_query($sql);
$list = array();
while ( $r = mysql_fetch_object( $res ) )
{
    if ( ! isset( $list[ $r->category_id ] ) )
    {
        $list[ $r->category_id ] = array();
    }

    $list[ $r->category_id ][ $r->product_id ] = array(
        'SKU'     => $r->product_sku,
        'Description' => $r->product_s_desc,
		
	
    );
}
print_r($list);
var_dump($r);
?>


The output is:

Array ( [5] => Array ( [1] => Array ( [SKU] => EASY819-AC-RC [Description] => ) [3] => Array ( [SKU] => EASY819-DC-RC [Description] => ) [2] => Array ( [SKU] => EASY819-AC-RCX [Description] => ) ) [6] => Array ( [4] => Array ( [SKU] => EASY719-AB-RC [Description] => ) [5] => Array ( [SKU] => EASY719-AB-RCX [Description] => ) ) )

bool(false)

As I stated above I am trying to achieve:

category 1
product 1
product 2
product 3
category 2
product 1
product 2
product 3

Thanks for the advice.
Posted
Updated 27-Feb-12 6:56am
v2
Comments
Sergey Alexandrovich Kryukov 27-Feb-12 12:49pm    
The problem is not that you are a novice. The problem is that you don't ask a question. You did not even explain how the result should look, exactly. When you enter the question text, you can use HTML.

You should write the code, not try to find anything in manuals. Write it and ask a question if you face any problems. Use "Improve question" above.
--SA
wizardzz 27-Feb-12 15:39pm    
I think you might want to use Group By?
Paul Hayman 27-Feb-12 15:43pm    
Thanks for the help, but I don't think group by is the answer. Doesn't this group the rows into one? I need separate lines with products not a summary.
wizardzz 27-Feb-12 15:55pm    
Nevermind, my bad.
Paul Hayman 27-Feb-12 16:00pm    
Hmm yeah I see your point. I thought it would be fairly straightforward to get the data into and array which I think i've done and then do a kind nested loop but I'm very new to PHP....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900