简体   繁体   中英

While Loop echos only 1 value twice

I have a 2 tables called forums and forum_categories in database

forum_categories
cat_id

forums
cat_id

I am trying to count the total for each category for some reason it displays total record for all records.

I have 2 categories so far in the database

first category has value of 1
second category has value of 2

In category one, I have two forums that has a cat_id of 1
In category two, I have one forum that has a cat_id of 2

it displays

category 1 -------------------- 3 ---------------
category 2 -------------------- 3 ---------------

when it should display

category 1 -------------------- 2 ---------------
category 2 -------------------- 1 ---------------

$topics_count always displays 3.

   $sql = "SELECT * FROM forum_categories, forums WHERE forum_categories.cat_id = forums.cat_id GROUP BY forum_categories.cat_id";
$result = query($sql);



$sql2 = "SELECT fc.cat_title COUNT(f.forum_id) FROM forums as f LEFT JOIN forum_categories fc ON (f.cat_id = fc.cat_id) GROUP BY f.cat_id";
$result2 = query($sql2);
$topics_count = count($result2);



while (($row = mysqli_fetch_assoc($result)) != false) {

    $cat_id = $row['cat_id'];
    $cat_title = $row['cat_title'];
    $forum_name = $row['forum_name']; 


    echo "<tr>";

    echo "<td>$cat_id</td>";
    echo "<td><a href='viewforum.php?f={$cat_id}'>$cat_title</a><br>$forum_name admin</td>";
    echo "<td>$topics_count</td>";
    echo "<td>0</td>";
    echo "</tr>";
}

This bit of code below gives you all results:

$sql2 = "SELECT * FROM forums WHERE cat_id = cat_id";
$result2 = query($sql2);
$topics_count = row_count($result2); // here this would be 3*

*in case of 2 for cat1 + 1 for cat2 and you echo this in each table row as:

echo "<td>$topics_count</td>";

therefore you see the total number (3) on both rows.

What you are actually trying is to join forums and forum_categories table, get the forum_categories.cat_title and COUNT(forums.cat_id) while grouping the results by forums.cat_id

SELECT
    fc.cat_title,
    COUNT(f.forum_id)
FROM forums as f
LEFT JOIN forum_categories fc ON (f.cat_id = fc.cat_id)
GROUP BY f.cat_id

Here is the same demo fiddle I've sent you yesterday.

Your query would then return you something like:

Demo Forum 1 | 2
Demo Forum 2 | 1

UPDATE:

$sql2 = "SELECT
            fc.cat_id,
            fc.cat_title,
            f.forum_name,
            COUNT(f.forum_id) as count
        FROM forums as f
        LEFT JOIN forum_categories fc ON (f.cat_id = fc.cat_id)
        GROUP BY f.cat_id";

$result2 = query($sql2);

while (($row = mysqli_fetch_assoc($result)) != false) {

    $cat_id = $row['cat_id']; // category id
    $cat_title = $row['cat_title']; // category title
    $forum_name = $row['forum_name']; // forum name
    $count = $row['count']; // count of forum_id (grouped by cat_id)

    echo "<tr>";
    echo "<td>$cat_id</td>";
    echo "<td><a href='viewforum.php?f={$cat_id}'>$cat_title</a><br>$forum_name admin</td>";
    echo "<td>$count</td>";
    echo "<td>0</td>";
    echo "</tr>";

}

Here, I'm pretty sure the count would be okay, but not sure about "Forum Name".

try this

replace this

while (($row = mysqli_fetch_assoc($result)) != false) {

with this

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

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