Jump to content

Recommended Posts

I'm struggling to get any output from 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,

    );
}
?>

 

I am trying to get the following:

 

Cat1

    Product1

    Product2

Cat2

    Product3

    Product4

 

etc etc.

 

Any help would be great... Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/257872-nexted-loop-help-newbie-help/
Share on other sites

Try a print_r($list) after your while loop. Anything?

 

OK so it is getting data:

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] => ) ) )

 

 

Can anybody help getting this

 

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] => ) ) )

 

to output like this:

 

Cat1

    Product1

    Product2

Cat2

    Product3

    Product4

 

I've

You will have to loop through the categories and products. Something like the code below.

 

foreach ($list as $catID => $products) {
  print($catID);
  foreach ($products as $prodID => $data) {
    print($prodID . ': ' . $data['SKU'] . ': ' . $data['Description']);
  }
}

 

I didn't put any formatting in there, you should be able to handle that.

You will have to loop through the categories and products. Something like the code below.

 

foreach ($list as $catID => $products) {
  print($catID);
  foreach ($products as $prodID => $data) {
    print($prodID . ': ' . $data['SKU'] . ': ' . $data['Description']);
  }
}

 

I didn't put any formatting in there, you should be able to handle that.

 

Thanks very much for all your help.... hope I can return the good some day.

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.