在php中根据条件按顺序显示数组元素

在php中根据条件按顺序显示数组元素,php,arrays,sorting,Php,Arrays,Sorting,我有一个数组,即$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')."'

我有一个数组,即$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和发布标题
我想将数组$checked\u posts值(id)与select查询fetch id进行比较
如果两者都匹配,则我希望尽早显示该记录,如果不匹配,则应在所有选定记录之后显示此类记录。
假设我的$checked\u post数组包含-

 Array
(
    [0] => 13
    [1] => 15
)  
我的select query fetch记录(id)如15、13、20、25、32在这种情况下,我必须首先显示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>";  
                 }
$q=mysql\u query(“从sau posts中选择ID、post\u title
其中post_author='“$this->session->userdata('admin_id')。”
和post_type='post'
和post_status='publish'
按ID描述订购
");                           
而($p=mysql\u fetch\u数组($q))
{
if(在数组中($p['ID',$checked_posts))
{
$check_post='checked';//我想先显示所有这些元素,然后显示所有不匹配的元素。
}
其他的
{
$check_post='';
}
echo“
请帮帮我。

谢谢

您可以通过将支票放入查询的
orderby
子句中来实现这一点

$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' : '';

您可以通过将支票放入查询的
orderby
子句来实现这一点

$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' : '';

您可以加入检查过的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

您可以加入检查过的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

从何处获取checked_posts数组?db中是否有一列带有checked?@Mihai从名称中,我猜它来自表单上的复选框。@Mihai我从另一个select查询中获取$checked_post数组。然后,我想您只需要与另一个字段连接即可获取选中值,不需要php数组操作从何处获取checked_posts数组?db中是否有一列带有checked?@Mihai从名称中,我猜它来自表单上的复选框。@Mihai我从另一个select查询中获取$checked_post数组。然后,我想您只需要与另一个字段连接,从中获取选中值,不需要php数组操作@Mihai在一个db表中我存储了像1,5,7,15(逗号分隔)这样的ID,那么我如何使用join query?@Deepak
。join。。。在FIND_IN_SET(sa_posts.ID,someoOtherTable.checkedds)
@Mihai上,它正在工作,但只显示匹配的记录。@Deepak使用左连接,并按@Mihai在一个db表中的顺序添加FIND_IN_SET。我存储了像1,5,7,15(逗号分隔)这样的ID,那么如何使用连接查询?@Deepak
。连接。。。在FIND_IN_SET(sa_posts.ID,someoOtherTable.checkedds)
@Mihai它正在工作,但只显示匹配的记录。@Deepak使用左连接,并按顺序添加FIND_IN_SET BY