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?
#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


Messages In This Thread
RE: Passing result set from model to controller to view. What is the best way to do it? - by rtorralba - 08-31-2015, 09:36 AM



Theme © iAndrew 2016 - Forum software by © MyBB