[英]Display results grouped by a category
OK i have no idea how to word the title better but hopefully i can explain better here: 好吧,我不知道如何更好地称呼标题,但希望我可以在这里更好地解释:
I have a database of animals, with several columns, one of which is named Category
which contains a single word string such as frog
, or newt
. 我有一个动物数据库,其中有几列,其中一列名为
Category
,其中包含一个单词字符串,例如frog
或newt
。
I can successfully query the database for these things in two separate queries and print the results separately, but i ideally want to use a single query and then split the data afterwards to print the result to the page but in two sections, one for frogs and one for newts. 我可以在两个单独的查询中成功查询数据库中的这些内容,然后分别打印结果,但是理想情况下,我想使用一个查询,然后将数据拆分以将结果打印到页面上,但是分为两个部分,一个用于青蛙,另一个用于一new。
currently my code looks like this: 目前我的代码如下所示:
$query = "SELECT * FROM livestock WHERE Category = 'frog' OR Category = 'newt'";
$result = mysqli_query($con, $query) or die(mysqli_error($con));
echo"<div class='rowtable'>";
while($row = mysqli_fetch_array($result)){
$commondraft = $entry['Name'];
echo"<a href='/stocklist/".$entry['id']."/".commonName($commondraft)."' class='row'>";
echo"<div class='common'>".$entry['Name']."</div>";
echo"<div class='descr'>".$row['Description']."</div>";
echo"<div class='sex'>".$row['Sex']."</div>";
echo"<div class='age'>".$row['Age']."</div>";
echo"<div class='size'>".$row['Size']."</div>";
echo"<div class='origin'>".$row['Origin']."</div>";
echo"<div class='scientific'>".$row['Scientific']."</div>";
echo"<div class='prices'>".$row['Price']."</div>";
echo"</a>";
}
echo"</div>";
This obviously prints out all entries for both categories of frog
AND newt
. 显然,这会打印出
frog
和newt
这两个类别的所有条目。 How can i select just one category here and then use the other category elsewhere without re-querying the database for just the remaining category? 我如何在这里仅选择一个类别,然后在其他地方使用另一个类别,而不必仅对剩余类别重新查询数据库?
You can do what others suggested ( order by Category
), but I think this will be better solution: when you retrieve items from livestock
table you can put them in separate arrays for every category. 您可以执行其他人建议的操作(
order by Category
),但是我认为这将是更好的解决方案:当您从livestock
表中检索项目时,可以将它们放在每个类别的单独数组中。 I mean, that you can use $items
array (in my example) as dictionary (hash map) where key is category name and value is array of all items which belongs to that category. 我的意思是,您可以将
$items
数组(在我的示例中)用作字典(哈希图),其中键是类别名称,值是属于该类别的所有项目的数组。 Later, when you want to output items from some category just call output
function with desired category. 稍后,当您要输出某个类别的项目时,只需调用所需类别的
output
函数即可。 For example (I have simplified output; you can use this for any amount of categories. You just need to change $query
in getItems
): 例如(我简化了输出;您可以将其用于任何数量的类别。您只需要在
getItems
更改$query
):
function getItems($con) {
$items = array();
$query = "SELECT * FROM livestock WHERE Category = 'frog' OR Category = 'newt'";
$result = mysqli_query($con, $query) or die(mysqli_error($con));
while($row = mysqli_fetch_array($result)) {
$category = $row['Category'];
if (!isset($items[$category])) {
$items[$category] = array();
}
array_push($items[$category], $row);
}
return $items;
}
function output($category, $items) {
echo"<div class='rowtable'>";
foreach ($items[$category] as $entry) {
echo"<div class='common'>".$entry['Name']."</div>";
}
echo"</div>";
}
$items = getItems($con); // $con holds connection to database
output('frog', $items); // prints just items from frog category
output('newt', $items); // prints just items from newt category
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.