按价格排序,名称脚本无效...... PHP和MySQL

按价格排序,名称脚本无效...... PHP和MySQL

问题描述:

So the issue lies within the code where I try to change what the products are sorted by, the page doesnt load atm, If I comment out the _GET sort code and just change the one and only result variable to sort by e.g. price then that works, thank you!

<?php 
    require 'functions.php';
?>


<html>
<head>
    <link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>

<body>
    <center>
        <a href="main.php"><img style="position: relative; top:50px" src="logo.png"/></a>
    </center>
<hr>
    <?php cart(); ?>
    <div id="shopping_cart">
        <span style="float:left;">Welcome Guest | Total Items: <?php total_items();?> <b style="colour:blue;">| Total: £<?php total_price(); ?></b> <a href="cart.php">Go to Cart: </a></span>
    </div>

    <div class="menu">
        <ul>
            <li class="select"><a href="main.php">HOME</a></li>
            <li class="select"><a href="cart.php">BASKET </a></li>
            <li class="select"><a href="summary.php">OUR WORLD </a></li>
        </ul>
        <div id="form">
            <form method="get" action="results.php" enctype="multipart/form-data">
                <input type="text" name="user_query" placeholder="Search a Product"/>
                <input type="submit" name="search" value="Search"/>
            </form>
        </div>
    </div>

    <br>
    <br>
    <br>
        <select name="menu" id="drop">
            <option>-- Select a filter --</option>
            <option value="main.php?sort=name">Name A - Z</option>
            <option value="main.php?sort=pasc">Price Low-High</option>
            <option value="main.php?sort=pdesc">Price High-Low</option>    
        </select>


    <script type="text/javascript">
            var sortmenu = document.getElementById( 'menu' );
            sortmenu.onchange = function() {
                window.open( this.options[ this.selectedIndex ].value, "_self" );
            };
    </script>

    <?php
        $con = mysqli_connect(hello);
        $query = "SELECT * FROM MrPiece";
        #$result = mysqli_query($con, $query);

        $ip=getIp(); 

        $result = mysqli_query($con, "SELECT * FROM MrPiece ORDER BY ID ASC");

            if(isset($_GET["sort"])){
            if($_GET["sort"]=='pasc'){
                $result = ($con,"SELECT * FROM MrPiece ORDER BY Price ASC");
            }      
            elseif($_GET["sort"]=='pdesc'){
                $result = ($con,"SELECT * FROM MrPiece ORDER BY Price DESC");
            }   
            elseif($_GET["sort"]=='name'){
                $result = ($con, "SELECT * FROM MrPiece ORDER BY Name ASC");
            }   

            }

        if(mysqli_num_rows($result) > 0) {
            while($row = mysqli_fetch_assoc($result)) {
                $pro_id = $row['ID'];
                $product_name = $row['Name'];
                $product_price = $row['Price'];
                $product_image = $row['Image'];

                echo "<div id='products_box'>";
                echo "<div id='single_product'>";

                echo "<a href='details.php?pro_id=$pro_id'><img src='$product_image' style='height:250px; width:180px'></a> <br>";
                echo "<a href='details.php?pro_id=$pro_id'>$product_name</a><br>";
                echo "£ $product_price <br>";
                echo "<a href='main.php?add_cart=$pro_id'><button style='float: center;'>Add to cart</button></a>";

                echo "</div>";
                echo "</div>";
            }
        }
        else {echo "No results";}
        mysqli_close($con);
    ?>
</body>

<html>

You've missed the mysqli_query function from inside your conditionals.

if(isset($_GET["sort"])){
    if($_GET["sort"]=='pasc'){
        $result = mysqli_query($con,"SELECT * FROM MrPiece ORDER BY Price ASC");
    }
    elseif($_GET["sort"]=='pdesc'){
        $result = mysqli_query($con,"SELECT * FROM MrPiece ORDER BY Price DESC");
    }
    elseif($_GET["sort"]=='name'){
        $result = mysqli_query($con, "SELECT * FROM MrPiece ORDER BY Name ASC");
    }
}

However, this whole section could be refactored down to something like:

$query = "SELECT * FROM MrPiece ORDER BY ID ASC";

if(isset($_GET["sort"])){
    if($_GET["sort"]=='pasc'){
        $query = "SELECT * FROM MrPiece ORDER BY Price ASC";
    }
    elseif($_GET["sort"]=='pdesc'){
        $query = "SELECT * FROM MrPiece ORDER BY Price DESC";
    }
    elseif($_GET["sort"]=='name'){
        $query =  "SELECT * FROM MrPiece ORDER BY Name ASC";
    }
}

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

main.php?sort=price_asc, price_desc, name_asc etc and then exploding $_GET will leave you with the column name at array index 0 and order in array index 1 , you can then use both in your mysql/mysqli query.