0

I have an array namely $checked_posts which looks like -

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

This array contains the id's of the posts which are selected.
I fetch data from database like -

$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
              ");  

This query fetch id and post_title
I want to compare array $checked_posts value(id) with select queries fetch id
If both match then I want to show that record early, if not then such records should be display after all selected records.
Suppose my $checked_post array contains -

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

and my select query fetch records(id's) like 15,13,20,25,32 in such conditions I have to display the records firstly where the id matches and later I want to display all unmatched elements.
I have tried so far -

$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>";  
                 }

Please help me.
Thanks.

4
  • From where do you get the checked_posts array?Do you have in db a column with checked? Commented Mar 25, 2017 at 7:13
  • @Mihai From the name, my guess is it comes from checkboxes on a form. Commented Mar 25, 2017 at 7:14
  • @Mihai I get $checked_post array from another select query. Commented Mar 25, 2017 at 7:15
  • 2
    Then I suppose you only need a JOIN with the other field where you get the checked values,no need for php array manipulation Commented Mar 25, 2017 at 7:16

2 Answers 2

1

You can JOIN the checked IDs since they come from the db,no need for php operations

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
Sign up to request clarification or add additional context in comments.

4 Comments

@ Mihai In one db table i have stored ids like 1,5,7,15(comma separated) so how would I use join query?
@Deepak ..JOIN... ON FIND_IN_SET(sa_posts.ID,someoOtherTable.checkedIds)
@ Mihai It is working but showing only matched records.
@Deepak Use left JOIN and also add the FIND_IN_SET in the ORDER BY
1

You can do this by putting the check into the ORDER BY clause of the query.

$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
           ");   

You can also avoid doing the in_array() test in the PHP code by adding this to the query results:

$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
           ");   

Then the PHP code can just use

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.