简体   繁体   English

在PHP中,如何获取mySQL表列中每个实例的计数?

[英]In PHP how can I get the count of each instance in a mySQL table column?

To clarify, I am trying to get a table to output that displays my MySQL database table in a certain way. 为了澄清起见,我试图获取一个表以某种方式显示我的MySQL数据库表的输出。 The table is populated with names, birth-years, and zodiac animals. 该表中填充了姓名,出生年月和生肖动物。

What I want to do is have a table row that says something like "cat zodiac signs" and then a heading of "name" and "birth year". 我想要做的是在一个表行中写上“猫生肖”之类的标题,然后是“姓名”和“出生年份”的标题。 Under that I want each persons' name and year they were born. 在此之下,我想要每个人的名字和出生年份。 I want this done for each zodiac animal. 我希望每个生肖动物都这样做。

The issue I am having is that the heading for is displaying each instance of the zodiac column. 我遇到的问题是标题正在显示黄道十二宫列的每个实例。 I know my code is wrong, I am just not sure what to do next! 我知道我的代码是错误的,我只是不确定下一步该怎么做! Please help! 请帮忙! Here is my code 这是我的代码

$TableName = "zodiacshared";
        $SQLstring = "SELECT * FROM $TableName GROUP BY sign, birthyear";
        $QueryResult = @mysql_query($SQLstring, $DBConnect);
        if (mysql_num_rows($QueryResult) == 0){
            echo "<p> There are no entries in the chinese zodiac gallery! </p>";
        }
        else{
            echo "<table width = '100%' border = '1'>";
            while ($row=mysql_fetch_array($QueryResult)){
                    echo "<th colspan='2'>"; echo $row['sign']; echo " zodiac signs</th>";
                    echo "<tr><th>Name</th><th>Birth Year</th></tr>";
                    echo "<tr>";
                    echo "<td>"; echo $row['name']; echo "</td>";
                    echo "<td>"; echo $row['birthyear']; echo "</td>";
                    echo "</tr>";

            }
            echo "</table>";
            mysql_free_result($QueryResult);
        }

在此处输入图片说明

Edit: Right that may be helpful My hopeful output is something like this (this is a rough draft made on Excel, but gets point across): 编辑:可能会有帮助我的希望的输出是这样的(这是在Excel上进行的粗略草拟,但很容易理解):

期望的输出

edit 2: Solution was: 编辑2:解决方案是:

$current_zodiac= '';
            while ($row=mysql_fetch_array($QueryResult)){
                    if ($row['sign']!=$current_zodiac){
                        echo "<th colspan='2'>"; echo $row['sign']; echo " zodiac signs</th>";
                        echo "<tr><th>Name</th><th>Birth Year</th></tr>";
                    }else{
                        //echo the other rows
                    }
                        echo "<tr>";
                        echo "<td>"; echo $row['name']; echo "</td>";
                        echo "<td>"; echo $row['birthyear']; echo "</td>";
                        echo "</tr>";
                    $current_zodiac = $row['sign']; //assign new zodiac to current zodiac variable

            }

You can have one variable to store current zodiac name in your while loop. 您可以使用一个变量在while循环中存储当前的生肖名称。 So every time the zodiac name is unique which means new zodiac found in the while loop, you can print new header for it. 因此,每次生肖名称都是唯一的,这意味着在while循环中找到新的生肖,则可以为其打印新的标头。

For example, the flow could be like this. 例如,流程可能像这样。

$current_zodiac = '';
while ($row=mysql_fetch_array($QueryResult)){
if ($row['sign']!=$current_zodiac){
 echo "<th colspan='2'>"; echo $row['sign']; echo " zodiac signs</th>";
}else{
//echo the other rows
}
$current_zodiac = $row['sign']; //assign new zodiac to current zodiac variable

}

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

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