我正在开发一个codeigniter网站,其中一个页面是使用多个视图构建的,我已经到了一个点,我需要使用另一个视图中设置的变量是否可以从另一个视图中访问它查看?
这是我的代码
控制器:
public function index() {
// $this->output->enable_profiler(TRUE);
$data = array();
if($query = $this->category_model->get_all_online()) {
$data['main_menu'] = $query;
}
$this->load->model('image_model');
/*
* Sort out the users backgrounds, basically do a check to see if there is a 'special' background
* if there is not a 'special' background then IF the user is logged in and has a background of there
* own show that one, if not show a generic one, if they are not logged in show a bang one
*/
$image = array();
if ($query = $this->image_model->get_special_backgrounds()) {
$image['special'] = $query;
} elseif(!isset($image['special']) && !isset($this->session->userdata['user_id'])) {
if($query = $this->image_model->get_mysite_background()) {
$image['generic'] = $query;
}
}
if(isset($this->session->userdata['user_id'])) {
if($query = $this->image_model->get_user_backgrounds($this->session->userdata['user_id'])) {
$image['user_background'] = $query;
}
}
$data = array_merge($data, $image);
$this->load->view('home/main_page.php', array_merge($data, $image));
}
型号:
public function get_mysite_background() {
$this->db->select('*');
$this->db->from('background');
$this->db->where('users_user_group_group_id', 1);
$this->db->where('is_special', 0);
$query = $this->db->get();
return $query->result_array();
}
public function get_special_backgrounds() {
$this->db->select('*');
$this->db->from('background');
$this->db->where('is_special', 1);
$query = $this->db->get();
return $query->result_array();
}
public function get_user_backgrounds($user_id) {
$this->db->select('*');
$this->db->from('background');
$this->db->where('users_user_id', $user_id);
$this->db->where('is_special', 0);
$query = $this->db->get();
return $query->result_array();
}
问题中的观点:首先是变量设置的位置,
</div>
<div id="background-select">
<?php
$count = 0;
if(isset($special)) {
foreach ($special as $row) {
$count ++;
print '<div class="select">';
print "<a href='index.php/home/category/".$row['background_id']."'>$count</a>";
print '</div>';
if($count = 1) {
$background = $row['background_name'];
}
}
}
if(isset($generic)) {
foreach ($generic as $row) {
$count ++;
print '<div class="select">';
print "<a href='index.php/home/category/".$row['background_id']."'>$count</a>";
print '</div>';
if($count = 1) {
$background = $row['background_name'];
}
}
}
if(isset($user_background)) {
foreach ($user_background as $row) {
$count ++;
print '<div class="select">';
print "<a href='index.php/home/category/".$row['background_id']."'>$count</a>";
print '</div>';
if($count = 1) {
$background = $row['background_name'];
}
}
}
?>
我想在哪里使用变量:
<body>
<div id="wrapper" style=<?php echo"background:url(/media/uploads/backgrounds/".$background.");";?>>
我希望使用$background
答案 0 :(得分:1)
您可以使用以下帮助程序类将值从一个视图传输到另一个视图:
class VarStore {
private static $values = array();
public static function get($key, $default = NULL) {
return (isset(self::$values[$key])? self::$values[$key] : $default);
}
public static function set($key, $value) {
self::$values[$key] = $value;
}
}
在第一个视图中调用VarStore::set('background', $background);
,在第二个视图中调用$background = VarStore::get('background');
。
答案 1 :(得分:0)
编程中的大多数问题都是概念性的和理解的......重点是视图是查看数据而不是计算数据。计算在模型中进行,其功能可在需要时由多个模块调用。所以更好的是在一个视图被称为计算出来之前它必须显示的所有数据并使用视图进行查看而不是扼杀......我希望这种范式的改变会有所帮助