can I return 2 sets of results from 2 different talbes from one function? if yes- how to? |
[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 firstname lastname Now, I am trying the following code in my model: Code: public function posts_that_match_search_term($search_term) { * 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
[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) { 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) { Hope that helps...???
[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?
[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
[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) { 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
[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
[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
[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: 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){ 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){ 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){ |
Welcome Guest, Not a member yet? Register Sign In |