Jump to content

Nexted Loop Help Newbie Help....


carbonxps

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.