开发者

Multiple views in codeigniter

I am working on a codeigniter site, in which a page is built using multiple views, I have come to a point where I need to work with a variable that is being set in another view is it possible to access it from another view?

This is code I have

Controller:

    public function index() {开发者_Go百科
//  $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));
}

Model:

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();
}

The Views in Question: Firstly where the variable is getting set,

</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'];
            }
        }
    }
?>

And where I want to use the variable:

<body>
<div id="wrapper" style=<?php echo"background:url(/media/uploads/backgrounds/".$background.");";?>>

The variable I looking to use $background


You can use the following helper class to transfer the values from one view to another:

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;
   }

}

Call VarStore::set('background', $background); in the first view and $background = VarStore::get('background'); in the second.


Most of the problems in programming are conceptual and of understanding... The point is a View is to VIEW the data and not calculate it. calculations are carried out in models whose functions can be called by multiple modules when required. So better is before a view is called work-out all the data it has to display and use the view for VIEWING only not callucations... I hope this change of paradigm will help

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜