1

Im loading different contents in my main view with my menu, the code is working properly but is i have this error message.

A PHP Error was encountered
Severity: Notice
Message: Use of undefined constant users - assumed 'users'
Filename: views/welcome.php
Line Number: 53
A PHP Error was encountered
Severity: Notice
Message: Use of undefined constant stores - assumed 'stores'
Filename: views/welcome.php
Line Number: 56
A PHP Error was encountered
Severity: Notice
Message: Use of undefined constant notes - assumed 'notes'
Filename: views/welcome.php
Line Number: 59

This is my view

            <div id="nav">
            <div id="imagesBox">
                <a href="index.php?section=users"><img class="imagesLeft" src="images/usuario_on.png"></a>
                <a href="index.php?section=stores"><img class="imagesLeft" src="images/tienda_on.png"></a>
                <a href="index.php?section=notes"><img class="imagesLeft" src="images/aviso_on.png"></a>
        </div>
        <?php
        $section; $users=0;
        switch ($_GET['section']) {
        case users:
            $this->load->view('users'); 
            break;
        case stores:
            $this->load->view('stores'); 
            break;
        case notes:
            $this->load->view('notes'); 
            break;
    }

?>
    </div>

And this is my controller (im using tank_auth and is working properly)

class Welcome extends CI_Controller
{
    function __construct()
    {
        parent::__construct();

        $this->load->helper('url');
        $this->load->library('tank_auth');
    }
    function index()
    {
        if (!$this->tank_auth->is_logged_in()) {
            redirect('/auth/login/');
        } else {
            $data['user_id']    = $this->tank_auth->get_user_id();
            $data['username']   = $this->tank_auth->get_username();
            $this->load->view('welcome', $data);
        }
    }
}

I dont find the best method to fix this and this is a little weird, because the code is working like i want.

1
  • You need to add quotes in your case statements: case 'users' etc. Commented Apr 23, 2014 at 8:41

3 Answers 3

3

Replace you view code with the below:

  <?php
        $section; $users=0;
        switch ($_GET['section']) {
        case "users":
            $this->load->view('users'); 
            break;
        case "stores":
            $this->load->view('stores'); 
            break;
        case "notes":
            $this->load->view('notes'); 
            break;
    }

A string must be define in the quotation mark. You have leave the quotation mark in the string used in case part.

Reference.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but i still have the error 'Message: Undefined index: section' on index.php when "section=something" is not on the url
you need to pass the section from url. or need to apply the check with if condition.
1

You need to add quotes to your case statements, otherwise they will be treated as constants, if they're not defined php assumes they are strings (which is why your code works, but generates a notice).

case 'users':
  ..
case 'stores':
  ..
case 'notes':
  ..

Since the value of $_GET['section'] corresponds to the view, you could simplify the code, using a simple whitelist:

if (in_array($_GET['section'], array('users', 'stores', 'notes')) {
  $this->load->view($_GET['section']);
}

Comments

1

This error could be happened because of a silly mistake. Make sure that you created the file for controller redirect('/auth');. Maybe already you've written redirect('/auth/login/'); but not created a file for the controller auth yet.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.