Welcome Guest, Not a member yet? Register   Sign In
undefined variable (newbie)
#11

[eluser]John_Betong[/eluser]
 
Hi WireDesingz,

// John is still in CI 1.5.4 land, CI 1.6 buffers output
// so concatenating views into a variable is no longer needed.
True but what versions are the posters using.

// @John: your'e adding the views into a variable named $result
// and then echoing a variable named $output, thats just weird dude Tongue
Whoops, you spotted my deliberate mistake, I blame it on my medication Smile
I have now amended the incorrect code Smile

Looking forward to feedback from the original posters to see if it worked.

Cheers,

John
 
 
 
#12

[eluser]jorre[/eluser]
wiredesignz, here is my view code:

Code:
<?=$heading;?>

<?=form_open('poll/create_step2'); ?>

Option1: &lt;input type="text" name="poll_choice1" /&gt;<br />
Option2: &lt;input type="text" name="poll_choice2" /&gt;

&lt;input type="submit" value="Save poll" /&gt;
&lt;/form&gt;
#13

[eluser]jorre[/eluser]
ok and here is the full controller:

Code:
&lt;?php
      class Poll extends Controller
    {
        function Poll()
        {
            parent::Controller();
        }
        
        function index()
        {
            $this->load->view('pollview');
        }
        
        function create()
        {
        
            $this->load->helper(array('form', 'url'));
            $this->load->library('validation');
            
            //set validation rules
            $rules['poll_topic'] = "trim|required";
            $rules['poll_yourname'] = "trim|required";
            $rules['poll_youremail'] = "trim|valid_email";
            $this->validation->set_rules($rules);
            
            //set default names for display in error message
            $fields['poll_topic'] = 'Poll topic';
            $fields['poll_yourname'] = 'Your name';
            $fields['poll_youremail'] = 'Your email address';
            $this->validation->set_fields($fields);
            
            //apply custom error formatting
            $this->validation->set_error_delimiters('<div class="error">', '</div>');
            $this->validation->set_message('required', '%s is required');
                        
            //run form validation check            
            if($this->validation->run() == FALSE)
            {
                //SHOW STEP 1        
                $this->load->view('header');    
                $this->load->view('pollcreate_step1');    
                $this->load->view('footer');    
            }
            else
            {                
                //STORE DATA FROM STEP 1 IN A SESSION
                $this->load->library('session');
                $step1data = array(
                   'poll_topic' => $this->input->post('poll_topic'),
                   'poll_yourname' => $this->input->post('poll_yourname'),
                   'poll_youremail' => $this->input->post('poll_youremail')
               );
                $this->session->set_userdata($step1data);
                
                //SHOW STEP 2
                $this->load->view('header');    
                $this->load->view('pollcreate_step2');    
                $this->load->view('footer');
            }
        }
        
        
        function create_step2()
        {
            
            
            $this->load->helper(array('form', 'url'));
            $this->load->library('validation');
            //set validation rules
            $rules['poll_choice1'] = "trim|required";
            $rules['poll_choice2'] = "trim";    
            $this->validation->set_rules($rules);
            
            //set default names for display in error message
            $fields['poll_choice1'] = 'At least one option must be available in any poll';
            $this->validation->set_fields($fields);
                        
            //apply custom error formatting
            $this->validation->set_error_delimiters('<div class="error">', '</div>');
            $this->validation->set_message('required', '%s');
            
            //run form validation check            
            if($this->validation->run() == FALSE)
            {
                //SHOW STEP 2
                $data['title'] = "My Real Title";
                $data['heading'] = "My Real Heading";        
                
                $this->load->view('header');        
                $this->load->view('pollcreate_step2',$data);    //reload step 2
                $this->load->view('footer');    
            }
            else
            {
                //LOAD THE DATABASE MODEL AND INSERT THE NEW POLL
                $this->load->model('Poll_model','', TRUE);
                $this->Poll_model->add_poll_options();
                
                $this->load->view('header');    
                $this->load->view('messages/poll_success');    //show success message
                $this->load->view('footer');    
            }
                        
        }
                
        
    }
?&gt;
#14

[eluser]wiredesignz[/eluser]
It's quite simple, you have $data['heading'] = "My Real Heading"; only inside the Validation loop.

I don't see it anywhere else and this will cause the problems.

At least define it in the main script somewhere too, even if it's $data['heading'] = "";
#15

[eluser]jorre[/eluser]
thanks for the reply again, unfortunately my error is still there..

I now changed my code to the following so that $data is available in the main function, and not only in the validation loop, without success:

Code:
function create_step2()
        {
            
            
            $this->load->helper(array('form', 'url'));
            $this->load->library('validation');
            //set validation rules
            $rules['poll_choice1'] = "trim|required";
            $rules['poll_choice2'] = "trim";    
            $this->validation->set_rules($rules);
            
            //set default names for display in error message
            $fields['poll_choice1'] = 'At least one option must be available in any poll';
            $this->validation->set_fields($fields);
                        
            //apply custom error formatting
            $this->validation->set_error_delimiters('<div class="error">', '</div>');
            $this->validation->set_message('required', '%s');
            
            //load data to pass to views
            $data['title'] = "My Real Title";
            $data['heading'] = "My Real Heading";    
            
            
            //run form validation check            
            if($this->validation->run() == FALSE)
            {
                //SHOW STEP 2
                    
                
                $this->load->view('header');        
                $this->load->view('pollcreate_step2',$data, TRUE);    //reload step 2
                $this->load->view('footer');    
            }
            else
            {
                //LOAD THE DATABASE MODEL AND INSERT THE NEW POLL
                $this->load->model('Poll_model','', TRUE);
                $this->Poll_model->add_poll_options();
                
                $this->load->view('header');    
                $this->load->view('messages/poll_success');    //show success message
                $this->load->view('footer');    
            }
                        
        }
#16

[eluser]jorre[/eluser]
SOLVED:

the problem was that I called a view , the first time, without passing any data...
just like you said, I guess I was just looking at the wrong place in my code.

Thanks all for pointing me out in the right direction, I'm starting to get a grip on how CI is handling variables between controller & views Smile
#17

[eluser]langithitam[/eluser]
I have same problem..

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: result

Filename: models/jobs_model.php

Line Number: 9

Code:
&lt;?php
class Jobs_model extends Model {
  function get_jobs() {
    $query = $this->db->get('jobs');
    foreach ($query->result_array() as $row)
    {
      $result[] = $row;
    }
    return $result; <-- this is line 9
  }
  
  function get_job($job_id) {
    $query = $this->db->where('id', $job_id)->get('jobs');
    $result = $query->row_array();
    return $result;
  }
}

what the problem with my result? I'm stuck guys..
#18

[eluser]xwero[/eluser]
You don't need the loop as result_array will return the rows as you want them.

The reason why result isn't known is because the jobs table is empty.

A rule of thumb: if there is a possibility the variable will not be set along the way set it with the correct type as high up as possible.
Code:
$result = array();
$query = $this->db->get('jobs');

    foreach ($query->result_array() as $row)
    {
      $result[] = $row;
    }

    return $result;
But don't use this snippet use
Code:
function get_jobs() {
    $query = $this->db->get('jobs');
    return $query->result_array();
}
#19

[eluser]langithitam[/eluser]
[quote author="xwero" date="1237900160"]You don't need the loop as result_array will return the rows as you want them.

The reason why result isn't known is because the jobs table is empty.

A rule of thumb: if there is a possibility the variable will not be set along the way set it with the correct type as high up as possible.
Code:
$result = array();
$query = $this->db->get('jobs');

    foreach ($query->result_array() as $row)
    {
      $result[] = $row;
    }

    return $result;
But don't use this snippet use
Code:
function get_jobs() {
    $query = $this->db->get('jobs');
    return $query->result_array();
}
[/quote]

Thanks, stupid me. U right dude, that because the table is empty.
Is there anyway to sent a message in the view, to tell us that the table is empty.?
#20

[eluser]xwero[/eluser]
It all depends on where you want to put the code. If you want only variables if possible in your views you can check the model method output in your controller and add the message there
Code:
$data['rows'] = $this->model->get_jobs();

if(count($data['rows']) == 0)
{
   $data['message'] = "No jobs today, it's the economy.";
}
But you can do this in your view too
Code:
&lt;?php if(empty($rows)): ?&gt;<p>No jobs today, it's the economy.</p>&lt;?php endif ?&gt;
If you do it in your view you don't have to deal with the message variable you pass to the view.




Theme © iAndrew 2016 - Forum software by © MyBB