1

I've got a table with some rows that contain 3 fields (category, title, image). At first I created a foreach loop that returned some html with the information from each of the rows. However, now I actually want to create a scenario where I can "filter" my loop by category.

What I mean to say is that I want to create a function that will only generate the html for rows that have a particular value for their category field. I want to be able to apply this function to all the different values for category.

Any help would be appreciated.

4

2 回答 2

2

MySql 查询解决方案:
在查询中 使用WhereStatement,并保持 PHP 不变。
例如

Select * From table Where `category`="Filter Value";

让我知道这是否适合您,或者您是否仅限于使用 PHP 来过滤类别..

于 2013-05-22T03:16:46.543 回答
1

要么WHERE按照 asifrc 的建议使用该子句。

或者做这样的事情

while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    if($row['category'] == 'category1') {
      // do some stuff example
      $html_output .= '<p style="font-weight:bold">Important category: ' . $row['title'] . '</p>';
    } else if($row['category'] == 'category2') {
      // do other stuff
      $html_output .= '<p>Not important category: ' . $row['title'] . '</p>';
    }
}
于 2013-05-22T03:22:27.400 回答