Home > Blockchain >  List items with category
List items with category

Time:06-21

This is my code PHP.

<ul >
    <?
        $que = mysql_query('SELECT * FROM dosya ORDER BY hasid ASC');
        $ayn = '';
        if (mysql_num_rows($que) > 0) {
            while($b = mysql_fetch_array($que,MYSQL_ASSOC)) {
                if ($b['hasid'] != $ayn) {
                    $ayn = $b['hasid'];
                    ?>
                        <li ><?=$b['hasid']?>

                    <?
                }
                ?>
                        <ul>
                            <li><?=$b['adi']?></li>
                        </ul>
                    </li>
                <?
            }
        }
    ?>
  
</ul>

results:
results

but I have more data inside of these folders... I tried so many combinations but it didn't work. How can I solve this?

CodePudding user response:

I tried to guess at how your data might be structured in the database based on your code, but I'm not sure I was right, because it's tough to makes sense of your logic.

Since the query is ordered by hasid I assume each file has the id of the folder it lives in.

<ul >
<?php
    $que = mysqli_query('SELECT * FROM dosya ORDER BY hasid ASC');
    $ayn = ''; // this isn't used for anything right now
    
    if (mysqli_num_rows($que) > 0) {
        // before getting confused trying to draw things,
        // get the information organized nicely.
        // Since we will be displaying files organized by folder,
        // make the data organized by folders first.

        // Obviously you don't have to do this step, but it will
        // make your code a lot easier to maintain.

        $folders = [];

        while($b = mysqli_fetch_array($que,MYSQL_ASSOC)) {
            
            $folderId = $b['hasid'];

            if (!isset($folders[$folderId']) {
                // create a new empty list for the new folder
                $folders[$folderId] = [];
            }

            // put the data for this row in its folder list
            $folders[$folderId][] = $b;

        }

        // Now you have a list of folders, and each folder has a list of its contents.
        // That will make it much easier to imagine how to render the data in lists.
        // Now you can loop through the folders, and for each folder, create the 
        // folder icon and then build the list of contents with another loop.

        // Using print instead of filling the code with <? ?> blocks is a
        // personal preference; if you change it back to match your style
        // that will give you a chance to go over the code.

        foreach ($folders as $folderId => $contents) {
            print '<li >' . $folderId;
            print '<ul>';
            foreach ($contents as $file) {
                print '<li>' . $file['adi'] . '</li>;
            }
            print '</ul>';
            print '</li>';
        }
    }
?>

</ul>
  • Related