Adding Dynamic Data To Views - wolfgang1983 - 01-18-2017
When I add my data the first $data array gets block by second data array
And I am unable to view the data in the first array
Any idea's whats causing it and how to solve it.
Code: public function index($post_id) {
$post_data = $this->post_model->getpost($post_id);
$div_id = 1;
// First Array
$data = array(
'title' => 'Welcome to CodeIgniter',
'islogged' => $this->user_model->is_logged(),
'username' => $this->user_model->getusername()
);
// Second Array
$data = array(
'post_user_id' => $this->user_model->getuserid(),
'post_title' => $post_data['subject'],
'post_message' => $this->parsedown->text($post_data['message']),
'post_id' => $post_data['post_id'],
'post_div_id' => $div_id,
'post_votes' => $post_data['votes']
);
$data['forum_id'] = $post_data['forum_id'];
$data['page'] = 'forum/post';
$this->load->view('default', $data);
}
Views > default.php
PHP Code: <?php $this->load->view('template/common/header');?><?php $this->load->view('template/common/navbar');?> <?php $this->load->view('template/' . $page);?> <?php $this->load->view('template/common/footer');?>
RE: Adding Dynamic Data To Views - enlivenapp - 01-18-2017
You're just overwriting the first array
There's a couple ways to solve it...
either:
PHP Code: // First Array $data = array( 'title' => 'Welcome to CodeIgniter', 'islogged' => $this->user_model->is_logged(), 'username' => $this->user_model->getusername(), 'post_user_id' => $this->user_model->getuserid(), 'post_title' => $post_data['subject'], 'post_message' => $this->parsedown->text($post_data['message']), 'post_id' => $post_data['post_id'], 'post_div_id' => $div_id, 'post_votes' => $post_data['votes'] );
or something like
PHP Code: // First Array $data['array1'] = array( 'title' => 'Welcome to CodeIgniter', 'islogged' => $this->user_model->is_logged(), 'username' => $this->user_model->getusername() );
// Second Array $data['array2'] = array( 'post_user_id' => $this->user_model->getuserid(), 'post_title' => $post_data['subject'], 'post_message' => $this->parsedown->text($post_data['message']), 'post_id' => $post_data['post_id'], 'post_div_id' => $div_id, 'post_votes' => $post_data['votes'] );
or...
PHP Code: // First Array $array1 = array( 'title' => 'Welcome to CodeIgniter', 'islogged' => $this->user_model->is_logged(), 'username' => $this->user_model->getusername() );
// Second Array $array2 = array( 'post_user_id' => $this->user_model->getuserid(), 'post_title' => $post_data['subject'], 'post_message' => $this->parsedown->text($post_data['message']), 'post_id' => $post_data['post_id'], 'post_div_id' => $div_id, 'post_votes' => $post_data['votes'] );
// then array_merge()...
Anytime you already have a variable (whether it's an array, string, int, etc) if you define it again, the first 'version' of it is overwritten.
RE: Adding Dynamic Data To Views - wolfgang1983 - 01-19-2017
Thanks this way works fine
PHP Code: public function index($post_id) { $post_data = $this->post_model->getpost($post_id);
$div_id = 1;
$general = array( 'title' => 'Welcome to CodeIgniter', 'islogged' => $this->user_model->is_logged(), 'username' => $this->user_model->getusername() );
$post = array( 'post_user_id' => $this->user_model->getuserid(), 'post_title' => $post_data['subject'], 'post_message' => $this->parsedown->text($post_data['message']), 'post_id' => $post_data['post_id'], 'post_div_id' => $div_id, 'post_votes' => $post_data['votes'], );
$data = array_merge($general, $post);
$data['forum_id'] = $post_data['forum_id'];
$data['page'] = 'forum/post';
$this->load->view('default', $data); }
|