【问题标题】:display array elements in order based on condition in php根据php中的条件按顺序显示数组元素
【发布时间】:2017-03-25 07:09:43
【问题描述】:

我有一个名为 $checked_posts 的数组,它看起来像 -

Array
(
    [0] => 13
    [1] => 15
)  

此数组包含所选帖子的 ID。
我从数据库中获取数据 -

$q=mysql_query("select ID,post_title from sa_posts
                where post_author='".$this->session->userdata('admin_id')."'
                and post_type='post'
                and post_status='publish'
                order by ID desc
              ");  

此查询获取 id 和 post_title
我想将数组 $checked_posts value(id) 与选择查询获取 id 进行比较
如果两者都匹配,那么我想尽早显示该记录,如果不匹配,则应在所有选定记录之后显示此类记录。
假设我的 $checked_post 数组包含 -

 Array
(
    [0] => 13
    [1] => 15
)  

在这种情况下,我的选择查询会获取 15、13、20、25、32 之类的记录(id),我必须首先显示 id 匹配的记录,然后我想显示所有不匹配的元素。
到目前为止我已经尝试过 -

$q=mysql_query("select ID,post_title from sa_posts
                where   post_author='".$this->session->userdata('admin_id')."'
                and post_type='post'
                and post_status='publish'
                order by ID desc
               ");                           
                while($p=mysql_fetch_array($q))
                {
                    if(in_array($p['ID'],$checked_posts))
                    {
                           $check_post='checked'; // I want to display all these elements firstly and later all unmatched elements.
                        }
                        else
                        {
                           $check_post='';
                        }
echo "<li class='menu_post_list' id='".$m_n."' style='list-style:none;display:".$disp_post."'>
<input type='checkbox' value='".$p['ID']."' class='menu_list_post' $check_post> ".$p['post_title']."</li>";  
                 }

请帮帮我。
谢谢。

【问题讨论】:

  • 你从哪里得到checked_posts数组?你在db中有一个checked列吗?
  • @Mihai 从名字上看,我猜它来自表单上的复选框。
  • @Mihai 我从另一个选择查询中得到 $checked_post 数组。
  • 那我想你只需要一个 JOIN 与你得到检查值的另一个字段,不需要 php 数组操作

标签: php arrays sorting


【解决方案1】:

你可以加入被检查的ID,因为它们来自db,不需要php操作

select ID,post_title from sa_posts 
        JOIN someOtherTable ON sa_posts.ID=someoOtherTable.checkedIds
                where   post_author='".$this->session->userdata('admin_id')."'
                and post_type='post'
                and post_status='publish'
                order by ID desc

【讨论】:

  • @Mihai 在一个数据库表中,我存储了 1、5、7、15 之类的 id(逗号分隔),那么我将如何使用联接查询?
  • @Deepak ..JOIN... ON FIND_IN_SET(sa_posts.ID,someoOtherTable.checkedIds)
  • @Mihai 它正在工作,但只显示匹配的记录。
  • @Deepak 使用左 JOIN 并在 ORDER BY 中添加 FIND_IN_SET
【解决方案2】:

您可以通过将检查放入查询的ORDER BY 子句来做到这一点。

$checked_str = implode(',', $checked_posts);

$q=mysql_query("select ID,post_title from sa_posts
            where   post_author='".$this->session->userdata('admin_id')."'
            and post_type='post'
            and post_status='publish'
            order by ID IN ($checked_posts) DESC, ID desc
           ");   

您还可以通过将其添加到查询结果中来避免在 PHP 代码中执行 in_array() 测试:

$q=mysql_query("select ID,post_title, ID IN ($checked_posts) as checked
            from sa_posts
            where   post_author='".$this->session->userdata('admin_id')."'
            and post_type='post'
            and post_status='publish'
            order by checked DESC, ID desc
           ");   

那么PHP代码就可以使用了

$check_post = $p['checked'] ? 'checked' : '';

【讨论】:

    猜你喜欢
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-22
    相关资源
    最近更新 更多