PHP Switch 语句 [英] PHP Switch statement

查看:39
本文介绍了PHP Switch 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

switch ($sort) {
    case 'abc':
        $order_by = 'subject ASC';
        break;
    case 'fn':
        $order_by = 'u.username ASC';
        break;
    case 'rd':
        $order_by = 'p.posted_on DESC';
        break;
    default:
        $order_by = 'p.posted_on DESC';
        $sort = 'rd';
        break;
}

我想稍微修改一下这段代码,只是不太确定该怎么做.我很确定我可以用 if else if else if 来做,但我假设改变这个开关会很简单,但我又不确定.

I want to modify this little piece of code slightly I'm just not quite sure how to do it. Im pretty sure I could do it with an if else if else if, but Im assuming changing this switch would be pretty simple, but then again I'm not sure.

无论如何这就是我想要做的,让我们以'case 'abc'为例

Anyway this is what Im trying to do, lets take the `case 'abc': for example

我想要

case 'abc':
    $order_by = 'subject DESC';
    break;

如果

case 'abc':
    $order_by = 'subject ASC';
    break;

这样,当您对记录进行排序时,如果排序已经在使用 ASC,它将切换到 DESC,现在我的排序按钮只能以一种方式工作.

So that way, when you are sorting through records, if the sort is already using ASC, it will switch to DESC, right now my sort buttons only work one way.

TY

推荐答案

switch ($sort) {
    case 'abc':
        $order_by = 'subject ASC';
        $sort = 'cba'
        break;
    case 'cba':
        $order_by = 'subject DESC';
        $sort = 'abc'
        break;  
    case 'fn':
        $order_by = 'u.username ASC';
        break;
    case 'rd':
        $order_by = 'p.posted_on DESC';
        break;
    default:
        $order_by = 'p.posted_on DESC';
        $sort = 'rd';
        break;
}

这篇关于PHP Switch 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆