Problem with data sent to the view in Codeigniter

Hello I have this function code in PHP Code igniter where I am trying to test if the data returned to the view is true or false.

function createuser(){
		$username = $this->input->post('username',false);
		if ($username == '') {
			$data['u'] = false;
			$this->load->view('loginview', $data);
		}
		else{
			$data['u'] = true;
			$this->questionsmodel->setuser($username);
			$this->load->view('loginview', $data);
		}
	}

Here is the code in the view, that should display the button to begin the quiz if the data is true.

if ($u == true){
  ?>
    <form action="../../index.php/questionscontroller/guess">
    <a><button>Begin Quiz</button></a>
    </form>
  <?php
  }
  ?>

The only problem is that I get this error when loading the view:

When setting the data from the view to the controller the contreoller sets $u acordinglly and sends the data to the view, then the error diapers. I believe I get this error because when loading the HTML code $u is not set in any way because I haven’t sent anything to the controller.
Any suggestions how I can fix this error? Thanks

I am not sure how CodeIgniter works, but shouldn’t the variable be $data['u']?

Scott

Should be changed to

if ($data['u']) {

so that you don’t get confused and think that it can’t be any non-falsy value rather than just true.

Either that or add the extra = to make sure that $data[‘u’] must === true.

(note this includes the change already pointed out of using $data[‘u’] instead of $u)

One other picky item on your code. Don’t forget to use DRY. :smile:

function createuser(){
    $username = $this->input->post('username',false);
    if ($username == '') {
    	$data['u'] = false;
    }else{
    	$data['u'] = true;
    	$this->questionsmodel->setuser($username);
    }
    $this->load->view('loginview', $data);
}

Scott

@Andrei_Birsan

I copied, pasted and tried the script you supplied and it works fine.
Try this:

  error_reporting(-1);
  ini_set('display_errors', '1');

    $data['u'] = false; // DEFAULT
    if ($username != '')
    {
      $data['u'] = true;
      $this->questionsmodel->setuser($username);
    }
    
    // DEBUG   
    var_dump( $data ); die; // should halt execution and display all $data variables

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

This will eliminate the Undefine variable:

if ( isset($u) && $u ){
  ?>
    <!-- form action="../../index.php/questionscontroller/guess" -->
    <form action="/index.php/questionscontroller/guess"> <!-- TRY THIS -->
    <a><button>Begin Quiz</button></a>
    </form>
  <?php
  }
  

Edit:
Also try setting the error logging to maximum and view the error_log (which can be deleted after ever view)

file: /config/config/php

  $config['log_threshold'] = 4;
  
  // OPTIONAL
  $config['log_path'] = APPPATH; // ''; == DEFAULT to '';



@s_molinari, @felgall

That is the correct usage for passing data from CodeIgniter Controllers to the View

http://www.codeigniter.com/user_guide/general/views.html

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.