• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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




#2
[eluser]boltsabre[/eluser]
Well the way you have it set up currently is after your search queries, you're checking the result of $post_query before $post_query2
Code:
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;
}

You're returning either return array(); if no results were found, or $post_array;. This code will NEVER check $post_query2 as you are returning something before you get to it.
I'm not 100% sure what you're trying to achieve, but you could try something like this perhaps...???

Code:
if ($post_query->num_rows() == 0) {
    $return['post_q_1_empty'] = true;
    $return['post_q_1_full'] = false;
} else {
    $i = 0;

    foreach ($post_query->result() AS $row) {
        ...;
    }
    $return['post_q_1_empty'] = false;
    $return['post_q_1_full'] = $post_array;
}
if ($post_query2->num_rows() == 0) {
    $return['post_q_2_empty'] = true;
    $return['post_q_2_full'] = false;
} else {
    $a = 0;

    foreach ($post_query2->result() AS $row2) {
        ...
        $a++;
    }
    $return['post_q_2_empty'] = false;
    $return['post_q_2_full'] = $post_array2;
}
return $return
Basically you're just building your multiple return values into an array, and returning that array.

Hope that helps...???

#3
[eluser]zoreli[/eluser]
Hi

I tried this approach but i couldn't make it to work. Could be merging of array solve the problem? Anyone can point me how to do that?

#4
[eluser]boltsabre[/eluser]
okay, what do you get if you do it in the above way, if you're not getting any error messages try var_dump to see what you get, otherwise, can you tell us what the error message is?

Code:
//in your controller
$search_result = $this->my_model_name->posts_that_match_search_term($search_term);
var_dump($search_result);

//actually, you could just var_dump($return) from inside your model, it's up to you!

#5
[eluser]zoreli[/eluser]
[quote author="boltsabre" date="1340103802"]
You're returning either return array(); if no results were found, or $post_array;. This code will NEVER check $post_query2 as you are returning something before you get to it.
I'm not 100% sure what you're trying to achieve, but you could try something like this perhaps...???

Code:
if ($post_query->num_rows() == 0) {
    $return['post_q_1_empty'] = true;
    $return['post_q_1_full'] = false;
} else {
    $i = 0;

    foreach ($post_query->result() AS $row) {
        ...;
    }
    $return['post_q_1_empty'] = false;
    $return['post_q_1_full'] = $post_array;
}
if ($post_query2->num_rows() == 0) {
    $return['post_q_2_empty'] = true;
    $return['post_q_2_full'] = false;
} else {
    $a = 0;

    foreach ($post_query2->result() AS $row2) {
        ...
        $a++;
    }
    $return['post_q_2_empty'] = false;
    $return['post_q_2_full'] = $post_array2;
}
return $return
Basically you're just building your multiple return values into an array, and returning that array.

Hope that helps...???[/quote]

You nailed the problem, now I need to find the solution.

After commenting the part for the first query I dump the var_dump($post_array2);
die();
I get the result for the user. My result was displayed.

So I have to find the way how to implement the code that you posted above. For some reason the first time I make an attempt I get million error messages. I will now try again and post the errors here.

Regards,Zoreli

#6
[eluser]zoreli[/eluser]
Hi

In my view I placed this code:

var_dump($search_results_returned);

and I get this:

array(4) { ["post_q_1_empty"]=> bool(true) ["post_q_1_full"]=> bool(false) ["post_q_2_empty"]=> bool(false) ["post_q_2_full"]=> array(1) { [0]=> array(7) { ["user_id"]=> string(2) "34" ["user_name"]=> string(4) "John" ["user_username"]=> string(6) "z0reli" ["user_image_filename"]=> string(13) "z0reli_32.png" ["user_first_name"]=> NULL ["user_last_name"]=> NULL ["user_email"]=> string(20) "[email protected]" } } }

Data that I am getting is correct, I have to fix my view.

Thanks for the help.

Zoreli

#7
[eluser]zoreli[/eluser]
Earlier in my view my check was the following:

if(count($search_results_returned) == 0)....
now that condition is not true, since when searching for user, count is not 0...how can I perform the check now having in mind that data I am getting is:

array(4) { [“post_q_1_empty”]=> bool(true) [“post_q_1_full”]=> bool(false) [“post_q_2_empty”]=> bool(false) [“post_q_2_full”]=> array(1) { [0]=> array(7) { [“user_id”]=> string(2) “34” [“user_name”]=> string(4) “John” [“user_username”]=> string(6) “z0reli” [“user_image_filename”]=> string(13) “z0reli_32.png” [“user_first_name”]=> NULL [“user_last_name”]=> NULL [“user_email”]=> string(20) “[email protected]” } } }

Any help will be deeply appreciaed, I am staring at this code over 14 hours now and start to loosing the ball here ...

Regards,Zoreli

#8
[eluser]boltsabre[/eluser]
Sorry, been offline the last 12 or so hours.

Right, so it looks like we're getting the result from your model (as an array, contain 4 values), and it's being passed to your view from your controller yes?

Okay, it should be pretty simple...you have an array in your view that you want to access, looking like this:
Code:
$search_results_returned; //it's an array, containing:
$search_results_returned['post_q_1_empty'] //value = bool(true)
$search_results_returned['post_q_1_full'] //value = bool(false)
$search_results_returned['post_q_2_empty'] //value = bool(false)
$search_results_returned['post_q_2_full'] //value = array (of your search result data.

To do anything, you have to access the key of $search_results_returned that you want to check.

For example, if you wanted to check if 'post_q_1_empty' was true or false in your view you'd do it like this:
Code:
if($search_results_returned['post_q_1_empty'] == false){
   //do something
}else{
   //do something else
}

To check if you have a search result, you need to check if either both 'post_q_1_empty' and 'post_q_2_empty' == true, or if either post_'q_1_full' and 'post_q_2_full'!= false, like this:
Code:
if($search_results_returned['post_q_1_empty'] == true && $search_results_returned['post_q_2_empty'] == true){
   //no search results were returned
}

...or...
if($search_results_returned['q_1_full'] != false || $search_results_returned['q_2_full'] != false){
   //we have a search result in one of them...
}

To access your search result 'post_q_2_full', I'm pretty sure this will work:
Code:
if($search_results_returned['post_q_1_empty'] != false){
   foreach ($search_results_returned['post_q_1_empty'] as $row){
      echo $row->user_image_filename;
   }
}
[/code]


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.