dongxi1943 2016-04-24 06: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 07: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 盘古气象大模型调用(python)
  • ¥15 传人记程序做的plc 485从机程序该如何写
  • ¥15 已知手指抓握过程中掌指关节、手指各关节和指尖每一帧的坐标,用贝塞尔曲线可以拟合手指抓握的运动轨迹吗?
  • ¥50 libwebsockets 如何添加其他socket事件回调
  • ¥50 实现画布拖拽算子排布,通过flink实现算子编排计算,请提供思路
  • ¥15 esium自定义材质拉伸问题
  • ¥15 cmake+mingw使用<mysqlx/xdevapi.h>报错
  • ¥15 eNSP中防火墙的使用
  • ¥15 关于#mlnet#的问题:mlnet相关请求(语言-c#)
  • ¥15 lvgl7.11怎么做出文字被选中的效果
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部