Welcome Guest, Not a member yet? Register   Sign In
Why can't I have undefined variables in CI?
#1

[eluser]ckelly06[/eluser]
I've been coding in php for a while but am brand new to CodeIgniter and am having a big problem with undefined variables. I've scoured Google for the answer thinking a million people would have already asked this question, but surprisingly I found nothing, so here it is:

Why can't I have undefined variables in my code?

I'm building a two page form: the first collects zip code and the second gets the rest of the user's information. Users can get to the second page either through the first page or directly, so there is not always a $_POST variable when they're on the second page. Both pages have some hidden fields to collect things like "keyword" and "creative_id" from Google advertising traffic.

With flat php, populating those hidden fields was simply a matter of echoing $_GET items on the first page (since Google appends these to the URL) and $_POST items on the second page; if those variables didn't exist the fields would just be blank, which is exactly what I wanted. Very easy!

With CI, I get errors in my controller and view unless those variables are defined in every instance, so even if a user organically navigates to the second page of my form (i.e. didn't arrive on my site by clicking an ad) I need to create a keyword variable in my controller (assigning its value as ' ') and pass it to my view. As a result, my controller has become bloated with if statements and empty variables so that no matter how someone arrives on my form, the hidden variables are defined and passed to the view.

This doesn't make sense to me. If the variable doesn't exist, why can't CI simply ignore it the way flat php does? Is there some setting to make it do that? If so, I could go back to simply echoing those variable names, and if they exist "great!" they show up, and if they don't "who cares?" and those hidden fields are blank.

This is basically what the controller for the second page of the form looks like. I made some comments to show where I'm running into the undefined variable issue.

Any help is greatly appreciated. I'm tearing my hair out over this! :ahhh:

Code:
class Pagetwo extends CI_Controller {

public function index()
{
  $this->load->helper(array('form', 'url'));
  $this->load->library('form_validation');
  
  if ($this->input->post())
  {
   //do a bunch of stuff with the variables from page 1.
   //in this case i know the variables exist and i need to do work on them
  }

  else
  {
   // create a bunch of blank items in the data array and pass them to the view.
   // this seems incredibly pointless to me, but i don't know what else i can do
   // that won't generate undefined variable errors.

   $data = array
   (
    'hidden_var1' => '',
    'hidden_var1' => '',
    'hidden_var1' => '',
    
   );

   $this->load->view('Pagetwo',$data);
  }
}

public function validate()
{
  // i have to declare those hidden values here and assign them to the $data array
  // to pass back to the Page2 view or else i get errors, but i don't understand why.
  // they're already part of the $_POST array, so why do i need to pass them as part of
  // $data?
}
}
#2

[eluser]CroNiX[/eluser]
"flat php" won't ignore them either if the error reporting is set to display E_NOTICE. You have your CI set up to show "notices", so it is. While it's not mandatory to define variables before using them in PHP, it really is a best practice in just about all languages. In just about every job I've had, the app had to run with NO notices/warnings/errors, etc, when full error reporting was turned on. If you want to hide these, turn off displaying E_NOTICE.
#3

[eluser]ckelly06[/eluser]
Thanks Cronix. So it sounds like I was just doing it the wrong way before and getting away with it. In that case, I'm thinking it probably makes sense to define all these variables once as part of the class, and then manipulate them with functions, rather than defining them in each function. I've never really workd with php classes before, so I guess I need to read up on that.




Theme © iAndrew 2016 - Forum software by © MyBB