php使用过滤器更改帖子的顺序

时间:2014-02-10 06:22:40

标签: php

我正在尝试在我的网站上创建一些过滤器,用户可以选择,如果他想要最新/最旧的帖子在顶部等等。 我该怎么做才能让用户选择所需的选项然后点击保存,页面刷新显示帖子的新顺序,并显示所做的选择

我试过这个

<ul>
<li><label><input type="radio" name="order_list" value="link1">Newest First</label></li>
<li><label><input type="radio" name="order_list" value="link2">Lowest Price First</label></li>
<li><label><input type="radio" name="order_list" value="link3">Highest Price First</label></li>
<li><label><input type="radio" name="order_list" value="link4">Nearest First</label></li>
</ul>


<?php
        switch($_POST['order_list']) {

case "link1":
    $res = mysql_query("SELECT * FROM `posts` ORDER BY `id` DESC") or die(mysql_error());
    break;

case "link2":
    $res = mysql_query("SELECT * FROM `posts` ORDER BY `price` ASC") or die(mysql_error());
    break;

case "link3":
    $res = mysql_query("SELECT * FROM `posts` ORDER BY `price` DESC") or die(mysql_error());
    break;

default:
    $res = mysql_query("SELECT * FROM `posts` ORDER BY `id` DESC") or die(mysql_error());
}
?>

i什么过滤器只在点击“保存”时工作,并以某种方式显示用户帐户的选择,所以每次他们登录网站时,选择总是设置为他们保存它

更新:

这是$ res的作用:

if(mysql_num_rows($res) > 0){
        while($row = mysql_fetch_assoc($res)){
            $id = $row['id'];
            $user_id = $row['user_id'];
            $price = $row['price'];
            $cover = $row['cover'];
            $title = $row['title'];

2 个答案:

答案 0 :(得分:0)

您需要为此添加表单并提交按钮,并使action=""重新加载同一页面,并提交此类

<?php
$order_list = "";

if(isset($_POST['submit'], $_POST['order_list']))
{
    $order_list = $_POST['order_list'];
}

$link1 = "";
$link2 = "";
$link3 = "";
$link4 = "";

        switch($order_list) {

        case "link1":
            $res = mysql_query("SELECT * FROM `posts` ORDER BY `id` DESC") or die(mysql_error());
            $link1 = "checked";
            break;

        case "link2":
            $res = mysql_query("SELECT * FROM `posts` ORDER BY `price` ASC") or die(mysql_error());
            $link2 = "checked";
            break;

        case "link3":
            $res = mysql_query("SELECT * FROM `posts` ORDER BY `price` DESC") or die(mysql_error());
            $link3 = "checked";
            break;

        default:
            $res = mysql_query("SELECT * FROM `posts` ORDER BY `id` DESC") or die(mysql_error());
            $link4 = "checked";
        }
?>


<form action=""  method="post" >
    <ul>
        <li><label><input type="radio" name="order_list" value="link1" <?php echo $link1;?>>Newest First</label></li>
        <li><label><input type="radio" name="order_list" value="link2" <?php echo $link2;?>>Lowest Price First</label></li>
        <li><label><input type="radio" name="order_list" value="link3" <?php echo $link3;?>>Highest Price First</label></li>
        <li><label><input type="radio" name="order_list" value="link4" <?php echo $link4;?>>Nearest First</label></li>
    </ul>
    <input type="submit" value="Save" name="submit" />
</from>

答案 1 :(得分:0)

你好把你的内容放在一个表格中,这样做

<form action           ='<?php $_SERVER['PHP_SELF']; ?>' method='post'>
<ul>
<li><label><input type ="radio" name="order_list" value="link1">Newest First</label></li>
<li><label><input type ="radio" name="order_list" value="link2">Lowest Price First</label></li>
<li><label><input type ="radio" name="order_list" value="link3">Highest Price First</label></li>
<li><label><input type ="radio" name="order_list" value="link4">Nearest First</label></li>
</ul>
<input type            ="submit" value='Search'>
</form>


<?php
switch($_POST['order_list']) {

case "link1":
    $res = mysql_query("SELECT * FROM `posts` ORDER BY `id` DESC") or die(mysql_error());
    break;

case "link2":
    $res = mysql_query("SELECT * FROM `posts` ORDER BY `price` ASC") or die(mysql_error());
    break;

case "link3":
    $res = mysql_query("SELECT * FROM `posts` ORDER BY `price` DESC") or die(mysql_error());
    break;

default:
    $res = mysql_query("SELECT * FROM `posts` ORDER BY `id` DESC") or die(mysql_error());
}
?>