svgmx5 Posted June 30, 2010 Share Posted June 30, 2010 This is probably a simple question yet i can't find the answer to it. I have a database that is filled with items and each item has a category field ..so its the item id, item name and item category. Now i want to be able to display all those items, but arrange them by category with out having to call various SQL queries. i'm trying to make it look something like this: CATEGORY item, item, item, item CATEGORY item, item, item, item and so on... Quote Link to comment https://forums.phpfreaks.com/topic/206248-looping-through-a-database-to-get-results/ Share on other sites More sharing options...
kenrbnsn Posted June 30, 2010 Share Posted June 30, 2010 Here's one way (off the top of my head) <?php $q = "select item_id, item, category from your_table order by category"; $rs = mysql_query($q); $tmp = array(); while ($rw = mysql_fetch_assoc($rs)) { if (!array_key_exists($rw['category'],$tmp) { $tmp[$rw['category']] = array(); } $tmp[$rw['category']][] = $rw['item']; } foreach ($tmp as $cat => $items) { echo $cat . "<br><br>" . implode(',',$items) . "<br><br>\n"; } ?> Note: completely untested & no error checking coded. Ken Quote Link to comment https://forums.phpfreaks.com/topic/206248-looping-through-a-database-to-get-results/#findComment-1079015 Share on other sites More sharing options...
svgmx5 Posted June 30, 2010 Author Share Posted June 30, 2010 damn that worked...i just had to close a parenthesis tag. Thanks for that!! Quote Link to comment https://forums.phpfreaks.com/topic/206248-looping-through-a-database-to-get-results/#findComment-1079019 Share on other sites More sharing options...
svgmx5 Posted June 30, 2010 Author Share Posted June 30, 2010 okay so i have another issue.... while thats working i'm trying to display all the items in a list enviornment. howoever i'm having problems understanding how i can. i've tried using explode instead of implode, but it just returns an array. This is the code i have right now <?php $get_cats = "SELECT * FROM services ORDER BY industry"; $run_get = mysql_query($get_cats) or die(mysql_error()); $tmp = array(); while($rw = mysql_fetch_assoc($run_get)){ if (!array_key_exists($rw['industry'],$tmp)) { $tmp[$rw['industry']] = array(); } $tmp[$rw['industry']][] = $rw['category']; } foreach ($tmp as $cat => $items) { echo' <div class="resume_category_selection"> <h3>'.$cat.'</h3> <table width="750" cellspacing="0" cellpadding="0" border="0"> <tr> <td> <ul> <li>'. implode(',',$items) .'</li> </ul> </td> </tr> </table> </div> '; } ?> with this all the elements are displayed in a single <li> tag Like i said i've tried exploding it instead but i still can't get it to work Quote Link to comment https://forums.phpfreaks.com/topic/206248-looping-through-a-database-to-get-results/#findComment-1079037 Share on other sites More sharing options...
kenrbnsn Posted June 30, 2010 Share Posted June 30, 2010 Change: <li>'. implode(',',$items) .'</li> to <li>'. implode('</li><li>',$items) .'</li> Ken Quote Link to comment https://forums.phpfreaks.com/topic/206248-looping-through-a-database-to-get-results/#findComment-1079038 Share on other sites More sharing options...
svgmx5 Posted June 30, 2010 Author Share Posted June 30, 2010 i noticed i forgot to put the code of explode foreach ($tmp as $cat => $items) { $services = explode(' ', $items); echo' <div class="resume_category_selection"> <h3>'.$cat.'</h3> <table width="750" cellspacing="0" cellpadding="0" border="0"> <tr> <td> <ul> <li>'.$services.'</li> </ul> </td> </tr> </table> </div> '; } Quote Link to comment https://forums.phpfreaks.com/topic/206248-looping-through-a-database-to-get-results/#findComment-1079039 Share on other sites More sharing options...
svgmx5 Posted June 30, 2010 Author Share Posted June 30, 2010 oops i didn't noticed that you had replied already.. well that once again did the job Quote Link to comment https://forums.phpfreaks.com/topic/206248-looping-through-a-database-to-get-results/#findComment-1079040 Share on other sites More sharing options...
travo1992 Posted June 30, 2010 Share Posted June 30, 2010 nevermind this post, you guys fixed it between when I stated posting and when I finished Quote Link to comment https://forums.phpfreaks.com/topic/206248-looping-through-a-database-to-get-results/#findComment-1079041 Share on other sites More sharing options...
svgmx5 Posted June 30, 2010 Author Share Posted June 30, 2010 okay you guys...i just have one more problem that i need help with...i'm trying to put them all in a form..which i realized i should f stated from the beginning, but that wasn't my main plan...so, sorry about all this extra steps, i truly do appreciate the help.. anyway like i was saying i'm trying to make checkboxes with the name next to them. I've been playing with it and i'm almost there, but not there yet, i'm doing something wrong but i can't find it anyway this should be the last one! foreach ($tmp as $cat => $items) { echo' <div class="resume_category_selection"> <h3>'.$cat.'</h3> <table width="750" cellspacing="0" cellpadding="0" border="0"> <tr> <td> <ul> <li><input type="checkbox" name="service" value="'.implode('"/>',$items).'"/>'. implode('</li><li><input type="checkbox" name="service" value="',$items).'</li> </ul> </td> </tr> </table> </div> '; } Quote Link to comment https://forums.phpfreaks.com/topic/206248-looping-through-a-database-to-get-results/#findComment-1079053 Share on other sites More sharing options...
kenrbnsn Posted June 30, 2010 Share Posted June 30, 2010 This one is slightly harder. You can't use a plain implode to do it, you have to use a foreach loop. Also, for checkboxes, all the "names" can't be the same unless you're using an array as the name: <?php echo' <div class="resume_category_selection"> <h3>'.$cat.'</h3> <table width="750" cellspacing="0" cellpadding="0" border="0"> <tr> <td> <ul> <li>'; foreach ($items as $item) { echo '<input type="checkbox" name="service[]" value="'. $item .'"/>' . $item .'<br />'; } echo '</li> </ul> </td> </tr> </table> </div> '; } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/206248-looping-through-a-database-to-get-results/#findComment-1079059 Share on other sites More sharing options...
svgmx5 Posted June 30, 2010 Author Share Posted June 30, 2010 Thanks, Well that does it then i got it working Thanks allot to everyone! I'm actually adding a number to the name so it would be like service_1 service_2 so on... Quote Link to comment https://forums.phpfreaks.com/topic/206248-looping-through-a-database-to-get-results/#findComment-1079060 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.