rishiraj Posted September 4, 2007 Share Posted September 4, 2007 My table structure is like ----------------------------------------- |category | Products | |---------------------------------------- |category1 | product1 | |category1 | product2 | |category1 | product3 | |category1 | product4 | |category1 | product5 | |category1 | product6 | |category2 | product7 | |category2 | product8 | |category2 | product9 | |category2 | product10 | |category3 | product11 | |category3 | product12 | |category3 | product13 | |--------------------------------------- I want to select all products from table and arrange them in categories. I am using two dimensional array and then foreach loop to do it but not able to suceed. Please help with logic and code. I want to print my products like this. Category1 : Product1 Product2 Product3 Product4 Product5 Product6 Category2: Product7 Product8 Product9 Category3: Product10 Product11 Product12 Product13 Quote Link to comment https://forums.phpfreaks.com/topic/67909-solved-help-with-arrays/ Share on other sites More sharing options...
Jessica Posted September 4, 2007 Share Posted September 4, 2007 Well, post your code. Quote Link to comment https://forums.phpfreaks.com/topic/67909-solved-help-with-arrays/#findComment-341324 Share on other sites More sharing options...
rishiraj Posted September 4, 2007 Author Share Posted September 4, 2007 I have made a mess with code, I am not able to make the logic for it, anyways here is the code // query code $m=-1; $n=0; $tempurl="xxxxx"; while($row = mysql_fetch_array($result)) { if($tempurl!=$row['url']) { $m++; $n=0; } $url[$m] = $row['url']; $tempurl=$url[$m]; $keyword[$m][$n] =$row['keywords']; $n++; } // printing code $o=0; foreach($url as $url2) { echo "<h3>Keywords for url ".$url2."</h3>"; foreach($keyword[$o] as $keywordd) { echo $o." ".$keywordd."\t"; } $o++; } as you can see from the code i m newbie, so please help with me logic Quote Link to comment https://forums.phpfreaks.com/topic/67909-solved-help-with-arrays/#findComment-341326 Share on other sites More sharing options...
Jessica Posted September 4, 2007 Share Posted September 4, 2007 Where is your SQL? What I would do is select them all out ordered by first the category and then the product. SELECT * FROM table ORDER BY category, products Then do your loop something like this: (pseudo code, make sure to edit it to work with your code. <?php $lastCat = ''; while(//there are more rows){ $cat = $row['category']; if($cat != $lastCat){ // it's a new category print $cat; } $lastCat = $cat; print $row['product']; } ?> You really should have a seperate categories table and just use the ID to reference what category it is in the products table. Quote Link to comment https://forums.phpfreaks.com/topic/67909-solved-help-with-arrays/#findComment-341327 Share on other sites More sharing options...
cooldude832 Posted September 4, 2007 Share Posted September 4, 2007 Lets try this code <?php $q = "select category, Products From `Table` Orderby category"; $r = mysql_query($q) or die(mysql_error()); while($row = mysql_fetch_array($r)){ if($temp_cat != $row['category'] || !ISSET($temp_cat)){ echo "<br/><h1>".$row['category']."</h1>"; } echo $row['Products']."<br/>"; $temp_cat = $row['category']; } ?> It uses recursive logic to test if the next row pulled has the same category as the previous, if not it adds it. Quote Link to comment https://forums.phpfreaks.com/topic/67909-solved-help-with-arrays/#findComment-341328 Share on other sites More sharing options...
sspoke Posted September 4, 2007 Share Posted September 4, 2007 dunno i never did php before but i think it would be something like this $m = 0; while($row = mysql_fetch_array($result)) { $cata = $row['catagory']; if($cata == 'category1') { $cataproduct[$m][0] =$row['product']; } else if($cata == 'category2') { $cataproduct[$m][1] =$row['product']; } else if($cata == 'category3') { $cataproduct[$m][2] =$row['product']; } $m++; } dunno Quote Link to comment https://forums.phpfreaks.com/topic/67909-solved-help-with-arrays/#findComment-341330 Share on other sites More sharing options...
Barand Posted September 4, 2007 Share Posted September 4, 2007 <BS> It uses recursive logic to test if the next row pulled has the same category as the previous, if not it adds it. </BS> No it doesn't, so why try to confuse the guy with terms you don't understand. Quote Link to comment https://forums.phpfreaks.com/topic/67909-solved-help-with-arrays/#findComment-341432 Share on other sites More sharing options...
Guardian-Mage Posted September 4, 2007 Share Posted September 4, 2007 Never mind, mine doesn't work Quote Link to comment https://forums.phpfreaks.com/topic/67909-solved-help-with-arrays/#findComment-341438 Share on other sites More sharing options...
cooldude832 Posted September 4, 2007 Share Posted September 4, 2007 yeah it does use recursive logic braand you are out to get me and I don't appreciate it, just because you have no life and thousands of post doesn't mean you can take an attitude of superiority over people Logic is defined by the if statement, the recursive part is the test against the previous row to the current row, recursive isn't defined by a depth, only that it test something to something of similar value that has already been established. Quote Link to comment https://forums.phpfreaks.com/topic/67909-solved-help-with-arrays/#findComment-341635 Share on other sites More sharing options...
Barand Posted September 4, 2007 Share Posted September 4, 2007 Yawn. Does the bullshit never end? Quote Link to comment https://forums.phpfreaks.com/topic/67909-solved-help-with-arrays/#findComment-341643 Share on other sites More sharing options...
teng84 Posted September 4, 2007 Share Posted September 4, 2007 damn I saw this argument again !!! i hate this I'm not gonna judge the both of you but please !!!! this is no good CHEERS Quote Link to comment https://forums.phpfreaks.com/topic/67909-solved-help-with-arrays/#findComment-341656 Share on other sites More sharing options...
teng84 Posted September 5, 2007 Share Posted September 5, 2007 Where is your SQL? What I would do is select them all out ordered by first the category and then the product. SELECT * FROM table ORDER BY category, products Then do your loop something like this: (pseudo code, make sure to edit it to work with your code. <?php $lastCat = ''; while(//there are more rows){ $cat = $row['category']; if($cat != $lastCat){ // it's a new category print $cat; } $lastCat = $cat; print $row['product']; } ?> You really should have a seperate categories table and just use the ID to reference what category it is in the products table. im with jesirose you cant have that in multidimensional array in single query or if you really want then your gonna have additional loop and query which is not good I guess this is solved!!!! Quote Link to comment https://forums.phpfreaks.com/topic/67909-solved-help-with-arrays/#findComment-341698 Share on other sites More sharing options...
rishiraj Posted September 5, 2007 Author Share Posted September 5, 2007 Thanks a lot everyone, I thinks everyone's code is similar to the code posted by jesirose, and I am able to understand it. I am not sure wheather its recursive or not but it worked form me. Thanks a lot friends. <?php $lastCat = ''; while(//there are more rows){ $cat = $row['category']; if($cat != $lastCat){ // it's a new category print $cat; } $lastCat = $cat; print $row['product']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/67909-solved-help-with-arrays/#findComment-342009 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.