Question

I'm new to PHP and started learning recently, and I've been working on a forum. Everything is going great so far and everything is completely functional until I tried grouping the forum sections together as you would see on almost all popular forum software.

It's supposed to work sort of like this, however the first section isn't showing up under General Discussions and if I add anymore categories and category groups everything this screws up. Example Image

Here is the section of code, and excuse the most likely improper coding.. I'm really new to this. Any help would be appreciated.

<?php
$groupquery = "SELECT * FROM catgroup";
$gresult = mysqli_query($dbc, $groupquery)
or die('Error querying database');

while ($row = $gresult->fetch_row()){
    $groupid = $row[0];
    $grouptitle = $row[1];

    print('<div id="groupmain">');
    print('<div id="grouptitle">'.$grouptitle.'</div>');

    $query = "SELECT id, title, description, groupid FROM category";
    $result = mysqli_query($dbc, $query)
    or die('Error querying database');
    while ($row = $result->fetch_row()){
        $catgid = $row[3];
        $catid = $row[0];
        $cattitle = $row[1];
        $catdesc = $row[2];
    }



    if($groupid == $catgid){
        print('<div id="catmain">');
        print('<div id="catitle">');
        print('<a class="nav" href=section.php?id=' . $catid . '><img         src="icon2.png" alt="folder">' . $cattitle . '</a><br /><Br/>');
        print('</div>');
        print('<a class="desc">' . $catdesc . '</a>');
        print('</div><div><br />');
    }

    print('</div><br/><br/>');




}
Était-ce utile?

La solution

Can you try using, it shows only last occurrence $catid, since your section outside the second loop. Now Moved into the loop section, you can view all the section with all categories.

    while ($row = $result->fetch_row()){
            $catgid = $row[3];
            $catid = $row[0];
            $cattitle = $row[1];
            $catdesc = $row[2];

            if($groupid == $catgid){
                print('<div id="catmain">');
                print('<div id="catitle">');
                print('<a class="nav" href=section.php?id=' . $catid . '><img         src="icon2.png" alt="folder">' . $cattitle . '</a><br /><Br/>');
                print('</div>');
                print('<a class="desc">' . $catdesc . '</a>');
                print('</div><br />');
            }
        }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top