CodeIgniter Forums
newbie help with database view and lack of errors - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: newbie help with database view and lack of errors (/showthread.php?tid=8180)



newbie help with database view and lack of errors - El Forum - 05-08-2008

[eluser]neilw[/eluser]
Hello,
I'm brand new to code igniter and it's looking really good Smile

However, I'm having a couple of problems which I imagine are embarrassingly simple. I decided to do a simple test of retrieving data from a database and outputting the contents to the screen using a controller/view (without the model):

1.
In my controller class I added this function:
Code:
function games()
    {
        $this->load->database();
        $query=$this->db->get('retro_games');
        $ret="";
        foreach($query->result() as $row) {
            $ret .= $row->name . '<br />';
        }
        $data['gamelist']=$ret;
        $this->load->view('gameslist',$data);
    }
In my view I first tried to view it:
Code:
&lt;body&gt;
&lt;?php echo $gamelist;
?&gt;
&lt;/body&gt;
All works fine, however I wanted to take the string generation out of the controller and put it in the view so that the view can determine how to view the data, but I can't figure out how to correctly pass in and iterate through the data. I tried passing in $query, e.g.
$this->load->view('gameslist',$query);

but I don't know what to do in the view as you don't get $query, but the contents of it. I tried passing in $query->result(); but again I didn't know what to use in the for loop, e.g.
&lt;?php foreach($xxxxx as $row) {} ?&gt;

Can anyone help?

2.
In my controller I thought I put in a correct bit of code, which turned out to be wrong, however no errors are generated and there is nothing in the log file, I simply get a completely blank screen with no generated html. Errors are turned on and I've got the log set to everything, e.g. following my code above I tried to chain things together:

$query=$this->db->get('retro_games')->order_by('name','asc');

But there was no error and the view wasn't called afterwards as there was no html? This also happened in my view where I put the wrong syntax in some php, btw.

Many thanks.


newbie help with database view and lack of errors - El Forum - 05-08-2008

[eluser]Mirage[/eluser]
Hi, welcome to CI!

Controller:
Code:
function games()
{
    $this->load->database();
    $query=$this->db->get('retro_games');
    $games= ($query && $query->num_rows()>0) ? $query->result(): array();
    
    $this->load->view('gameslist',compact('games'));
}

View:
Code:
&lt;? foreach($games as $game): ?&gt;
&lt;?=$game?&gt;<br/>
&lt;? endforeach; ?&gt;

Tip: Watch the CI tutorials on this site!


newbie help with database view and lack of errors - El Forum - 05-08-2008

[eluser]neilw[/eluser]
thanks, I did watch the tutorials but as it's 20 minutes long and the examples on it aren't supplied with the release (perhaps they should?) I didn't want to watch it all again as it might not have contained the answers. I need to brush up on my php as well as I never knew compact existed Smile

How about my second question regarding blank outputs? I can't see anything wrong with my apache or php configuration.

Thanks again.

btw, you've just fixed my other problem as I couldn't get 'endforeach' to work (I'm from old php 3/4 days still) and just realised it's the missing colon.


newbie help with database view and lack of errors - El Forum - 05-08-2008

[eluser]Mirage[/eluser]
You're welcome.

My example probably isn't the best 'beginners' code. Using compact to process local variables to an array is just something I've grown accustomed to.

As far as blank output, it's usually a syntax problem. There are circumstances under which an error will not be reported where you expect it. I think it's largely related to the PHP 'error_reporting' mumbo jumbo that CI taps into. Was really the only way before we got exceptions in PHP5 but those really aren't used in CI because of PHP4 compatibility.

- In your config, set your 'log_threshold' to 4
- In your index.php add this line: ini_set('display_errors',1));
- tail -f your CI logfile and the apache error_log

Between the things above you should discover where your error is.

Cheers!


newbie help with database view and lack of errors - El Forum - 05-08-2008

[eluser]neilw[/eluser]
Thanks, the ini setting was already on for full error reporting. I found the error in the apache error.log file.

The error generated is usually output to the screen though, it's only when using codeignite that it's failing to do so?


newbie help with database view and lack of errors - El Forum - 05-08-2008

[eluser]Mirage[/eluser]
In all my years of PHP coding I never found out. Or perhaps, I never bothered to find out. It's a combination of capturing error reporting, output buffering and where in the chain something happens. For example you're loading your views at a late point, if something was output already then display errors may not do anything. Something of that sort. Output buffer level related perhaps.

I still have a lot of PHP4 coding practices but personally I can't wait until CI goes PHP5 only or at least deploys a full PHP5 branch. That would probably help me adopt PHP5 coding principles better. I wish Zend had done a better job with supplanting PHP4. Or worked the object model a bit more. It's really put the PHP community in an ugly longstanding dilemma. The future appears bright however.

Cheers!


newbie help with database view and lack of errors - El Forum - 05-09-2008

[eluser]neilw[/eluser]
ok thanks, you can mark this thread as successfully completed. I'm busy converting my site now and it's great. I just wish eclipse could recognise variables in php so it auto completes them for me when I type -> Smile

btw, I have the same dilemma choosing mysqli. I keep meaning to, but I always end up with mysql.


newbie help with database view and lack of errors - El Forum - 05-09-2008

[eluser]Mirage[/eluser]
This may not be an option for you - but Zend IDE for Eclipse does all of that. I don't think I could have accomplished as much as I did in past few years if it wasn't for Zend IDE [and CI].

Cheers!


newbie help with database view and lack of errors - El Forum - 05-09-2008

[eluser]neilw[/eluser]
Hello,
Thanks, I've got it working now and it was painful Wink

Your code:
Code:
&lt;? foreach($games as $game): ?&gt;
&lt;?=$game?&gt;<br/>
&lt;? endforeach; ?&gt;

isn't right and I couldn't at first figure it out, because it was producing an array of objects and each object had a variable of the field name, so I eventually worked it out (I'm used to using arrays of non-objects):

$game->name;

where name is the name of the field.