Welcome Guest, Not a member yet? Register   Sign In
Passing result set from model to controller to view. What is the best way to do it?
#1

Hello guys,

I am new to CI.

I have a query that can return zero or any number of rows, which is passed to the controller and
then the controller passed that to a view that will show the results.

The problem I have is that I would like to return from the model to the controller a 0 (and assign that to a vairable)
if there are no rows found or of course the result set if the query finds any matches in the db. However I cannot do this as
when I return it to the controller I have to assigned to an array in order to pass it to the view.
Otherwise I am not able to show the query results in the view later.

I am not show if I am clear enough with my explanation. Please see the code below.

Questions
1) is there a better way to do this?

2) is there a way to evaluate in the controller what is returned from the model before assigning that to an array?
(Bear in mind I tried but then I am not able to show the values in the view)

3) Can you please suggest any best practices to do this?

Thank you in advance!

Model

public function ps_search_serv($ps_search_str) {

$ps_q1=("SELECT S_SERVICES.comp_id, S_COMP.comp_name, S_SERVICES.serv_desc FROM S_SERVICES, S_COMP WHERE MATCH (serv_desc) AGAINST (? IN BOOLEAN MODE) AND S_SERVICES.comp_id=S_COMP.comp_id AND S_COMP.comp_active=1");
$ps_q1_rows=$this->db->query($ps_q1, array($ps_search_str));
if ($ps_q1_rows->num_rows() > 0){
$ps_q1return=$ps_q1_rows->result_array();
return $ps_q1return;
}
else {
return $ps_q1return=$ps_q1_rows->num_rows();
}
}

Controller

$ps_q1return['results']=$this->psite_model->ps_search_serv($ps_search_str);
$this->load->view('public/v_public_header');
$this->load->view('public/v_public_nav_mainpage');
$this->load->view('public/v_public_frontpage_publicsearch_results',$ps_q1return);
$this->load->view('public/v_public_frontpage_content_2');
$this->load->view('public/v_public_footer');


View

<?php
//var_dump($this->_ci_cached_vars);

foreach ($results as $row)
{
echo $row['comp_name'];
echo $row['serv_desc'];
echo br(2);
}

?>
Reply
#2

Just check for you returned value:

PHP Code:
$return $this->psite_model->ps_search_serv($ps_search_str);
$ps_q1return['results'] = ($return == 0) ? $return
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Thanks that works. I had done something similar before I posted the question and it did not work Smile.

is your expression above similar to the one below (I mean the idea)? (I am asking as I do not understand the use of "?") . If you could point me to any documentation about it that'd be great.

if (is_array($return)) {
echo "Pasamos un array a la vista"; echo br(2);
$ps_q1return['results']=$return;
$this->load->view('public/v_public_header');
$this->load->view('public/v_public_nav_mainpage');
$this->load->view('public/v_public_frontpage_publicsearch_results',$ps_q1return);
$this->load->view('public/v_public_frontpage_content_2');
$this->load->view('public/v_public_footer');
}
else {
echo "Pasamos un variable a la vista"; echo br(2);
$ps_q1return['results']=$return;
$this->load->view('public/v_public_header');
$this->load->view('public/v_public_nav_mainpage');
$this->load->view('public/v_public_frontpage_publicsearch_results',$ps_q1return);
$this->load->view('public/v_public_frontpage_content_2');
$this->load->view('public/v_public_footer');
}


Thanks a lot!
Reply
#4

(This post was last modified: 08-31-2015, 09:42 AM by rtorralba.)

(08-30-2015, 11:32 PM)estebanc Wrote: Hello guys,

I am new to CI.

I have a query that can return zero or any number of rows, which is passed to the controller and
then the controller passed that to a view that will show the results.

The problem I have is that I would like to return from the model to the controller a 0 (and assign that to a vairable)
if there are no rows found or of course the result set if the query finds any matches in the db.  However I cannot do this as
when I return it to the controller I have to assigned to an array in order to pass it to the view.
Otherwise I am not able to show the query results in the view later.

I am not show if I am clear enough with my explanation.  Please see the code below.

Questions
1) is there a better way to do this?

2) is there a way to evaluate in the controller what is returned from the model before assigning that to an array?
(Bear in mind I tried but then I am not able to show the values in the view)

3) Can you please suggest any best practices to do this?

Thank you in advance!

Model

public function ps_search_serv($ps_search_str) {
   
$ps_q1=("SELECT S_SERVICES.comp_id, S_COMP.comp_name, S_SERVICES.serv_desc FROM S_SERVICES, S_COMP WHERE MATCH (serv_desc) AGAINST (? IN BOOLEAN MODE) AND S_SERVICES.comp_id=S_COMP.comp_id AND S_COMP.comp_active=1");
$ps_q1_rows=$this->db->query($ps_q1, array($ps_search_str));
if ($ps_q1_rows->num_rows() > 0){
$ps_q1return=$ps_q1_rows->result_array();
return $ps_q1return;
}
else {
return $ps_q1return=$ps_q1_rows->num_rows();
}
   }

Controller

$ps_q1return['results']=$this->psite_model->ps_search_serv($ps_search_str);
$this->load->view('public/v_public_header');
$this->load->view('public/v_public_nav_mainpage');
$this->load->view('public/v_public_frontpage_publicsearch_results',$ps_q1return);
$this->load->view('public/v_public_frontpage_content_2');
$this->load->view('public/v_public_footer');


View

<?php
//var_dump($this->_ci_cached_vars);

   foreach ($results as $row)
   {
echo $row['comp_name'];
echo $row['serv_desc'];
echo br(2);
   }
   
   ?>

At model:


PHP Code:
public function ps_search_serv($ps_search_str) {
$sql = ("SELECT S_SERVICES.comp_id, S_COMP.comp_name, S_SERVICES.serv_desc FROM S_SERVICES,   S_COMP WHERE MATCH (serv_desc) AGAINST (? IN BOOLEAN MODE) AND S_SERVICES.comp_id=S_COMP.comp_id AND S_COMP.comp_active=1");

$query $this->db->query($ps_q1, array($ps_search_str));

if(
$query->num_rows() > 0)
  return $query->result_array();
else
  return array();


Is a good practice that the functions return the same type of data

Then at controller:


PHP Code:
$data['results']=$this->psite_model->ps_search_serv($ps_search_str); 

Finally at view:


PHP Code:
<? if(count($results) > 0): ?>
  <? foreach ($results as $result): ?>
    <p><?= $result->comp_name ?></p>
  <? endforeach ?>
<? else: ?>
  <p>No results Found</p>
<? endif ?>

With this way you know that results always is an array, somtimes empty array but always array
Greetings.
Reply
#5

Thanks a lot rtorralba!

Yes I agree with you too. It is just that I had trouble passing the empty array to the view and showing it. I will test your suggestion.

Thanks for the quick reply to both of you!
Reply
#6

(This post was last modified: 09-01-2015, 04:07 AM by InsiteFX.)

Comparison Operators

PHP Shorthand If/Else Using Ternary Operators (?Smile
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB