[英]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. 您将变量传递给模板,但是在left_menu视图中需要该变量,但没有将该变量提供给该视图。 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. 假设$ data ['list2']是一个数组。 Since an array should iterated even though it has a single value on each array key.
因为即使每个数组键上都有一个值,数组也应该迭代。 a foreach will solve your problem.
foreach可以解决您的问题。
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.