1

I'm using a script (applications/view/pages/home.php) which has an AJAX request whereby it gets and displays the content of another script (we'll call it scheduler.php). The scheduler file is just a file containing dynamically modified html based on a $_GET parameter passed to it from the AJAX request.

My issue is that this dynamic content comes from the database, and since the scheduler.php is being called by AJAX, it doesn't inherit the $this->db-> ability.

I get the error: Fatal error: Using $this when not in object context.

How can I fix this? I am a novice to both CodeIgniter and to AJAX. Thanks!

Edit: Scheduler.php code:

<?php
$shift = $_GET['shift'];
?>
<table>
    <tr>
        <th><?php echo $d->format('l') . '<br>' . $d->format('F jS'); ?></th>
    </tr>
    <tr>
        <td>
            <?php
            $this->db->select('id', 'event', 'time');
            $query = $this->db->get('myTable', $shift, $shift-5);
            foreach ($query->result() as $row) {
                echo "<a href='/schedule/{$row['id']}'>{$row['event']} at {$row['time']}</a><br>";
            }
        </td>
    </tr>
</table>
4
  • your scheduler.php may not be a controller. Commented Jan 25, 2014 at 17:46
  • can you add your scheduler.php code? Commented Jan 25, 2014 at 18:02
  • It's not a controller, no. Should it be? Again, I'm a novice at CodeIgniter (my experience is just based on the user guide and about a week of working with it). Commented Jan 25, 2014 at 18:14
  • you can not use $this on view. Commented Jan 25, 2014 at 18:37

2 Answers 2

1

As per discussion in comments, scheduler.php is not controller or library.

So you are calling outside the CI project. You can't use CI DB function untill you process through CI index.php file.

So just make scheduler.php as controller as below:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Scheduler extends CI_Controller {
    public function index($shift = "")
    {
        // add your stuff here to response to ajax request.
    }
}
?>

Then change ajax url to : domain.com/index.php/scheduler/index/{shift_value_here}

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

1 Comment

This worked perfectly! Thank you. Definitely feel like I understand CodeIgniter a bit more now.
0

You can get access to the CodeIgniter "super object" through $CI =& get_instance();

After that, replace $this with $CI in scheduler.php and you will have access to the framework libraries and functions etc.

Read section: Utilizing CodeIgniter Resources within Your Library in the documentation.

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.