Welcome Guest, Not a member yet? Register   Sign In
can I return 2 sets of results from 2 different talbes from one function? if yes- how to?
#1

[eluser]zoreli[/eluser]
Hi

I have a search in which visitor can search for posts or user name or user name e-mail.

My tables structure is something like:

Posts

post_id
user_id
post
post_date
visible


Users

userid
username
email
firstname
lastname

Now, I am trying the following code in my model:


Code:
public function posts_that_match_search_term($search_term) {

//this query get the posts and user that post specific post

$post_query = $this->db->query("SELECT * FROM posts,users WHERE post_message LIKE '%$search_term%' AND post_user_id=user_id  ORDER BY post_id DESC", array());

// this query get the users which has username or e-mail LIKE the search term

$post_query2 = $this->db->query("SELECT * FROM users WHERE user_username LIKE '%$search_term%' OR user_email LIKE '%$search_term%'  ORDER BY user_id DESC", array());


if ($post_query->num_rows() == 0) {
    return array();
} else {
    $i = 0;

    foreach ($post_query->result() AS $row) {
        $post_array[$i]["post_id"] = $row->post_id;
        $post_array[$i]["post_message"] = $row->post_message;

        $post_array[$i]["post_datetime"] = $row->post_datetime;
        $post_array[$i]["post_completion_status"] = $row->post_completion_status;
        $post_array[$i]["post_completion_date"] = $row->post_completion_date;
        $post_array[$i]["post_completion_notes"] = $row->post_completion_notes;
    }

    return $post_array;
}

if ($post_query2->num_rows() == 0) {
    return array();
} else {
    $a = 0;

    foreach ($post_query2->result() AS $row2) {

        $post_array2[$a]["user_id"] = $row2->user_id;
        $post_array2[$a]["user_name"] = $row2->user_name;
        $post_array2[$a]["user_username"] = $row2->user_username;
        $post_array2[$a]["user_image_filename"] = $row2->user_image_filename;
        $post_array2[$a]["user_first_name"] = $row2->user_first_name;
        $post_array2[$a]["user_last_name"] = $row2->user_last_name;
        $post_array2[$a]["user_email"] = $row2->user_email;





        $a++;
    }

    return $post_array2;
}
}

* note = code is shortened on some places for sake of simplicity */

Both queryes works and return results if I run them one by one. That is if I comment the first query and all the code after if ($post_query->num_rows() == 0) { then second part of code works normally and return results. Same is true for opposite.

When both are not commented, I am getting no error, just 0 results.

Any help will be deeply appreciate.

Regards, Zoran





Messages In This Thread
can I return 2 sets of results from 2 different talbes from one function? if yes- how to? - by El Forum - 06-19-2012, 02:53 AM



Theme © iAndrew 2016 - Forum software by © MyBB