dongxi1943 2016-04-24 14:58
浏览 32
已采纳

按价格排序,名称脚本无效...... 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>
  • 写回答

2条回答 默认 最新

  • duannuan0074 2016-04-24 15:04
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助