php 按“最新”排序“最喜欢” “最不喜欢”

发布于 2024-11-19 17:38:37 字数 2134 浏览 0 评论 0原文

我有一个帖子系统,

    <?php
/**
Display the results from the database
**/
$q = ("SELECT * FROM threads ORDER BY posted");
$r = mysql_query($q);

if(mysql_num_rows($r)>0): //table is non-empty
    while($row = mysql_fetch_assoc($r)):
        $net_vote = $row['votes_up'] - $row['votes_down']; //this is the net result of voting up and voting down
?>
<div class='entry'>
    <span class='link'>
        <?php echo $row['author']; ?>&nbsp;&nbsp;&nbsp;
        <?php $row['posted'] = date("jS M Y h:i",$row['posted']); echo $row['posted']; ?>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-text="<?php echo $row['message']; ?>">
        Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
        <br/> 
        <div class='message'><?php echo $row['message']; ?><br/></div> 
            <?php echo "<a href='msg.php?id=$row[id]'/> Comments/Add comments $row[replies]</a>" ?> &nbsp;
        <?php echo "Likes: " . $row['votes_up'] . "&nbsp "; echo "Dislikes: " . $row['votes_down'] . "&nbsp"; ?>    
    </span>

    <span class='votes_count' id='votes_count<?php echo $row['id']; ?>'></span> 
    <span class='vote_buttons' id='vote_buttons<?php echo $row['id']; ?>'>
        <a href='javascript:;' class='vote_up' id='<?php echo $row['id']; ?>'></a>
        <a href='javascript:;' class='vote_down' id='<?php echo $row['id']; ?>'></a>

    <br/>
    </span>

</div>

<br/>
<?php
    endwhile;
endif;
?>

我想添加文本“order by:最近|”最喜欢 |最不喜欢 '
正如你所看到的,我认为我已经通过默认方式发布了最新的内容,这就是我想要的。

但我还想要的是当您点击“最喜欢”时“最不喜欢”按“Vote_up”(喜欢)和“最不喜欢”排序“vote_down”(不喜欢)都在同一页面上,并显示最喜欢(最喜欢)和最不喜欢(最不喜欢)的帖子

编辑***
抱歉,我的问题是如何添加两个功能,点击时按“最喜欢”和“最不喜欢”排序

I have a post system in place

    <?php
/**
Display the results from the database
**/
$q = ("SELECT * FROM threads ORDER BY posted");
$r = mysql_query($q);

if(mysql_num_rows($r)>0): //table is non-empty
    while($row = mysql_fetch_assoc($r)):
        $net_vote = $row['votes_up'] - $row['votes_down']; //this is the net result of voting up and voting down
?>
<div class='entry'>
    <span class='link'>
        <?php echo $row['author']; ?>   
        <?php $row['posted'] = date("jS M Y h:i",$row['posted']); echo $row['posted']; ?>
                    <a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-text="<?php echo $row['message']; ?>">
        Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
        <br/> 
        <div class='message'><?php echo $row['message']; ?><br/></div> 
            <?php echo "<a href='msg.php?id=$row[id]'/> Comments/Add comments $row[replies]</a>" ?>  
        <?php echo "Likes: " . $row['votes_up'] . "  "; echo "Dislikes: " . $row['votes_down'] . " "; ?>    
    </span>

    <span class='votes_count' id='votes_count<?php echo $row['id']; ?>'></span> 
    <span class='vote_buttons' id='vote_buttons<?php echo $row['id']; ?>'>
        <a href='javascript:;' class='vote_up' id='<?php echo $row['id']; ?>'></a>
        <a href='javascript:;' class='vote_down' id='<?php echo $row['id']; ?>'></a>

    <br/>
    </span>

</div>

<br/>
<?php
    endwhile;
endif;
?>

I want to add text that says 'order by : Most recent | Most liked | least liked '
As you can see i think ive got it already posting most recent by defualt which is what i want.

But what i want also is when you click 'Most liked' & 'least liked' it sorts by 'Vote_up' ( likes) & 'vote_down' (dislikes) all on the same page and shows posts with most likes on them (most liked) and most dislike (least liked)

EDIT***

sorry my question is how can i add 2 functions that when on click sorts by 'most liked' and 'least liked'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬﹪忆之殇 2024-11-26 17:38:37

在 html:

<a href='script.php?order=recent'>Recent</a>
<a href='script.php?order=liked'>Liked</a>
...

在 php:

if ($_GET['order'] == 'recent') $order = "posted";
elseif ($_GET['order'] == 'liked') $order = "smth";
...
$q = "SELECT * FROM threads ORDER BY ".$order."";

但实际上最好使用一些 js 框架(我更喜欢 extjs 为此)在客户端对输出进行排序

in html:

<a href='script.php?order=recent'>Recent</a>
<a href='script.php?order=liked'>Liked</a>
...

in php:

if ($_GET['order'] == 'recent') $order = "posted";
elseif ($_GET['order'] == 'liked') $order = "smth";
...
$q = "SELECT * FROM threads ORDER BY ".$order."";

but actually it's better to use some js framework (i prefer extjs for that) to sort the output on client side

风铃鹿 2024-11-26 17:38:37

AFAIK 您几乎没有选择如何执行此操作:

  1. 让链接通过 URL 传递 $_GET 值(导致页面刷新!!),然后在 PHP 中对此进行测试并运行必要的 SQL 来获取新的记录顺序.

  2. 使用 AJAX 异步执行相同的请求,使用 PHP 脚本处理 SQL 函数并返回所需的结果。

    使用

  3. 使用 jQuery 插件(例如 tablesorter)(可能)基本上提供与 2 中相同的功能,或者通过它自己的过滤系统——我不确定我没有使用过它!!

我的偏好是 1.(即纯 PHP + MySQL 解决方案),因为这提供了最好的通用功能。您可以随时添加 javascript / ajax ,让事情变得更适合更现代的浏览器和用户!

AFAIK you have few choices how to do this:

  1. Have the links pass a $_GET value via URL (causes a page refresh!!), then test for this in the PHP and run the necessary SQL to get the new record order.

  2. Use AJAX to perform the same request asynchronously, with a PHP script to handle the SQL function and return the desired results.

  3. Use a jQuery plugin such as tablesorter to (probably) basically provide the same functionality as that in 2, or via it's own filtering system -- I'm not sure i've not used it!!

My preference would be 1. (i.e a pure PHP + MySQL solution) as this offers the best universal functionality. You could always add javascript / ajax later to make things more swish for more modern browsers and users!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文