简体   繁体   中英

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. 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):

期望的输出

edit 2: Solution was:

$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. So every time the zodiac name is unique which means new zodiac found in the while loop, you can print new header for it.

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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