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

[eluser]Fabdrol[/eluser]
Besides, I'm sorry for the half-post earlier. My internet connection got mixed up in the train so the forum posted half my code ;-)

I've completely scanned my code and can't find any more typos... but I'm still getting blank screens. Is it a CI issue, or is it invoked by something in my code? I'm very confused...

thanks!

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 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>';
        }
    }
    
    function _process($type, $json) {
        $obj = json_decode($json);
        
        if($type == 'web') {
            $process = $obj->ysearchresponse->resultset_web;
            $out = array(
                'status': 'OK',
                'results': array()
            );
            
            foreach($process as $i) {
                $f = $this->_filter($i->title, $i->abstract);
                
                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;
                }
            }
        }
        
        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;
    }
    
    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) {
        $search = strtolower($search);
        $content = strtolower($content);
        
         if(strpos($content, $search) === false) {
             return false;
         } else {
             return true;
         }
    }
    
    function _list($level) {
        switch($level) {
            case 1:
                $this->db->where('level', '1');
            break;
            
            case 2:
                $this->db->where('level', '2');
            break;
            
            case 3:
                $this->db->where('level', '3');
            break;
        }
        
        $q = $this->db->get($this->config->item('filter'));
        
        if($q->num_rows() > 0) {
            return $q->result();
        } else {
            return false;
        }
    }

}

I'm not even getting the 'hello world' thing..
#12

[eluser]Jelmer[/eluser]
Sorry, don't have the time to look into the script to find the problem. But in regard to the missing error_reporting: it's set to E_ERROR in the main index.php file at the top. Look into my starting with CI article (in my sig) for a suggestion to get both PHP & Database errors easily switched on (for development use) and off (for public use).
#13

[eluser]Fabdrol[/eluser]
Np, but my error reporting levels are set accordingly. I wouldn't post the problem here if i'd get an error message of some kind, probably.
#14

[eluser]Jelmer[/eluser]
I understood you did, but as I read your post you set it in your php.ini - which is overruled in the first part of your main index.php file that says:
Code:
&lt;?php
/*
|---------------------------------------------------------------
| PHP ERROR REPORTING LEVEL
|---------------------------------------------------------------
|
| By default CI runs with error reporting set to ALL.  For security
| reasons you are encouraged to change this when your site goes live.
| For more info visit:  http://www.php.net/error_reporting
|
*/
    error_reporting(E_ERROR);

Which you might change to: (to see if that makes it show errors)
Code:
&lt;?php
/*
|---------------------------------------------------------------
| PHP ERROR REPORTING LEVEL
|---------------------------------------------------------------
|
| By default CI runs with error reporting set to ALL.  For security
| reasons you are encouraged to change this when your site goes live.
| For more info visit:  http://www.php.net/error_reporting
|
*/
    error_reporting(E_ALL);

// And add for extra measure
ini_set('display_errors', 'On');
#15

[eluser]intractve[/eluser]
SOmeone correct me if I am wrong, but I do not believe you can use function variables in the controller,
like
function serve($type,$query) { ... }

That is only for Libraries / Models / Internal functions in controller.

Controller functions are invoked via URL (except internal functions) and not sure how you are sending the variables to your function

I would use
Code:
function serve()
{
  $type = $this->uri->segment(3);
// or a session variable or something.
  
}
#16

[eluser]Fabdrol[/eluser]
[quote author="intractve" date="1261089579"]SOmeone correct me if I am wrong, but I do not believe you can use function variables in the controller,
like
function serve($type,$query) { ... }

That is only for Libraries / Models / Internal functions in controller.

Controller functions are invoked via URL (except internal functions) and not sure how you are sending the variables to your function

I would use
Code:
function serve()
{
  $type = $this->uri->segment(3);
// or a session variable or something.
  
}
[/quote]

you sure can. It works like this:

Code:
function serve($a, $b) {
    // do stuff
}

access the variables like this (via the url)
Code:
site-name.com/index.php/proxy/serve/var_a/var_b

see also: http://ellislab.com/codeigniter/user-gui.../urls.html
#17

[eluser]intractve[/eluser]
By using URI-Routing? coz the link you gave didn't have a specific info for this.
Just asking to know coz this will be very useful in my projects.
#18

[eluser]Fabdrol[/eluser]
[quote author="intractve" date="1261091241"]By using URI-Routing? coz the link you gave didn't have a specific info for this.
Just asking to know coz this will be very useful in my projects.[/quote]

well, CI uri segments works like this:

base_url() / Controller name / Method name / Parameter 1 / Paremter 2 / etc...
#19

[eluser]intractve[/eluser]
Cool, I'll try this out. Thanks...
#20

[eluser]BrianDHall[/eluser]
Even if you don't use it much, it's nice to have an IDE with syntax verification like netbeans to use in cases like this. Took me just about 30 seconds to pop it open, paste in your code, and have it reveal the problem.

Lines 34-37:

Code:
$out = array(
                'status': 'OK',
                'results': array()
            );

The annoyance of using multiple languages at once - you defined your array in javascript style Smile

A simple correction removes the syntax errors, so hopefully this will solve the issue:

Code:
$out = array(
                'status' => 'OK',
                'results' => array()
            );

One downside to CI is this blank-screen problem, where in pure PHP it would always output _something_.

My solution is using step-through debugging - you just advance one line at a time until it suddenly halts execution, and you know that last line was where something broke php.




Theme © iAndrew 2016 - Forum software by © MyBB