N
N
Nikolay2015-12-12 21:06:20
PHP
Nikolay, 2015-12-12 21:06:20

How to display errors or where is the bug?

<?php
require_once('core/db.php');
require_once('core/Data_object.php');
require_once('db_rating.php');

Class ratings
{    
    protected $widget_id;  
    protected $data;
    private   $DB_Rating = new DB_Rating();
  
  function __construct($wid) {
    $this->data = $this->DB_Rating->get($wid);
    	$this->widget_id = $wid;
  }

  public function get_ratings() { 
    $Data_object = new Data_object();

    $Data_object->widget_id = $this->widget_id;
    $Data_object->number_votes = 0;
    $Data_object->total_points = 0;
    $Data_object->dec_avg	   = 0;
    $Data_object->whole_avg	   = 0;
    	
    	foreach($this->data as $vote)
    	{
    		$Data_object->number_votes += 1;
    		$Data_object->total_points += $vote->vote;
    	}
    	$Data_object->dec_avg = $Data_object->total_points / $Data_object->number_votes;
    	$Data_object->whole_avg = round($Data_object->dec_avg);

    	echo json_encode($Data_object->get_filds_in_assoc_array());
  }

  public function vote() {
      $Data_object = new Data_object();

      preg_match('/star_([1-5]{1})/', $_POST['clicked_on'], $match);  
      $vote = (integer) $match[1];
    $ID = $this->widget_id;  
    
    session_start();
    $Data_object->set_fields(
      'vote', 	  $vote,
      'company_id', $ID,
      'user_id',	  (integer) $_SESSION['user_id']
    );
    $this->DB_Rating->set_data($Data_object);
    $this->DB_Rating->set();

    $this->get_ratings();
  }
}

<?php

Class DB_Rating extends DB
{
  protected $select_query = 'SELECT * FROM rating INNER JOIN users USING(user_id) WHERE company_id = ?';

  protected $insert_query = 'INSERT INTO rating';
  protected $set_vote  		= 'vote = ?';
  protected $set_company_id 	= 'company_id = ?';
  protected $set_user_id 		= 'user_id = ?';

  protected $check_query = 'SELECT * FROM rating WHERE company_id = ? AND user_id = ?';


  public function __construct($obj = '')
  {
    $this->pdo = new PDO($this->dsn, 'root', '', $this->opt);
        $this->Data_object = $obj;
  }

  public function get($company_id)
  {
    if (isset($company_id))
        {
            $Data_object = new Data_object();
    
      $stmt = $this->pdo->prepare($this->select_query);
      $stmt->execute(array($company_id));
      $j = 0;
      foreach($stmt as $request_vote)
      {
        $Vote = new Data_object();
        $Vote->set_fields
        (
          'user', $request_vote['login'],
          'vote', $request_vote['vote']
        );
        $name = 'vote_'.$j;
        $Data_object->$name = $Vote;
        $j++;
      }
      return $Data_object;
        }
  }

  public function set()
  {
    if (isset($this->Data_object) || is_object($this->Data_object))
        {
            $names_of_fields = $this->Data_object->get_names();
            for ($i = 0; $i < count($names_of_fields); $i++)
            {
                $this->filter('INSERT INTO rating', 'insert', $names_of_fields[$i], 'set', ' SET ', ', ');
            }
            $arr = $this->Data_object->get_filds_in_array();
            if (
            	isset($arr) && 
            	$this->insert_query != 'INSERT INTO rating' && 
            	$this->isclone( $check_query, $this->Data_object->company_id )
            ) {
                $stmt = $this->pdo->prepare($this->insert_query);
                $stmt->execute($arr);
            }
        }
        else {
            throw new Exception("It must be object.");
        }
  }

  private function isclone($query, $fild_name)
  {
    session_start();
    $name = $this->Data_object->get_fields
    (
      'company_id', $fild_name,
      'user_id', $_SESSION['user_id']
    );

        if (isset($name))
        {
            $check = $this->pdo->prepare($query);
            $check->execute(array($name));
            if (!$row = $check->fetch(PDO::FETCH_LAZY))
            {
                return true;
            }
            else {
                echo '$err_text_1';
                return false;
            }
        }
        else {
            echo '$err_text_2';
            return false;
        }
  }
  }

  public function set_data($Data_object)
    {
    	$this->Data_object = $Data_object;
    }
}

Data_object - a class for collecting, formatting, transferring and convenient presentation of data.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question