PHP: do an ORDER BY using external data?

前端 未结 2 1385
臣服心动
臣服心动 2021-01-22 01:27

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:

相关标签:
2条回答
  • 2021-01-22 01:32
    ... ORDER BY FIELD(type,"Fruit","People","Place","Animals","Landscape")
    

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

    0 讨论(0)
  • 2021-01-22 01:42

    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 .")")
    
    0 讨论(0)
提交回复
热议问题