geudrik Posted October 7, 2008 Share Posted October 7, 2008 Well, heres my issue. I have a database with multiple rows. One of the columns is 'category'. What I need to do is pull all info out of the database and organize it by category. Where should I begin looking on how to get this done? I got as far as a sorty by argument in my sql, or maybe a for each l($row[category]) Thoughts? And it's not just sorting it by category, I need to physically pull chunks of the database out but only where they fall under each category. Link to comment https://forums.phpfreaks.com/topic/127447-sorting-by-category/ Share on other sites More sharing options...
DarkWater Posted October 7, 2008 Share Posted October 7, 2008 Can a piece of data be in multiple categories? Link to comment https://forums.phpfreaks.com/topic/127447-sorting-by-category/#findComment-659339 Share on other sites More sharing options...
aebstract Posted October 7, 2008 Share Posted October 7, 2008 So you need something like this? $myquery = mysql_query("SELECT * FROM tablenamehere WHERE category='cat2'") or DIE(mysql_error()); while($r=mysql_fetch_array($myquery)) { $id=$r["id"]; $value=$r["value"]; $value=$r["value"]; echo "$value"; } Quicky to get the idea to you, basically grab all information with a certain category. Then you just set variables for whatever information you want from each from. Hope this is what you were looking for. Link to comment https://forums.phpfreaks.com/topic/127447-sorting-by-category/#findComment-659342 Share on other sites More sharing options...
revraz Posted October 7, 2008 Share Posted October 7, 2008 You probably want to use the GROUP BY parameter in MySQL. Link to comment https://forums.phpfreaks.com/topic/127447-sorting-by-category/#findComment-659345 Share on other sites More sharing options...
F1Fan Posted October 7, 2008 Share Posted October 7, 2008 <?php $query = "SELECT name, description, category, upc FROM products ORDER BY category, name"; $result = mysql_query($query); $all = array(); while ($row = mysql_fetch_assoc($result)){ $all[$row['category']][$row['upc']] = $row; } print_r($all) ?> Link to comment https://forums.phpfreaks.com/topic/127447-sorting-by-category/#findComment-659346 Share on other sites More sharing options...
geudrik Posted October 7, 2008 Author Share Posted October 7, 2008 wow thank you all for the volume of responses. Greatly appreciated. A little more info... The table is set up like... id | title | content | category given that, I thought about using ORDER BY in my sql, but that really doesnt work, as I need to be able to pull everything from the table, sort everything from each row by category, and then be able to generate a link on each one <a href="view.php?id=$id">$title</a> so, based on what I've seen from you guys thus far, I'm thinking something along the lines of a combination of F1Fan's and aebstract's suggestions. Yes? Link to comment https://forums.phpfreaks.com/topic/127447-sorting-by-category/#findComment-659351 Share on other sites More sharing options...
geudrik Posted October 7, 2008 Author Share Posted October 7, 2008 addition: each 'group' of rows that fall into the same category need to be placed in different places within the page, hence why I'm thinking they should be their own entity. Now, I realize that I COULD simply make numerous queries, one for each category, but it seems like I should be able to have something a little more modular... Link to comment https://forums.phpfreaks.com/topic/127447-sorting-by-category/#findComment-659353 Share on other sites More sharing options...
F1Fan Posted October 7, 2008 Share Posted October 7, 2008 My suggestion should make this fairly simple. <?php $query = "SELECT * FROM products ORDER BY category, title"; $result = mysql_query($query); $all = array(); while ($row = mysql_fetch_assoc($result)){ $all[$row['category']][$row['id']] = $row; } /* If you want to select one category... */ $cat = "somecat"; foreach ($all[$cat] as $item){ echo "<a href=\"view.php?id={$item['id']}\">{$item['title']}</a>"; } /* Or, loop through all categories... */ foreach ($all as $cat=>$vals){ echo "Category $cat...<br>"; foreach ($vals as $item){ echo "<a href=\"view.php?id={$item['id']}\">{$item['title']}</a>"; } } ?> Link to comment https://forums.phpfreaks.com/topic/127447-sorting-by-category/#findComment-659377 Share on other sites More sharing options...
geudrik Posted October 9, 2008 Author Share Posted October 9, 2008 F!Fan, you're awesome. That worked just fine. I did a little tweeking to better suit my needs and its working great. Thanks man. Link to comment https://forums.phpfreaks.com/topic/127447-sorting-by-category/#findComment-660835 Share on other sites More sharing options...
F1Fan Posted October 9, 2008 Share Posted October 9, 2008 Glad to help. Link to comment https://forums.phpfreaks.com/topic/127447-sorting-by-category/#findComment-660922 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.