Jump to content

Sorting by category...


geudrik

Recommended Posts

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

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

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

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

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

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.