Welcome Guest, Not a member yet? Register   Sign In
Some **OK** code in my controller beaks CodeIgniter: output goes totally blank.
#1

[eluser]Fabdrol[/eluser]
Hi guys,

I've got a little problem. Two methods in my controller seem to cause CI to break. When I comment them out, the test() method work but when I have them not commented out, the controller doesn't output anything at all.

There's no information in the logs, although my threshold is at 4.

It's about the following methods: _process() & _filter().
The code:

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

class Proxy extends Controller {

    function __construct() {
        parent::Controller();
    }
    
    function index() {
        echo "Hello World";
    }
    
    function test() {
        echo "Hello World";
    }
    
    function serve($type, $query) {
        if($type == 'web') {
            $url = $this->config->item('boss_url');
            $url.= urlencode($query);
            $url.= '?appid=' . $this->config->item('boss_api_key');
            $url.= '&format=json&start=0&count=10&abstract=long';
            
            $json = $this->_execute($url);
            $o = $this->_process('web', $json);
            
            echo '<pre style="border: 1px solid #ccc; padding: 10px 20px;">';
            print_r($o);
            echo '</pre>';
        }
    }
    // first problematic method
    function _process($type, $json) {
        $obj = json_decode($json);
        
        switch($type) {
            case 'web':
                $process = $obj->ysearchresponse->resultset_web;
                $out = array(
                    'status': 'OK',
                    'results': array()
                );
                
                foreach($process as $i) {
                    $f = true;
                    
                    if($f != false) {
                        if($f == '1') $o['haram'] = 1;
                        if($f == '2') $o['haram'] = 2;
                        if($f == '3') $o['haram'] = 3;
                        if($f == true) $o['haram'] = false;
                        
                        $o['title'] = $i->title;
                        $o['abstract'] = $i->abstract;
                        
                        $out['results'][] = $o;
                    }
                }
            break;
        }
        
        return $out;
    }
    
    function _execute($url) {
        $c = curl_init();
            
        curl_setopt($c, CURLOPT_URL, $uri);
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
        
        $o = curl_exec($c);
        curl_close($c);
        
        if(!$o) return json_encode(array('status' => 'error', 'error' => 'no server response'));
        return $o;
    }
    // second problematic method
    function _filter($titel, $abstract) {
        $strip = array(".",",",";",":","&","%","@","#","$","€","*","(",")","{","}","]","[","|","\\","/", "<", ">","`","~","±","§","'","\"");
        
        $titel = str_ireplace($strip, ' ', $titel);
        $abstract = str_ireplace($strip, ' ', $abstract);
        
        foreach($this->_list(1) as $r) {
            $term = ' ' . $r->term . ' ';
            
            if($this->_contains($term, $titel) {
                return '1';
            }
            
            if($this->_contains($term, $abstract) {
                return '1';
            }
        }
        
        foreach($this->_list(2) as $r) {
            $term = ' ' . $r->term . ' ';
            
            if($this->_contains($term, $titel) {
                return '2';
            }
            
            if($this->_contains($term, $abstract) {
                return '2';
            }
        }
        
        foreach($this->_list(3) as $r) {
            $term = ' ' . $r->term . ' ';
            
            if($this->_contains($term, $titel) {
                return false;
            }
            
            if($this->_contains($term, $abstract) {
                return false;
            }
        }
        
        return true;
    }
    
    function _contains($search, $content, $ignorecase = true) {
        if($ignorecase){
            $search = strtolower($search);
            $content = strtolower($content);
         }
        
         return strpos($content, $search) ? true : false;
    }
    
    function _list($level) {
        switch($level) {
            case 1:
                $this->db->where('level', '1');
            break;
            
            case 2:
                $this->db->where('level', '1');
            break;
            
            case 3:
                $this->db->where('level', '1');
            break;
        }
        
        $q = $this->db->get($this->config->item('filter'));
        
        if($q->num_rows() > 0) {
            return $q->result();
        } else {
            return false;
        }
    }

}
#2

[eluser]bretticus[/eluser]
Not sure is wrong with _process() but I see some syntax errors in _filter().

Code:
if($this->_contains($term, $titel) {

is missing the closing paren for the if statement (and so are several others.)
#3

[eluser]Fabdrol[/eluser]
stupid mistake, I shouldn't write code at midnight!
Thing is, if thats the cause, I'd expect an php error, not an blank output!

Could I have some naming issues regarding internal CI methods or functions?
#4

[eluser]bretticus[/eluser]
[quote author="Fabdrol" date="1260727304"]
Thing is, if thats the cause, I'd expect an php error, not an blank output![/quote]

Actually blank pages are common with PHP. Often, in a production environment, administrators will disable error output to avoid displaying any tantalizing clues to prospect hackers. Smile

Make sure you are showing errors.

Disclaimer on the link above: It's for a specific file in a software distribution, but the same rules apply for your script (or any PHP script if you want to display errors.)
#5

[eluser]Fabdrol[/eluser]
Hi Bretticus,

I know quite well what to set when developing and I assure you that my error_reporting settings are all set correctly.
The script breakes the CI logic somewhere before it can initialize the output class, so if the problems remain, I'll do some testing by adding some 'echo's' within the main CI logic, to see till where it's called.

thanx anyway,
Fabian
#6

[eluser]Fabdrol[/eluser]
Crap, it didn't solve the problem at all. My updated code below, did I forget something?
thanx!

[code]
&lt;?php if(!defined('BASEPATH')) exit('No direct script access allowed');

class Proxy extends Controller {

function __construct() {
parent::Controller();
}

function index() {
echo "Hello World";
}

function test() {
echo "Hello World";
}

function serve($type, $query) {
if($type == 'web') {
$url = $this->config->item('boss_url');
$url.= urlencode($query);
$url.= '?appid=' . $this->config->item('boss_api_key');
$url.= '&format;=
#7

[eluser]stuffradio[/eluser]
[quote author="Fabdrol" date="1260740987"]Crap, it didn't solve the problem at all. My updated code below, did I forget something?
thanx!
[/quote]

Code:
$url.= '&format;=
No semicolon at the end and no " ' " either.

Code:
$url.= '&format;=';
#8

[eluser]bretticus[/eluser]
May I suggest an IDE with code syntax highlighting? I respect those that can crank stuff out in notepad, but sometimes a little syntax highlighting can save you lots of grief.

For Windows I like Notepad++. For Mac, I like TextWrangler (because it's free.)

For an overarching IDE I love Netbeans.

Cheers!
#9

[eluser]stuffradio[/eluser]
[quote author="bretticus" date="1260749845"]May I suggest an IDE with code syntax highlighting? I respect those that can crank stuff out in notepad, but sometimes a little syntax highlighting can save you lots of grief.

For Windows I like Notepad++. For Mac, I like TextWrangler (because it's free.)

For an overarching IDE I love Netbeans.

Cheers![/quote]
I use Dreamweaver.
#10

[eluser]Fabdrol[/eluser]
I use Coda, but somehow my code doesn't look broken in there.
Anyways, last time I looked at it it was midnight and I'm not that focussed as during the day.

I'll take a good look again, scan my code for anything I might have overlooked.




Theme © iAndrew 2016 - Forum software by © MyBB