简体   繁体   English

计算并显示数据库中的条目

[英]Count and display entries with a database

I have menu that displays entries using php and mysql. 我有菜单显示使用php和mysql的条目。 What I'm trying to do is to get a list of products that equal clothing for example then shoes and so on. 我想做的是获取与衣服相等的产品清单,例如鞋子等。 Theirs about 4 main types, this value can't be changed. 他们大约有4种主要类型,此值无法更改。

So: 所以:

$query = mysql_query("SELECT * FROM products WHERE type = 'Clothing'")or die(mysql_error());

Instead of looping through I'll just repeat the process 4 times. 除了重复遍历之外,我将重复该过程4次。 After this I want to display each main category and sub category for each type of clothing with the products table, so that it displays like this. 之后,我要在产品表中显示每种服装的每个主要类别和子类别,以便其显示如下。

Clothing 服装

Main Category 1 (1)Sub 1 (35)Sub 2 (4)Sub 3 主类别1(1)子1(35)子2(4)子3

Main Category 2 (1)Sub 1 (35)Sub 2 (4)Sub 3 主类别2(1)子1(35)子2(4)子3

Shoes

Main Category 1 (1)Sub 1 (35)Sub 2 (4)Sub 3 主类别1(1)子1(35)子2(4)子3

Main Category 2 (1)Sub 1 (35)Sub 2 (4)Sub 3 主类别2(1)子1(35)子2(4)子3

And so on I get a list of each main catogroy in the DB form a different table and loop through 依此类推,我得到了数据库中每个主目录的列表,形成了不同的表并循环遍历

$get_cats = mysql_query("SELECT * FROM main_cats")or die(mysql_error());
    //loop through each 
while($main_cat = mysql_fetch_assoc($get_cats)){
    //count main cat in products
    $check = mysql_real_escape_string($main_cat['cat']);
        $p_main_count =  mysql_query("SELECT * FROM products WHERE cats = '$check' ORDER BY cats")or die(mysql_error());
//this would get an array of each product that has that main category
} 

This looks all well and good but I can't think of a way how to display the data in the format I need it. 这看起来很好,但是我想不出一种如何以我需要的格式显示数据的方法。 The products table has 3 main columns for this: 产品表为此包含3个主要列:

  1. type 类型
  2. cats (main category) 猫(主要类别)
  3. sub cats (contains string 1,2,4 of multiple sub categorys) 子猫(包含多个子类别的字符串1,2,4)

Is there a way selecting and groups each main category and then displaying and counting the sub category's for each main category. 有没有一种方法可以选择并分组每个主要类别,然后显示并计算每个主要类别的子类别。

W3Schools has a really good tutorial for counting and displaying MySql Database entries. W3Schools有一个非常好的教程,用于计数和显示MySql数据库条目。 Here's a snippet of how you could try to display the Data in a table. 这是您如何尝试在表中显示数据的片段。

<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM main_cats");

while($row = mysqli_fetch_array($result))
  {

  echo $row['type'] . " " . $row['cats'] . " " . $row['subcats'];
  echo "<br>";
  }
?> 

Lets say row 1 has the data Type: 1 Cats: 2 SubCats: 3 假设第1行的数据类型:1个类别:2个子类别:3个
and row 2 has the data Type:2 Cats: 4 SubCats: 6 并且第2行的数据类型:2猫:4个子猫:6

This would output the data like this 这样将输出数据

1 2 3 1 2 3
2 4 6 2 4 6

You can easily use the tables from HTML to make this look alot nicer. 您可以轻松地使用HTML中的表格来使外观看起来更好。

echo "<table border="1">";
while($row = mysqli_fetch_array($result))
  {

  echo "<tr><td>" . $row['type'] . "</td><td> " . $row['cats'] . "</td><td> " . $row['subcats'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

This would output in the same order but in a table. 这将以相同顺序但在表中输出。

Hope this helps. 希望这可以帮助。 Also if you want any good references http://w3schools.com is a very good website. 另外,如果您需要任何好的参考资料, http://w3schools.com是一个非常好的网站。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM