Welcome Guest, Not a member yet? Register   Sign In
Adding Dynamic Data To Views
#1

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');?>
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

(This post was last modified: 01-18-2017, 09:02 PM by enlivenapp. Edit Reason: int, not init and commas in arrays... who knew? )

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.
Reply
#3

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);
    
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB