【发布时间】: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 数组操作