将查询结果划分为基于字段的列表

时间:2022-06-09 19:21:17

I'm creating a custom page with Wordpress and need to divide my query results into lists based on a tag. I know there's a Wordpress stack exchange but this seems more like a basic PHP function to me. The only thing I found on the Wordpress stack said to execute new queries based on the tag name.

我正在使用Wordpress创建自定义页面,需要将查询结果划分为基于标记的列表。我知道有一个Wordpress堆栈交换,但这对我来说似乎更像是一个基本的PHP函数。我在Wordpress堆栈上找到的唯一一件事就是根据标签名称执行新的查询。

There must be a more efficient way...

必须有一个更有效的方式......

I'm new to PHP. To complicate matters further I've always struggled with loops but am grasping the concept more and more everyday.

我是PHP的新手。更复杂的是,我一直在努力解决循环问题,但我每天都在越来越多地掌握这个概念。

Here's the logic I'm looking to achieve:

这是我想要实现的逻辑:

Query the DB and Get the "title" of posts tagged with "Red, Green, Blue, Yellow". This part I can do with the WP_Query() function, no problem there... The trouble is the next part.

查询数据库并获取标有“红色,绿色,蓝色,黄色”的帖子的“标题”。这部分我可以用WP_Query()函数做,没有问题......麻烦就是下一部分。

Loop over the query and output:

遍历查询和输出:

<ul id="red">
<li>Output title of first post tagged with Red</li>
<li>Output title of second post tagged with Red</li>
<li>Output title of third post tagged with Red</li>
<!--[etc...]-->
</ul>
<ul id="green">
<li>Output title of first post tagged with Greeen</li>
<li>Output title of second post tagged with Green</li>
<li>Output title of third post tagged with Green</li>
<!--[etc...]-->
</ul>
<!--[continue for other colors requested in the query]-->

End the loop

结束循环

I understand the first loop that would iterate over the results and output the title of all the records, I'm just confused about how to loop over the results and further divide by the color.

我理解第一个循环将迭代结果并输出所有记录的标题,我只是对如何循环结果并进一步除以颜色感到困惑。

Any help is greatly appreciated.

任何帮助是极大的赞赏。

1 个解决方案

#1


0  

Firstly, you can sort the results you get from the database, simply add a

首先,您可以对从数据库中获得的结果进行排序,只需添加一个

ORDER BY `color` // for MySql at least

If you're looking to do this all in one loop the code is going to get a bit messy. You'll have to store the last color and terminate+start a new ul element every time the color changes.

如果你想在一个循环中完成所有这些代码将变得有点混乱。你必须存储最后一种颜色,并在每次颜色变化时终止+开始一个新的ul元素。

Another (better IMO) option would be to categorize your elements into arrays inside your loop and then after the loop, echo out the contents of each array appropriately.

另一个(更好的IMO)选项是将元素分类到循环内的数组中,然后在循环之后,适当地回显每个数组的内容。

$colors = array();
while($row = ..FETCH_MORE_DATA_FROM_DB...){
  if (!is_array($colors[$row['color']])){ 
    $colors[$row['color']] = array(); 
  }
  $colors[$row['color']][] = "<li>{$row['title}</li>";
}

foreach($colors AS $colorName => $elements){
  echo '<ul id="'. $colorName .'">';
  foreach($elements AS $element){
    echo $element;
  }
  echo '</ul>';
}

#1


0  

Firstly, you can sort the results you get from the database, simply add a

首先,您可以对从数据库中获得的结果进行排序,只需添加一个

ORDER BY `color` // for MySql at least

If you're looking to do this all in one loop the code is going to get a bit messy. You'll have to store the last color and terminate+start a new ul element every time the color changes.

如果你想在一个循环中完成所有这些代码将变得有点混乱。你必须存储最后一种颜色,并在每次颜色变化时终止+开始一个新的ul元素。

Another (better IMO) option would be to categorize your elements into arrays inside your loop and then after the loop, echo out the contents of each array appropriately.

另一个(更好的IMO)选项是将元素分类到循环内的数组中,然后在循环之后,适当地回显每个数组的内容。

$colors = array();
while($row = ..FETCH_MORE_DATA_FROM_DB...){
  if (!is_array($colors[$row['color']])){ 
    $colors[$row['color']] = array(); 
  }
  $colors[$row['color']][] = "<li>{$row['title}</li>";
}

foreach($colors AS $colorName => $elements){
  echo '<ul id="'. $colorName .'">';
  foreach($elements AS $element){
    echo $element;
  }
  echo '</ul>';
}