• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
One of my controllers refuses to load any views!

#1
[eluser]dauber[/eluser]
The title says it all -- I have a controller that's just refusing to load any views. I know the controller itself works because it's successfully calling a model (verified by error logs I've put in), but I'm not getting any kind of error messages, either on screen or in the error log.

Here's the controller (with some variable assignments stripped out for brevity):

Code:
<?php

class Srchgames_ctrl extends CI_Controller {
              
    function __construct() {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->database();
        $this->load->helper('form');
        $this->load->helper('email');
    }
    
    function do_search() {
        $this->load->model('srchgames_model');
        error_log("Starting do_search");
        // bunch of $search_games[] assignments removed
        $search_result = $this->srchgames_model->search_games($search_games);
        error_log("About to load the new view...");
        $this->load->view('search_list',$search_result);
    }
}

Now, I've tried moving the "$this->load->view()" command to the very top of the do_search() method, but even then nothing happened. The view absolutely exists; and in fact I tried plugging in other views that other controllers do successfully load, and even gibberish, meaning that the view wouldn't be found...but got nothing. Even took out the $search_result parameter just to get the raw view with no data. But again, I get nothing. Just a blank white page with empty source code.

FWIW, here's 'search_list.php' (which is situated in the "views" directory along with the other views):

Code:
<!DOCTYPE html>
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;Da Games&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        THIS IS A TEST!!!
&lt;?php
foreach($gameTitle as $game) {
    echo $gameTitle."<br />";
}
?&gt;
    &lt;/body&gt;
&lt;/html&gt;

Thoughts???

#2
[eluser]joergy[/eluser]
Does Your $search_result contain an array named "gameTitle" ?
What about writing a more primitive controller
Code:
class Nix extends CI_controller {
function index(){
$search_result = array(
"gameTitle" => array("agame")
}
$this->load->view(...,$search_result);
}
to make sure that there is no bug in your other code?

#3
[eluser]dauber[/eluser]
Ooh...you know what? No, I don't have a "gameTitle" array after all!

What I'm trying to do is pass through some SQL results as an array...but they're indexed numerically. I guess I have to find out how to handle that in the view...the results are basically:
[0]
[gameTitle]
[otherMeaninglessStuffYouDontCareAbout]

[1]
[gameTitle]
[otherMeaninglessStuff...]
[2]
[gameTitle]
[otherMeaninglessStuff...]

etc. My next question, I guess, is how to display that array in the view. :/

#4
[eluser]InsiteFX[/eluser]
Use php count on the array to get the total count, then you can use a for loop to loop through them.

#5
[eluser]CroNiX[/eluser]
When you pass an array to view, codeigniter runs php's extract() on it
Code:
//put your search result in an array and pass that array to data.
$data['search_result'] = $this->srchgames_model->search_games($search_games);
$this->load->view('search_list',$data);

in view:
Code:
//now you can access $search_result
&lt;?php foreach($search_result as $result): ?&gt;
<div>&lt;?php echo $result['gameTitle']; ?&gt;</div>
&lt;?php endforeach; ?&gt;

#6
[eluser]dauber[/eluser]
So....I just took ALL PHP CODE out of the view, so now it's 100% HTML...and it still won't load. Sad

And I take it back...the resulting page is not blank -- it has the number "1" at the top. Even after I removed all the error_log() and print_r() commands. arrghhhh...


#7
[eluser]Tim Brownlaw[/eluser]
Let's see if we can make it start showing errors... if you haven't already got this setup try changing the following index.php

Code:
define('ENVIRONMENT', 'development');
/*
*---------------------------------------------------------------
* ERROR REPORTING
*---------------------------------------------------------------
*
* Different environments will require different levels of error reporting.
* By default development will show errors but testing and live will hide them.
*/

if (defined('ENVIRONMENT'))
{
switch (ENVIRONMENT)
{
  case 'development':
   error_reporting(E_ALL);
  break;

  case 'testing':
  case 'production':
   error_reporting(0);
  break;

  default:
   exit('The application environment is not set correctly.');
}
}
To this ( we are adding in the Error Display commands )
Code:
ini_set('display_errors', 1);           // Enable errors to be displayed to the screen
ini_set('display_startup_errors',1);    // Not really needed but doesn't hurt.
So you should end up with something like...
Code:
define('ENVIRONMENT', 'development');
/*
*---------------------------------------------------------------
* ERROR REPORTING
*---------------------------------------------------------------
*
* Different environments will require different levels of error reporting.
* By default development will show errors but testing and live will hide them.
*/

if (defined('ENVIRONMENT'))
{
switch (ENVIRONMENT)
{
  case 'development':
   ini_set('display_errors', 1);           // Enable errors to be displayed to the screen
   ini_set('display_startup_errors',1);    // Not really needed but doesn't hurt.
   error_reporting(E_ALL);
  break;

  case 'testing':
  case 'production':
   error_reporting(0);
  break;

  default:
   exit('The application environment is not set correctly.');
}
}

And see what results from that.

Also read over and check your code for any errors or typos or "1"'s kicking about...
If you are getting a "1" showing up - the code you have provided already isn't the code you are running... somethings changed!

Another question - "How are you calling this Controller/Method? From the URL or from another Controller / Module?



#8
[eluser]dauber[/eluser]
Thanks. I made the additions you suggested, but I'm still not getting anything different. Nothing in the error logs, nothing on the screen, and the source code itself again is just the number 1.

As for how the controller/method is called, it's via a form. Here's the flow:

Fill out a form --&gt; form submit button triggers a JS form validator --&gt; after validation the "do_search()" method in the "srchgames_ctrl" controller is called via AJAX --&gt; do_search() loads "srchgames_model", parses variables passed from the controller, builds a SQL query and executes the search (again, error logs confirm that the correct information is pulled from MySQL), then attempts to send that data to --&gt; views/search_list.php.

But something is wonky with do_search in the controller, because it refuses to load ANY views at ANY point, whether the views have any PHP or not, and no matter where in the method I put the $this->load->view() command, and whether or not I attempt to pass any data.

#9
[eluser]dauber[/eluser]
So some further steps....

- Reduced the do_search() method so that this was literally the WHOLE METHOD:

Code:
function do_search() {
        $this->load->view('search_list');
}

Result: blank page with "1" as the entire source code.

- Replaced that line in do_search() with this:
Code:
redirect('srchgames_ctrl/display_results');

and added this function to the controller:

Code:
function display_results() {
        error_log("display results");
        $this->load->view('search_list');
    }

Result: same thing: blank page, with "1" in the source code and nothing else.

And the error log didn't have "display results" in it, so that redirect wasn't even firing.

- Added this to the do_search() method:
Code:
error_log("redirecting");

Result: again, blank page with "1" as the entire source code, but this time the word "redirecting" appeared in the error log.

I'm really beyond frustrated at this point. I'm pretty much mirroring every other form, model, controller, view, etc., that I have on this page, all of which work perfectly...

#10
[eluser]CroNiX[/eluser]
You mentioned calling do_search with ajax. Is there something going on in your js code? There are pieces of this puzzle that are missing. Post all relevant code, including the js, relevant controller, relevant model and any views.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.