Welcome Guest, Not a member yet? Register   Sign In
$data variable not recognised until after refresh when passing array to view
#1

[eluser]rossmurphy[/eluser]
I am passing the $data variable to my view page like so..
Code:
$data['Counter'] = 0;
        
$this->load->view('header');
$this->load->view('members/memberhome', $data);
$this->load->view('footer');

When my view loads, i get a PHP error...

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: members/memberhome.php

Line Number: 23

But when i refresh the page the variable is recognised... and ideas?
#2

[eluser]gtech[/eluser]
can we have the view code....

in the view you should print out counter like so:
Code:
<?=$Counter?>
#3

[eluser]jedd[/eluser]
Hi Ross, and welcome to the CI forums.

Have a re-read of the CI User Guide, specifically the bit about views.

When you pass $data to a view, you don't actually get a $data variable at the view end. Instead, you get the exploded contents, as it were, of the $data array.

This is why you were right to have this:
Code:
$data['Counter'] = 0;

But when it gets to the view, you will now have a variable called $Counter

Absolutely no idea why a refresh would give you some value, especially without seeing code, but I think we should politely ignore this anomaly and just get it working consistently first.
#4

[eluser]Dam1an[/eluser]
I would also recommend you have your variable names all lower case, as per the style guide
#5

[eluser]rossmurphy[/eluser]
I understand the way array's are exploded. My fault, i should have been thinking and paste ALL of my code.. so here it is:

This is my model.

Code:
<?php

class bingorooms extends Model {
    
    function bingorooms()
    {
        parent::Model();        
    }
    
    function index()
    {

        // show bingo games on the member home page
        $xmlCall = api_call('bingorooms');
        
        // set error token and check if it contains any errors
        $data['error'] = $xmlCall->errors->error['token'];
        
        if(empty($data['error']) and $data['error'] == '') {
        
            // set session variables
            $xmlCall = $xmlCall->rows->xpath('row[@nextGameStarting>0]');
            
            }
    
        return $xmlCall;
    
    }
}
?>

This is my controller.

Code:
<?php
class memberhome extends Controller {
    
    function memberhome()
    {
        parent::Controller();        
    }
    
    function index()
    {
        // check if user is authenticated else redirect to login with error
        if($this->session->userdata('authenticated') != 'true') {
            $this->session->set_flashdata('error', 'error.session.expired');
            redirect('login/index');
        }
                
        $data['bingocounter'] = 0;
        
        // load the BINGOROOMS model
        $this->load->model('bingorooms');
        $data['bingorooms'] = $this->bingorooms->index();
        
        // load the BINGOLAUNCH model
        $this->load->model('bingolaunch');
        $data['bingoroomslaunch'] = $this->bingolaunch->index();
        
        // load the GETBALANCE model
        $this->load->model('getbalance');
        $data['balance'] = $this->getbalance->index();

        // load the views
        $this->load->view('header_member');
        $this->load->view('templates/member_menutop', $data);
        $this->load->view('members/memberhome', $data);
        $this->load->view('footer');
        
    }        
}
?>

This is my view.

[code]
<div id="content">
            &lt;?php
            foreach ($bingorooms as $row) {
            $bingocounter++;
                
                foreach($bingoroomslaunch as $form) {
                    echo $form;
                }
    
            }
            ?&gt;
</div>

Please correct me if this is the wrong way to go about doing any of this.
[/code]
#6

[eluser]jedd[/eluser]
Okay, it looks like in your model you are assuming that $data is the same as $this->data. I think this is probably your biggest conceptual problem, judging by your code. The most obvious impact of this is that when you talk about $data in your model, you're not talking about what you think you are talking about.

It also looks like you're treating $data as a kind of globally-scoped variable, which is a style and/or logic problem.

For data you need to refer to in the model, you should pass it to the model's method explicitly as parameters. Similarly, any returned data should be returned (rather than set in a global variable or facsimile of same).

Can I also suggest that within your controller - if you are going to use one or more of your models throughout pretty much every method, put the ->load->model() calls in the controller's constructor.

Otherwise, put their load call in a labelled section at the start of each method - it'll make it easier for you to read.

You may also want to consolidate some of your models - bingorooms and bingolaunch may both be easier for you if they are called rooms() and launch() methods and reside in your Bingo() model (for example).
#7

[eluser]rossmurphy[/eluser]
That was great advice, will really help clean up my code a lot. Thanks jedd!
#8

[eluser]rossmurphy[/eluser]
if i have a function that queries an api then receives xml results, and it is a common function used by nearly every controller in my app, should this function be in a model?

Thanks.
#9

[eluser]gtech[/eluser]
I would suggest a library or a helper, or inherit(Extend) from a base controller, a model in therory is there as an interface to the database but as CI is a loose framework putting it a valid solution if it makes sense to you.
#10

[eluser]rossmurphy[/eluser]
is it frowned upon to load a model in a helper?




Theme © iAndrew 2016 - Forum software by © MyBB