Home > Back-end >  Call to undefined function getRating() in Codeigniter
Call to undefined function getRating() in Codeigniter

Time:03-06

Please help me solve this error

I use codeigniter framework

File Beranda_model.php

public function getPost() {

    // lets assume a user is logged in with id $user_id
    $user_id = 1;

    // if user clicks like or dislike button
    if (isset($_POST['action'])) {
      $post_id = $_POST['post_id'];
      $action = $_POST['action'];
      switch ($action) {
        case 'like':
             $sql="INSERT INTO rating_info (user_id, post_id, rating_action) 
                 VALUES ($user_id, $post_id, 'like') 
                 ON DUPLICATE KEY UPDATE rating_action='like'";
             break;
        case 'dislike':
              $sql="INSERT INTO rating_info (user_id, post_id, rating_action) 
                   VALUES ($user_id, $post_id, 'dislike') 
                 ON DUPLICATE KEY UPDATE rating_action='dislike'";
             break;
        case 'unlike':
            $sql="DELETE FROM rating_info WHERE user_id=$user_id AND post_id=$post_id";
            break;
        case 'undislike':
              $sql="DELETE FROM rating_info WHERE user_id=$user_id AND post_id=$post_id";
          break;
        default:
          break;
      }

        $this->db->query($sql);
        echo getRating($post_id);
        exit(0);
    }

    // Get total number of likes for a particular post
    function getLikes($id)
    {

      $ci =& get_instance();
      $sql = "SELECT COUNT(*) FROM rating_info WHERE post_id = $id AND rating_action='like'";
      $rs = $ci->db->query($sql)->result_array();
      return implode($rs[0]);
    }

   // Get total number of dislikes for a particular post
    function getDislikes($id)
    {
      $ci =& get_instance();
      $sql = "SELECT COUNT(*) FROM rating_info 
            WHERE post_id = $id AND rating_action='dislike'";
      $rs = $ci->db->query($sql)->result_array();
      return implode($rs[0]);
    }

    // Get total number of likes and dislikes for a particular post
    function getRating($id)
    {
      $ci =& get_instance();
      $rating = array();
      $likes_query = "SELECT COUNT(*) FROM rating_info WHERE post_id = $id AND rating_action='like'";
      $dislikes_query = "SELECT COUNT(*) FROM rating_info 
                WHERE post_id = $id AND rating_action='dislike'";
      $likes_rs = $ci->db->query($likes_query)->result_array();
      $dislikes_rs =$ci->db->query($dislikes_query)->result_array();
      $rating = [
        'likes' =>  implode($likes_rs[0]),
        'dislikes' => implode($dislikes_rs[0])
      ];
      return json_encode($rating);
    }

    // Check if user already likes post or not
    function userLiked($post_id)
    {

      $user_id = 1;
      $ci =& get_instance();
      $sql ="SELECT * FROM rating_info WHERE user_id=$user_id AND post_id=$post_id AND rating_action='like'";
      $rs = $ci->db->query($sql);
      if ($rs->num_rows() > 0) {
        return true;
      } else {
        return false;
      }
    }

  // Check if user already dislikes post or not
  function userDisliked($post_id)
  {
    $user_id = 1;
    $ci =& get_instance();
    $sql = "SELECT * FROM rating_info WHERE user_id=$user_id AND post_id=$post_id AND rating_action='dislike'";
      $rs = $ci->db->query($sql);
      if ($rs->num_rows() > 0) {
        return true;
      } else {
        return false;
      }
  }

    $sql = "SELECT * FROM posts";
    $posts = $this->db->query($sql)->result_array();

    return $posts;

  }

the error :

An uncaught Exception was encountered Type: Error Message: Call to undefined function getRating() Filename: D:\xampp\htdocs\paseh_aspirasi\application\models\Beranda_model.php Line Number: 51 Backtrace: File: D:\xampp\htdocs\paseh_aspirasi\application\controllers\Beranda.php Line: 18 Function: getPost File: D:\xampp\htdocs\paseh_aspirasi\index.php Line: 315 Function: require_once

here:

echo getRating($post_id);

Thank you!

CodePudding user response:

What you did is kind of fighting the OOP paradigm.

OOP offers the ability to group functions and variables under a class/object as methods/attributes

  1. Constants, static attributes and static methods belong to the class and can be called using self::$property, self::CONSTANT, self::method(), ClassName::CONSTANT, ClassName::method() ...
  2. Attributes and methods are called using $this->property, $objectName->method() ...

ref1

Nomral functions are meant to be declared globally and referred to as helpers (You may require their help in any context within your code).

class x
{
// ...
    public function getPost()
    {
        $user_id = 1;

        if (isset($_POST['action'])) {
            $post_id = $_POST['post_id'];

            // ...

            echo Self::getRating($post_id);

            exit(0);
        }

        // ...

        return $posts;
    }

    private static function getLikes($id)
    {
        // ..
    }

    private static function getDislikes($id)
    {
        // ...
    }

    private static function getRating($id)
    {
        // ...
    }

    private static function userLiked($post_id)
    {
        // ...
    }

    private static function userDisliked($post_id)
    {
        //...
    }
// ...
}
  •  Tags:  
  • php
  • Related