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

时间:2016-04-24 14:58:14

标签: php html mysql sorting

所以问题出在代码中,我尝试更改产品的排序方式,页面不加载atm,如果我注释掉_GET排序代码,只需更改一个且唯一的结果变量来排序,例如:价格那么有效,谢谢!

<?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>

2 个答案:

答案 0 :(得分:0)

你错过了条件内的mysqli_query功能。

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");
    }
}

但是,整个部分可以重构为:

$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);

答案 1 :(得分:-1)

main.php?sort = price_asc,price_desc,name_asc等然后爆炸$ _GET将为您提供数组索引0处的列名和数组索引1中的顺序,然后您可以在mysql / mysqli查询中使用它们。