I am attempting to count the amount of rows of my table that meet specific criteria. My code works fine and returns the amount of rows accordingly. But i am getting problem to echo time of rows in my opinion

my controller:

function index(){
  $data['states'] = $this->state_model->get_cities();
  $data['status'] = $this->state_model->get_status();

  $this->load->view('testviewad', $data);
}

in second the road the amount of rows are saved. basically do

<?php echo $status?>

in my opinion it shows

  0
  0
  0
  0

However i should show

  0
  1
  3
  0

basically echo in the model the counted quantity of the rows shows properly. So my totally working. However i think i am making mistake while passing towards the view. Can anyone let me know what i am doing wrong.

my model:

function get_status(){
  $states = $this->db->get('state');
  $like = array();

  foreach ($states->result() as $state){
    $cities = $this->db->get_where('city', array('state_id'=> $state->id));
    foreach ($cities->result() as $v){ 
      $s=$v->city_id."<br/>";
      $this->db->where('city_city_id',$v->city_id);
      $this->db->where('status_status_id',1);
      $q=$this->db->get('info');
      $sql =  $q->num_rows();  
    }
    return $sql;
  }
}   

I am using codeigniter. Thanks ahead of time.

My prediction is the fact that $data['status'] gets overwritten each iteration, departing the need for 0 when it is passed towards the view.

It's difficult to inform exactly without seeing the appropriate code "in context", but you could do this this:

$data['states'] = $this->state_model->get_cities();
$data['status'][] = $this->state_model->get_status(); // adding brackets - now $status will be an array

Then you may loop through $status within the view:

foreach ($status as $s) 
{
    echo $s . "\n";
}

UPDATE

After looking at more code. It appears you are overwriting $sql within the get_status() model method. Change that for this:

function get_status(){
  $states = $this->db->get('state');
  $like = array();

  foreach ($states->result() as $state){
    $cities = $this->db->get_where('city', array('state_id'=> $state->id));
    foreach ($cities->result() as $v){ 
      $s=$v->city_id."<br/>";

      $this->db->where('city_city_id',$v->city_id);
      $this->db->where('status_status_id',1);
      $q=$this->db->get('info');
      $sql[] =  $q->num_rows();  // add the array brackets
    }
  }
  // move this down outside of the first foreach loop
  return $sql; // now $sql is an array of values
}

Then, your call to $data['status'] = $this->state_model->get_status(); returns an assortment and may be looped through within the view:

foreach ($status as $s) 
{
    echo $s . "\n";
}