在 PHP 中,如何获取 mySQL table 列中每个实例的计数?
In PHP how can I get the count of each instance in a mySQL table column?
澄清一下,我正在尝试让 table 以某种方式显示我的 MySQL 数据库 table 的输出。 table 填充了名称、birth-years 和十二生肖。
我想要做的是有一个 table 行,上面写着类似 "cat zodiac signs" 的内容,然后是 "name" 和 "birth year" 的标题。在那之下,我想要每个人的名字和他们出生的年份。我想为每个生肖动物都这样做。
我遇到的问题是标题显示的是十二生肖栏的每个实例。我知道我的代码是错误的,我只是不确定下一步该怎么做!请帮忙!这是我的代码
$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);
}
编辑:可能有用的权利
我希望的输出是这样的(这是在 Excel 上制作的草稿,但要点):
编辑 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
}
您可以在 while 循环中使用一个变量来存储当前的生肖名称。所以每次生肖名称都是唯一的,这意味着在 while 循环中找到了新的生肖,你可以为它打印新的 header。
比如流程可以是这样的
$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
}
澄清一下,我正在尝试让 table 以某种方式显示我的 MySQL 数据库 table 的输出。 table 填充了名称、birth-years 和十二生肖。
我想要做的是有一个 table 行,上面写着类似 "cat zodiac signs" 的内容,然后是 "name" 和 "birth year" 的标题。在那之下,我想要每个人的名字和他们出生的年份。我想为每个生肖动物都这样做。
我遇到的问题是标题显示的是十二生肖栏的每个实例。我知道我的代码是错误的,我只是不确定下一步该怎么做!请帮忙!这是我的代码
$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);
}
编辑:可能有用的权利 我希望的输出是这样的(这是在 Excel 上制作的草稿,但要点):
编辑 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
}
您可以在 while 循环中使用一个变量来存储当前的生肖名称。所以每次生肖名称都是唯一的,这意味着在 while 循环中找到了新的生肖,你可以为它打印新的 header。
比如流程可以是这样的
$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
}