Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9124355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:33:12+00:00 2026-06-17T06:33:12+00:00

This script is an open source tutorial on Net tuts+ but I can’t figure

  • 0

This script is an open source tutorial on Net tuts+ but I can’t figure out how to change the script to connect with a database instead of the text-file that was attached the documentation.

The script is here and I understand what is going on… but I am afraid that I might have to reprogram almost everything or write a lot of additional code, so before I begin to do that I would like to know if there is an easier way to change this.. For info I am using PDO and a hidden config.php file for database connection using $conn as the PDO-DB access.

The use of the class:

$rating = new ratings($_POST['widget_id']);
isset($_POST['fetch']) ? $rating->get_ratings() : $rating->vote();

The ratings class:

class ratings
{
    private $data_file = './ratings.data.txt';
    private $widget_id;
    private $data = array();


    function __construct($wid)
    {
        $this->widget_id = $wid;

        $all = file_get_contents($this->data_file);

        if ($all) {
            $this->data = unserialize($all);
        }
    }
    public function get_ratings()
    {
        if ($this->data[$this->widget_id]) {
            echo json_encode($this->data[$this->widget_id]);
        } else {
            $data['widget_id']    = $this->widget_id;
            $data['number_votes'] = 0;
            $data['total_points'] = 0;
            $data['dec_avg']      = 0;
            $data['whole_avg']    = 0;
            echo json_encode($data);
        }
    }
    public function vote()
    {
        # Get the value of the vote
        preg_match('/rate_([1-5]{1})/', $_POST['clicked_on'], $match);
        $vote = $match[1];

        $ID = $this->widget_id;
        # Update the record if it exists
        if ($this->data[$ID]) {
            $this->data[$ID]['number_votes'] += 1;
            $this->data[$ID]['total_points'] += $vote;
        }
        # Create a new one if it doesn't
        else {
            $this->data[$ID]['number_votes'] = 1;
            $this->data[$ID]['total_points'] = $vote;
        }

        $this->data[$ID]['dec_avg']   = round($this->data[$ID]['total_points'] / $this->data[$ID]['number_votes'], 1);
        $this->data[$ID]['whole_avg'] = round($this->data[$ID]['dec_avg']);


        file_put_contents($this->data_file, serialize($this->data));
        $this->get_ratings();
    }

    # ---
    # end class
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:33:13+00:00Added an answer on June 17, 2026 at 6:33 am

    This is just a sim. about how to, hope to helps you.

    <?
    class Vote
    {
        private $_db;
    
        public function __construct() {
            // needs a db class
            $this->_db = DB::getInstance();
            // open connection or use existing
            $this->_db->connected or $this->_db->connect();
        }
    
        public function sum() {
            // get id securely or set it as NULL (@ makes it NULL silently)
            @ $ID = (int) $_POST['ID'];
            // Expr: if id !== null and id !== 0
            if ($ID) {
                // simply fetch all votes 
                return $this->_db->fetch("
                            SELECT SUM(`vote_point`) AS sums 
                            FROM `votes`
                            WHERE `ID` = $ID
                            ORDER BY sums");
            }
        }
    
        public function insert() {
            // get id securely or set it as NULL (@ makes it NULL silently)
            @ $ID = (int) $_POST['ID'];
            // Expr: if id !== null and id !== 0
            if ($ID) {
                // get vote point securely or set it as NULL
                $votePoint = !isset($_POST['votePoint']) ? NULL : intval($_POST['votePoint']);
                // vote point is number, so not NULL?
                if (is_int($votePoint)) {
                    $this->_db->query("
                        INSERT INTO `votes` (`ID`, `vote_point`) 
                        VALUES ($ID, $votePoint)");
                }
                // yield last inserted row id (ID)
                return $this->_db->insertId();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can't run this open source project http://railsapps.github.com/tutorial-rails-bootstrap-devise-cancan.html.I 'm using Ruby version1.9.3p286 and Rails 3.2.8.
I'm trying to figure out how to apply a for-loop to this script and
I'm developing a asp.net web application and I have this script in source code:
I have found this script and cant seem to figure out why it throws
I compress my files using this script import pylzma, struct i = open(path+fileName,'rb') o
in my site i want to set default page to open, using this script
I have this code in userpage.php: <script langauge=JavaScript><!-- function newWindow(fileName,windowName) { msgWindow=window.open(fileName,windowName); } //--></script>
This script works fine on single click but on double click it shows both
I couldn't find a open source PHP Script that allow me to choose the
I am developing a (free, open-source) Entity Framework tool, it is basicaly an ADO.NET

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.