PHP:使用外部数据执行 ORDER BY?

发布于 2024-12-23 20:41:42 字数 520 浏览 0 评论 0原文

嘿大家!长话短说,如果您不介意向这位 PHP 新手伸出援手的话。 :)

我有一个名为“类别”的数据库字段,其中存储了此内容:

Fruit, People, Place, Animals, Landscape

我在数据库中还有一个单独的表,其中每个项目的字段中都有带有这些类别名称的项目。现在,该脚本(我正在尝试对其进行一点分叉)使用:

SELECT DISTINCT(type), type FROM the_categories ORDER BY type ASC 

为了显示所有可用类别的列表。够简单吧?

好吧.....我不想按 ASC 排序,我想按我提到的第一个“类别”字段中的项目列表排序。无论它们的顺序是什么,我想要显示上面的“类型”的顺序。

显然,我必须对逗号进行爆炸,并且可能按照任何顺序给它们一个 1...但即便如此...我如何使用存储在另一个文件夹中的数据进行“orderby”?

这可能吗?哈哈,再次感谢!

Ahoy all! Long story short with this one if you don't mind lending a hand to this novice PHPer. :)

I have a database field called "Categories" that has this stored:

Fruit, People, Place, Animals, Landscape

I also have a separate table in the DB that has items with these category names in the fields for each item. Right now, the script (i am trying to fork it a bit) uses:

SELECT DISTINCT(type), type FROM the_categories ORDER BY type ASC 

in order to display a list of all categories available. Simple enough right?

Welllllll..... I don't want to sort by ASC, I want to sort by the list of items in the first Categories field I mentioned. Whatever order those are in is the order I want to display the "types" above.

Obviously I will have to do an explode on the commas, and maybe give them a 1 to whatever order....but even then.... how do I do an "orderby" using data stored in another folder?

Is this even possible? lol Thanks again!

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

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

发布评论

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

评论(2

梦回梦里 2024-12-30 20:41:42
... ORDER BY FIELD(type,"Fruit","People","Place","Animals","Landscape")

http://www.cfdan.com/posts/Handy_MySQL_-_ORDER_BY_FIELD.cfm

... ORDER BY FIELD(type,"Fruit","People","Place","Animals","Landscape")

http://www.cfdan.com/posts/Handy_MySQL_-_ORDER_BY_FIELD.cfm

迷爱 2024-12-30 20:41:42

这样未来的旁观者就可以得到它......这是爆炸代码

$query2 = mysql_query("SELECT * FROM categorytable");
        while($row = mysql_fetch_array($query2)){
            $categories = html_entity_decode($row['categories']);
            $thelist = explode(',', $categories);

        foreach($thelist as $order){
            if(trim($order) != ''){
                $order = trim($order);
                $order = ", '".$order."'";
                $theorder .= $order;
            }
        }

,然后对于查询,只需放入 order 变量

SELECT DISTINCT(type), type FROM the_categories ORDER BY FIELD(type" . $theorder .")")

And just so future onlookers have it .... here is the explode code

$query2 = mysql_query("SELECT * FROM categorytable");
        while($row = mysql_fetch_array($query2)){
            $categories = html_entity_decode($row['categories']);
            $thelist = explode(',', $categories);

        foreach($thelist as $order){
            if(trim($order) != ''){
                $order = trim($order);
                $order = ", '".$order."'";
                $theorder .= $order;
            }
        }

and then for the query, just put in the the order variable

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