简体   繁体   中英

PHP function not showing data from dropdown menu

Currently creating 2 dropdown menus, one for category and one for subcategory. My current function shows all data on the page only for subcategories but not for categories. Why is this happening?

Current Functionality: User selects a category, page refreshes and is blank. Once user selects SUBcategory it shows all products in that subcategory.

Desired Functionality: User selects a category, page refreshes and shows all products in that category. Once user selects subcategory it shows all products in that subcategory.

function populate_search_catsub()
{
    global $link;
    $subcatt = "";
    $query = 'SELECT * FROM item';
    $result = mysqli_query($link, $query);
    $catt = $_GET['catt'];
    if (isset($_GET['subcatt'])) {
        $subcatt=$_GET['subcatt'];
    }
    $nbprod = mysqli_query($link, "SELECT * FROM `item` WHERE cat='$catt' AND field='$subcatt'");

    if (!isset($_GET['catt']) || $_GET['catt'] == '') {
        unset($_GET['catt'], $_GET['submitsearchsub']);
        populate_main();
    } else {

        if (isset($_GET['subcatt'])) {
            echo '<span>Search results for Category="'.$catt.' And Sub Category='.$subcatt.'"</span><hr>';
        }

        $result = mysqli_query($link,"SELECT * FROM `item` WHERE cat='$catt' AND field='$subcatt'");

        if ($cat = mysqli_fetch_row($result)) {
            echo '
                <div class="itemlist">
                    <span><h3>'.$cat[1].'</h3><h6><a href="#"><u>View</u></a></h6></span>
                    <div class="col-lg-12" style="background-color: white;"><br>
                        <div class="row">
                            <div class="col-lg-12" style="margin-right: 2%;">
                                <a href="#" class="thumbnail"><img src="https://via.placeholder.com/160x210"></a>
                            </div>
                        </div><br>
                    </div>
                </div>
                <hr>
            ';
            while ($cat = mysqli_fetch_row($result))
                echo '
                    <div class="itemlist">
                        <span><h3 style="display:inline;">'.$cat[1].'</h3><h6 style="display:inline; margin-left: 1%;"><a href="#"><u>View</u></a></h6></span>
                        <div class="col-lg-12" style="background-color: white;"><br>
                            <div class="row">
                                <div class="col-lg-2" style="margin-right: 2%;">
                                    <a href="#" class="thumbnail"><img src="https://via.placeholder.com/160x210"></a>
                                </div>
                            </div><br>
                        </div>
                    </div>
                    <hr>
                ';
        } else {
            if (isset($_GET['subcatt'])) {
                echo "<h2 >No results found</h2>";
            }
            unset($_GET['catt'], $_GET['submitsearchsub']);
            populate_main();
        }
    }
}

First of all, grab $_GET['catt'] and $_GET['subcatt'] . Then, you can build a query based on these parameters. And please, use mysqli_num_rows to check if there are more than zero rows returned.

function populate_search_catsub()
{
    global $link;

    $catt = $_GET['catt'] ?? ''; // Requires PHP 7.0 or upper.
    $subcatt = $_GET['subcatt'] ?? '';  // Requires PHP 7.0 or upper.

    $query = 'SELECT * FROM `item`'; // Initial query

    if (!$catt) {
        unset($_GET['submitsearchsub']);
        populate_main();
    }  else {
        $query .= " WHERE `cat` = '{$catt}'";

        if ($subcatt) {
            echo '<span>Search results for Category="' . $catt . ' And Sub Category=' . $subcatt . '"</span><hr>';
            $query .= " AND `field` = '{$subcatt}'";
        }

        $result = mysqli_query($link, $query);

        if (mysqli_num_rows($result) > 0) { // Check if result set is not empty.
            while ($cat = mysqli_fetch_row($result))
                echo '
                    <div class="itemlist">
                        <span><h3 style="display:inline;">'.$cat[1].'</h3><h6 style="display:inline; margin-left: 1%;"><a href="#"><u>View</u></a></h6></span>
                        <div class="col-lg-12" style="background-color: white;"><br>
                            <div class="row">
                                <div class="col-lg-2" style="margin-right: 2%;">
                                    <a href="#" class="thumbnail"><img src="https://via.placeholder.com/160x210"></a>
                                </div>
                            </div><br>
                        </div>
                    </div>
                    <hr>
                ';
        } else {
            if ($subcatt) {
                echo '<h2 >No results found</h2>';
            }

            unset($_GET['catt'], $_GET['submitsearchsub']);
            populate_main();
        }
    }
}

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