Jordan Ramstad
Jordan Ramstad

Reputation: 179

PHP will not report errors

I feel this is weird but I cannot seem to get php to report errors.

I am making a class to check and format some results outside of a CMS to allow greater filter ability without having to create a custom plugin (due to time constraints).

I am including these 2 lines at the top of the page.

//error_reporting(E_ALL);
//ini_set('display_errors', '1');

I have toggled each and tried using both, no effect. My php.ini shows display_errors as being on and same with html_errors. error_reporting is set to 1.

For the older code I have it works, and the error reporting shows notices and errors. Though in some cases I did run into the same problem, though the issue was due to setting a variable outside of a function makes it not able to be called within the function without making it a function parameter. This is why I decided to switch to a class to avoid this problem.

I am trying to make a simple class to do this and output the result as JSON. My php is not perfect but if I at least get an error I can easily diagnose what went wrong. In this case I cannot get php to even show an echo, let alone any data/errors. All i get is a pure white page. Here is the full code I am using, it is a mess as I am in the process up updating it.

class k2JSON {
    private $limit = '';
    private $featured = '';
    private $prefix = 'base_';
    private $filter_query = '';
    private $types = array();
    private $dates = array();
    private $build = array();
    private $db_arr = array(
        'name' => 'westerner_days',
        'host' => 'localhost',
        'user' => 'rem',
        'pass' => '#w1n1nng'
    );
    function __construct(){
        echo 'called constructor <br />';
        if(isset($_GET['limit'])){
            $this->limit = ' LIMIT '.$_GET['limit'];
        }
        if(isset($_GET['featured'])){
            $this->featured = " AND `featured`='".$_GET['featured']."'";
        }
        $this->db = new mysqli(
            $db_arr['host'], 
            $db_arr['user'], 
            $db_arr['pass'], 
            $db_arr['name']
        );
        if(mysqli_connect_errno()){
            printf("Connect failed: %s\n", mysqli_connect_error());
            exit();
        }
        $this->checkFilter();
        $this->checkDates();
        $this->checkExtra();
    };
    private function checkFilter(){
        if(isset($_GET['filter'])){
            switch($_GET['filter']){
                case 'entertainment':
                    $this->filter_query = "SELECT * FROM `{$this->prefix}k2_items` WHERE `catid` IN (5,6,7,8,9,10,11){$this->featured} AND `trash`='0'".$this->limit;
                    break;
                case 'midway':
                    $this->filter_query = "SELECT * FROM `{$this->prefix}k2_items` WHERE `catid` IN (12,13,14){$this->featured} AND `trash`='0'".$this->limit;
                    break;
                case 'off-site':
                    $this->filter_query = "SELECT * FROM `{$this->prefix}k2_items` WHERE `catid` IN (15,16,17,18){$this->featured} AND `trash`='0'".$this->limit;
                    break;
                default:
                    $this->filter_query = "SELECT * FROM `{$this->prefix}k2_items` WHERE `catid` IN (5,6,7,8,9,10,11,12,13,14,15,16,17,18){$this->featured} AND `trash`='0'".$this->limit;
                    break;
            }
        }else{
            $this->filter_query = "SELECT * FROM `{$this->prefix}k2_items` WHERE `catid` IN (5,6,7,8,9,10,11,12,13,14,15,16,17,18){$this->featured} AND `trash`='0'".$this->limit;
        }
        $this->filter_result = $this->db->query($this->filter_query) or die($this->db->error.__LINE__);
    };
    private function checkDates(){
        $this->date_query = "SELECT * FROM `{$this->prefix}k2_extra_fields` WHERE `id` IN (2,14,11) AND `published`='1'";
        $this->date_result = $this->db->query($this->date_query) or die($this->db->error.__LINE__);
        while($row = $this->date_result->fetch_assoc()){
            $this->dates[$row['id']] = json_decode($row['value']);
        }
    };
    private function checkExtra(){
        $this->extra_query = "SELECT * FROM `{$this->prefix}k2_extra_fields` WHERE `published`='1'";
        $this->extra_result = $this->db->query($this->extra_query) or die($this->db->error.__LINE__);
        while($row = $this->extra_result->fetch_assoc()){
            $this->types[$row['id']] = json_decode($row['value']);
        }
    };
    private function buildOutput($data){
        $compile = array();
        $compile['extra']= array();
        $compile['title'] = $data['title'];
        $compile['featured'] = $data['featured'];
        $compile['published'] = $data['published'];
        $compile['desc'] = $data['introtext'];
        $compile['link'] = 'http://'.
            $_SERVER['HTTP_HOST'].
            dirname($_SERVER['REQUEST_URI']).'/'.
            (isset($_GET['filter'])?
                $_GET['filter']:
                in_array($data['catid'],[5,6,7,8,9,10,11])?
                    'entertainment':
                    in_array($data['catid'],[12,13,14])?
                        'midway':
                        in_array($data['catid'],[15,16,17,18])?
                            'off-site':
                            ''
            ).'/view/item/'.$data['id'].'-'.$data['alias'];
        $compile['type'] = $data['catid'];
        $compile['image'] = array(
            'xsmall' => '/media/k2/items/cache/'.md5('Image'.$data['id']).'_XS.jpg',
            'small' => '/media/k2/items/cache/'.md5('Image'.$data['id']).'_S.jpg',
            'medium' => '/media/k2/items/cache/'.md5('Image'.$data['id']).'_M.jpg',
            'large' => '/media/k2/items/cache/'.md5('Image'.$data['id']).'_L.jpg',
            'xlarge' => '/media/k2/items/cache/'.md5('Image'.$data['id']).'_XL.jpg',
        );
        $extra = json_decode($data['extra_fields']);
        foreach($extra as $key=>$value){
            switch($extra[$key]->id){
                case 2:
                    if(is_array($extra[$key]->value)){
                        $return_array = array();
                        foreach($extra[$key]->value as $key2=>$value2){
                            if($this->dates['2'][$extra[$key]->value[$key2]-1]->name!=null){
                                $return_array[]=$this->dates['2'][$extra[$key]->value[$key2]-1]->name;
                            }
                        }
                        $compile['date'] = $return_array;
                    }else{
                        $compile['date'] = $this->dates['2'][$extra[$key]->value-1]->name;
                    }
                    break;
                case 14:
                    if(is_array($extra[$key]->value)){
                        $return_array = array();
                        foreach($extra[$key]->value as $key2=>$value2){
                            if($dates['14'][$extra[$key]->value[$key2]-1]->name!=null){
                                $return_array[]=$this->dates['14'][$extra[$key]->value[$key2]-1]->name;
                            }
                        }
                        $compile['date'] = $return_array;
                    }else{
                        $compile['date'] = $this->dates['14'][$extra[$key]->value-1]->name;
                    }
                    break;
                case 11:
                    if(is_array($extra[$key]->value)){
                        $return_array = array();
                        foreach($extra[$key]->value as $key2=>$value2){
                            if($dates['11'][$extra[$key]->value[$key2]-1]->name!=null){
                                $return_array[]=$this->dates['11'][$extra[$key]->value[$key2]-1]->name;
                            }
                        }
                        $compile['date'] = $return_array;
                    }else{
                        $compile['date'] = $this->dates['11'][$extra[$key]->value-1]->name;
                    }
                    break;
                case 1:
                    $compile['extra'][$extra[$key]->id] = $extra[$key]->value;
                    break;
                case 15:
                    if(is_array($extra[$key]->value)){
                        $return_array = array();
                        foreach($extra[$key]->value as $key2=>$value2){
                            $return_array[]=$this->types['15'][$extra[$key]->value[$key2]]->name;
                        }
                        $compile['extra'][$extra[$key]->id] = $return_array;
                    }else{
                        $compile['extra'][$extra[$key]->id] = $this->types['15'][$extra[$key]->value]->name;
                    }
                    break;
                case 1:
                    if(is_array($extra[$key]->value)){
                        $return_array = array();
                        foreach($extra[$key]->value as $key2=>$value2){
                            $return_array[]=$this->types['1'][$extra[$key]->value[$key2]]->name;
                        }
                        $compile['extra'][$extra[$key]->id] = $return_array;
                    }else{
                        $compile['extra'][$extra[$key]->id] = $this->types['1'][$extra[$key]->value]->name;
                    }
                    break;
                case 7:
                    $compile['extra'][$extra[$key]->id] = $extra[$key]->value;
                    break;
                case 18:
                    $compile['extra'][$extra[$key]->id] = $extra[$key]->value;
                    break;
                case 10:
                    $compile['extra'][$extra[$key]->id] = $extra[$key]->value;
                    break;
                case 4:
                    $compile['extra'][$extra[$key]->id] = $extra[$key]->value;
                    break;
                case 16:
                    $compile['extra'][$extra[$key]->id] = $extra[$key]->value;
                    break;
                case 12:
                    $compile['extra'][$extra[$key]->id] = $extra[$key]->value;
                    break;
            }
        }
        return $compile;
    };
    public function parse(){
        if($this->filter_result->num_rows > 0) {
            while($row = $this->filter_result->fetch_assoc()) {
                if(isset($_GET['date'])){ //date but could have type
                    $extra = json_decode($row['extra_fields']);
                    foreach($extra as $key=>$value){
                        if($extra[$key]->id==2||$extra[$key]->id==14||$extra[$key]->id==11){ //find date extrafield
                            $check_array = array();
                            foreach($extra[$key]->value as $key2=>$value2){
                                $check_array[]=$dates['2'][$extra[$key]->value[$key2]-1]->name;
                            }
                            foreach($extra[$key]->value as $key2=>$value2){
                                $check_array[]=$dates['14'][$extra[$key]->value[$key2]-1]->name;
                            }
                            foreach($extra[$key]->value as $key2=>$value2){
                                $check_array[]=$dates['11'][$extra[$key]->value[$key2]-1]->name;
                            }
                            $compile['date'] = $check_array;
                            if(in_array($_GET['date'],$check_array)){
                                if(isset($_GET['type'])){ //type is set
                                    if($row['catid']==$_GET['type']){
                                        $this->build[]=$this->buildOutput($row);
                                    }
                                }else{ //type is not set, output all values.
                                    $bthis->build[]=$this->buildOutput($row);
                                }
                            }
                        } 
                    }
                }else if(isset($_GET['type'])){ //only type
                    if($row['catid']==$_GET['type']){
                        $this->build[]=$this->buildOutput($row);
                    }
                }else{ //no filters
                    $this->build[]=$this->buildOutput($row);
                }
            }
        }
    };
    public function outputResult(){
        print_r($this->build);
        echo json_encode($this->build);
    };
};
echo 'init <br />';
$json = new k2JSON();
$json->parse();
$json->outputResult();

I know there is probably some very obvious mistakes. But without php errors I cannot narrow it down. I hope that to someone with more experience with php this problem has an obvious fix.

Upvotes: 0

Views: 113

Answers (3)

hernan_arg
hernan_arg

Reputation: 1

quit the "//" in

//error_reporting(E_ALL);

//ini_set('display_errors', '1');

and remove the ";" after of functions declarations

Upvotes: 0

eleuteron
eleuteron

Reputation: 1983

Delete the ';' after methods brackets inside the class methods, and tell us if it works.

Upvotes: 0

Nathan van der Werf
Nathan van der Werf

Reputation: 332

are you sure you are getting errors?

try this also:

@ini_set('error_reporting', 'true');
error_reporting(E_ALL | E_STRICT);

your functions may be wrong you write functions like

public function something() {}; <==

write them like

public function something() {} 

doesnt solve your error problem, but at least a note

Upvotes: 1

Related Questions