Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4332744
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:17:54+00:00 2026-05-21T10:17:54+00:00

I have the following code in a controller: <?php class Student extends CI_Controller {

  • 0

I have the following code in a controller:

<?php

class Student extends CI_Controller
{

function index()
{
    $data = $this->init->set();
    
    $this->parser->parse('include/header', $data);
    $this->parser->parse('student/student_index', $data);
    $this->parser->parse('include/footer', $data);
}

function planner()
{
    $data = $this->init->set();
    
    $this->parser->parse('include/header', $data);
    $this->parser->parse('student/student_cal', $data);
    $this->parser->parse('include/footer', $data);      
}

}
?>

As you can see, there’s a lot of repetition here. Basically all of it. I already put my variables in a model so I only have to call the model function each time instead of putting the whole $data array at the start of each function. Anyway, I tried to reduce the repetition here by doing the following:

<?php

class Student extends CI_Controller
{

function index()
{
    $data = $this->init->set();
    
    $this->parser->parse('include/header', $data);
    
    switch($this->uri->segment(2))
    {
        case '': $this->home($data); break;
        case 'planner': $this->planner($data); break;
    }
    $this->parser->parse('include/footer', $data);
}

function home($data)
{
    $this->parser->parse('student/student_index', $data);
}


function planner($data)
{
    $this->parser->parse('student/student_cal', $data);
}

}
?>

This, somehow, works fine for my homepage. It parses the variables and there’s no problem whatsoever. However, on the ‘planner’ page, I get errors:

Message: Missing argument 1 for Student::planner()

Message: Undefined variable: data

Message: Invalid argument supplied for foreach()

I’m quite sure I get these errors because the function somehow doesn’t receive the $data array. I also read in the CI docs that the third segment in a URL gets passed as argument, and in this case the third segment is non-existent, thus nothing gets passed. However, the CI docs didn’t tell me how I could pass my $data array from the index() function to my planner() function. I also wonder why the home function works fine, without errors.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T10:17:55+00:00Added an answer on May 21, 2026 at 10:17 am

    Now, I don’t see the reason for that refactoring if it’s going to make the code really hard to look at. I’m not entirely sure what the parse function does, so the way I changed it was to actually pass the parameter as a string, but preferably I’d load the content into a buffer and pass it in that way. But here’s some cleaner and hopefully readable removable of the duplication… and hopefully it works :).

    
    
    class Student extends CI_Controller
    {
    
      private function load_student_page($content){
          $data = $this->init->set();
    
          $this->parser->parse('include/header', $data);
          $this->parser->parse($content, $data);
          $this->parser->parse('include/footer', $data);
    
      }
    
      function index()
      {
        $this->load_student_page('student/student_index');
      }
    
      function planner()
      {
        $this->load_student_page('student/student_cal');
      }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code in controllers/list.php: <?php class List extends Controller { function
I have the following controller file: <?php class Test extends CI_Controller { function __construct()
I have the following code in a Controller: $data['what'] = 'test'; $this->load->view('test_view', $data); $this->load->view('test_view');
I have the following code for my login view: <?php echo $session->flash('auth'); echo $this->Form->create('User',
I have the following code var test; $.ajax({ type: "GET", url: "../views/person/controller.php?actor=person&action=checkAge", data: "age="
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have the following code in my controller: public ActionResult Details(int id) { var
I have the following code in a CakePHP Controller: var $searchCondition = array( 'Item.date
I have the following controller code: def create @admin = Admin.new(params[:admin]) respond_to do |format|
I have the following code in a view controller. pageDisplay is a UIWebView. When

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.