views:

474

answers:

3

Hello, 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() {
//  $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

+1  A: 

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.

Lukman
+1  A: 

Either way, don't forget it's MVC and this kind of logic should be avoided.

Cinnamon
A: 

It's ok I was being a moron there was no reason for me to send the variable to a new view.

sico87