carbonxps Posted February 27, 2012 Share Posted February 27, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/257872-nexted-loop-help-newbie-help/ Share on other sites More sharing options...
batwimp Posted February 27, 2012 Share Posted February 27, 2012 Is it outputting anything? Quote Link to comment https://forums.phpfreaks.com/topic/257872-nexted-loop-help-newbie-help/#findComment-1321708 Share on other sites More sharing options...
carbonxps Posted February 27, 2012 Author Share Posted February 27, 2012 No nothing at all... I tried: 'SKU' => $r['product_sku'], 'Description' => $r['product_s_desc]', But it was giving a sytax error... Thanks so much for your help. I've been trying this all day... Is it outputting anything? Quote Link to comment https://forums.phpfreaks.com/topic/257872-nexted-loop-help-newbie-help/#findComment-1321720 Share on other sites More sharing options...
batwimp Posted February 27, 2012 Share Posted February 27, 2012 have you tried a var_dump($r) to make sure your database is being queried correctly? Quote Link to comment https://forums.phpfreaks.com/topic/257872-nexted-loop-help-newbie-help/#findComment-1321722 Share on other sites More sharing options...
KevinM1 Posted February 27, 2012 Share Posted February 27, 2012 What's the error message you're getting? Quote Link to comment https://forums.phpfreaks.com/topic/257872-nexted-loop-help-newbie-help/#findComment-1321726 Share on other sites More sharing options...
carbonxps Posted February 27, 2012 Author Share Posted February 27, 2012 OK so looks like it might not be getting data: have you tried a var_dump($r) to make sure your database is being queried correctly? bool(false) Wierd it works fine on the database directly. No Errors at all. Quote Link to comment https://forums.phpfreaks.com/topic/257872-nexted-loop-help-newbie-help/#findComment-1321727 Share on other sites More sharing options...
batwimp Posted February 27, 2012 Share Posted February 27, 2012 Try a print_r($list) after your while loop. Anything? Quote Link to comment https://forums.phpfreaks.com/topic/257872-nexted-loop-help-newbie-help/#findComment-1321730 Share on other sites More sharing options...
carbonxps Posted February 27, 2012 Author Share Posted February 27, 2012 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] => ) ) ) Quote Link to comment https://forums.phpfreaks.com/topic/257872-nexted-loop-help-newbie-help/#findComment-1321735 Share on other sites More sharing options...
carbonxps Posted February 27, 2012 Author Share Posted February 27, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/257872-nexted-loop-help-newbie-help/#findComment-1321761 Share on other sites More sharing options...
DavidAM Posted February 27, 2012 Share Posted February 27, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/257872-nexted-loop-help-newbie-help/#findComment-1321842 Share on other sites More sharing options...
carbonxps Posted February 28, 2012 Author Share Posted February 28, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/257872-nexted-loop-help-newbie-help/#findComment-1322001 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.