简体   繁体   中英

send a variable to the view with codeigniter

I have a function, that count the visits of my web site, I tried to send a variable into the view but it doesn't work, the variable has to pass trought a file that contents the templates. When the variable I pass directly to the view it works, but other way doesn't. Please help me. Thanks.

in the controller

$query = $object->Searcher_visits();
if ($query->num_rows > 0) {
while ($query->result()) {
$current_date = $list['date'] = $query->date; 
$count = $obj_forum->visits($current_date);
$list2['num'] = $count->num;
$list2['current_date'] = $current_date;
}
}
$data['list2'] = $list2;
$data['list'] = $lis;
$this->load->view('template/general_template/template', $data);

in the view (this view is the left menu)

<table border="1" width="250px" cellpading="5px" cellspacing="5px">';
<tr><td>FECHA</td><td>VISITAS</td></tr>';
tr><td><?php echo $list2['current_date']; ?></td>
<td align="right"><?php echo $list2['num'];?></td>
</tr>
</table>

the template

<?php
$this->load->view('header');
$this->load->view('banner');
$this->load->view('left_menu');
?>

You pass the variable to your template, but you need the variable in the left_menu view, but you don't give the variable to that view. A quick fix would be just pass the variable along from within your template:

<?php
$data = array($list1, $list2);
$this->load->view('header');
$this->load->view('banner');
$this->load->view('left_menu', $data);
?>
//Controller code

$query = $object->Searcher_visits();
if ($query->num_rows > 0){
   $data['list2'] = $query->result();
   $data['list'] = $obj_forum;
}
$this->load->view('header');
$this->load->view('banner');
$this->load->view('left_menu',$data);

//view page

<table border="1" width="250px" cellpading="5px" cellspacing="5px">
  <tr>
     <td>FECHA</td>
     <td>VISITAS</td>
  </tr>
  <?php foreach($list2 as $ex2){?>
  <tr>
      <td><?php echo $ex2->date; ?></td>
      <td align="right"><?php echo $list->visits($ex2->date);?></td>
  </tr>
<?php }?>
</table>

Assuming that $data['list2'] is an array. Since an array should iterated even though it has a single value on each array key. a foreach will solve your problem.

 <table border="1" width="250px" cellpading="5px" cellspacing="5px">'; <tr><td>FECHA</td><td>VISITAS</td></tr>'; tr><td><?php echo $list2['current_date']; ?></td> <td align="right"><?php echo $list2['num'];?></td> </tr> </table> 


should be like this:

 <table border="1" width="250px" cellpading="5px" cellspacing="5px">; <tr> <td>FECHA</td> <td>VISITAS</td> </tr>; <?php foreach($list2 as $list):?> <tr> <td> <?= $list->current_date ?> </td> <td align="right"> <?=$list->num?> </td> <?php endforeach;?> </tr> </table> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM