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 133751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:29:21+00:00 2026-05-11T06:29:21+00:00

I am trying to build a site with news links that can be voted,

  • 0

I am trying to build a site with news links that can be voted, I have the following code:

 case 'vote':                 require_once('auth/auth.php');                 if(Auth::isUserLoggedIn())                 {                     require_once('data/article.php');                     require_once('includes/helpers.php');                      $id = isset($_GET['param'])? $_GET['param'] : 0;                     if($id > 0)                     {                         $article = Article::getById($id);                         $article->vote();                         $article->calculateRanking();                     }                     if(!isset($_SESSION)) session_start();                     redirectTo($_SESSION['action'], $_SESSION['param']);                 }                 else                 {                     Auth::redirectToLogin();                  }                 break; 

The problem right now is how to check so the same user does not vote twice, here is the article file:

<?php require_once($_SERVER['DOCUMENT_ROOT'].'/config.php');  require_once(SITE_ROOT.'includes/exceptions.php');  require_once(SITE_ROOT.'data/model.php'); require_once(SITE_ROOT.'data/comment.php');  class Article extends Model  {     private $id;     private $user_id;     private $url;     private $title;     private $description;     private $ranking;     private $points;      function __construct($title = ' ', $description = ' ', $url = ' ', $username = ' ', $created = ' ', $modified = '') {          $this->setId(0);         $this->setCreated($created);         $this->setModified($modified);         $this->setUsername($username);         $this->setUrl($url);         $this->setTitle($title);         $this->setDescription($description);         $this->setRanking(0.0);         $this->setPoints(1);      }      function getId(){         return $this->id;     }      private function setId($value){         $this->id = $value;     }      function getUsername(){         return $this->username;     }      function setUsername($value){         $this->username = $value;     }      function getUrl(){         return $this->url;     }      function setUrl($value){         $this->url = $value;     }      function getTitle()     {         return $this->title;     }      function setTitle($value) {          $this->title = $value;     }      function getDescription() {      return $this->description;     }      function setDescription($value)     {         $this->description = $value;     }      function getPoints()     {         return $this->points;     }      function setPoints($value)      {         $this->points = $value;     }      function getRanking()     {         return $this->ranking;     }      function setRanking($value)     {         $this->ranking = $value;     }      function calculateRanking()     {         $created = $this->getCreated();          $diff = $this->getTimeDifference($created, date('F d, Y h:i:s A'));          $time = $diff['days'] * 24;         $time += $diff['hours'];         $time += ($diff['minutes'] / 60);         $time += (($diff['seconds'] / 60)/60);          $base = $time + 2;                  $this->ranking = ($this->points - 1) / pow($base, 1.5);          $this->save();     }      function vote()     {         $this->points++;         $this->save();     }      function getUrlDomain()     {         /* We extract the domain from the URL          * using the following regex pattern          */          $url = $this->getUrl();         $matches = array();          if(preg_match('/http:\/\/(.+?)\//', $url, $matches))         {             return $matches[1];         }         else         {             return $url;         }     }      function getTimeDifference( $start, $end )     {         $uts['start']      =    strtotime( $start );         $uts['end']        =    strtotime( $end );         if( $uts['start']!==-1 && $uts['end']!==-1 )         {             if( $uts['end'] >= $uts['start'] )             {                 $diff    =    $uts['end'] - $uts['start'];                 if( $days=intval((floor($diff/86400))) )                     $diff = $diff % 86400;                 if( $hours=intval((floor($diff/3600))) )                     $diff = $diff % 3600;                 if( $minutes=intval((floor($diff/60))) )                     $diff = $diff % 60;                 $diff    =    intval( $diff );                 return( array('days'=>$days, 'hours'=>$hours, 'minutes'=>$minutes, 'seconds'=>$diff) );             }             else             {                 echo( 'Ending date/time is earlier than the start date/time');             }         }         else         {             echo( 'Invalid date/time data detected');         }         return( false );     }        function getElapsedDateTime()     {         $db = null;         $record = null;          $record = Article::getById($this->id);         $created = $record->getCreated();                   $diff = $this->getTimeDifference($created, date('F d, Y h:i:s A'));         //echo 'new date is '.date('F d, Y h:i:s A');         //print_r($diff);          if($diff['days'] > 0 )         {             return sprintf('hace %d dias', $diff['days']);         }         else if($diff['hours'] > 0 )         {             return sprintf('hace %d horas', $diff['hours']);         }         else if($diff['minutes'] > 0 )         {             return sprintf('hace %d minutos', $diff['minutes']);         }         else         {             return sprintf('hace %d segundos', $diff['seconds']);         }     }      function save() {      /*             Here we do either a create or             update operation depending             on the value of the id field.             Zero means create, non-zero             update     */          if(!get_magic_quotes_gpc())         {             $this->title = addslashes($this->title);             $this->description = addslashes($this->description);         }          try         {             $db = parent::getConnection();             if($this->id == 0 )             {                 $query = 'insert into articles (modified, username, url, title, description, points )';                 $query .= ' values ('$this->getModified()', '$this->username', '$this->url', '$this->title', '$this->description', $this->points)';             }             else if($this->id != 0)             {                 $query = 'update articles set modified = NOW()'.', username = '$this->username', url = '$this->url', title = ''.$this->title.'', description = ''.$this->description.'', points = $this->points, ranking = $this->ranking where id = $this->id';             }              $lastid = parent::execSql2($query);              if($this->id == 0 )                 $this->id = $lastid;          }         catch(Exception $e){             throw $e;         }     }       function delete()     {         try         {             $db = parent::getConnection();             if($this->id != 0)             {                ;                 /*$comments = $this->getAllComments();                 foreach($comments as $comment)                 {                     $comment->delete();                 }*/                 $this->deleteAllComments();                 $query = 'delete from articles where id = $this->id';             }             parent::execSql($query);           }         catch(Exception $e){             throw $e;         }     }      static function getAll($conditions = ' ')     {         /* Retrieve all the records from the          * database according subject to          * conditions          */          $db = null;         $results = null;         $records = array();         $query = 'select id, created, modified, username, url, title, description, points, ranking from articles $conditions';         try         {             $db = parent::getConnection();              $results = parent::execSql($query);              while($row = $results->fetch_assoc())             {                 $r_id = $row['id'];                 $r_created = $row['created'];                 $r_modified = $row['modified'];                  $r_title = $row['title'];                 $r_description = $row['description'];                  if(!get_magic_quotes_gpc())                 {                     $r_title = stripslashes($r_title);                     $r_description = stripslashes($r_description);                 }                  $r_url = $row['url'];                 $r_username = $row['username'];                 $r_points = $row['points'];                 $r_ranking = $row['ranking'];                  $article = new Article($r_title, $r_description , $r_url, $r_username, $r_created, $r_modified);                 $article->id = $r_id;                 $article->points = $r_points;                 $article->ranking = $r_ranking;                 $records[] = $article;             }             parent::closeConnection($db);         }         catch(Exception $e)         {             throw $e;         }          return $records;     }      static function getById($id)     {/*      *  Return one record from the database by its id */          $db = null;         $record = null;          try         {             $db = parent::getConnection();             $query = 'select id, username, created, modified, title, url, description, points, ranking from articles where id = $id';             $results = parent::execSQL($query);             if(!$results) {                 throw new Exception ('Record not found', EX_RECORD_NOT_FOUND);             }              $row = $results->fetch_assoc();             parent::closeConnection($db);              if(!get_magic_quotes_gpc())             {                 $row['title'] = stripslashes($row['title']);                 $row['description'] = stripslashes($row['description']);             }               $article = new Article($row['title'], $row['description'], $row['url'], $row['username'], $row['created'], $row['modified']);             $article->id = $row['id'];             $article->points = $row['points'];             $article->ranking = $row['ranking'];              return $article;          }         catch (Exception $e){             throw $e;         }     }      static function getNumberOfComments($id)     {/*      *  Return one record from the database by its id */          $db = null;         $record = null;          try         {             $db = parent::getConnection();             $query = 'select count(*) as 'total' from comments where article_id = $id';             $results = parent::execSQL($query);             if(!$results) {                 throw new Exception ('Comments Count Query Query Failed', EX_QUERY_FAILED);             }              $row = $results->fetch_assoc();             $total = $row['total'];             parent::closeConnection($db);                  return $total;           }         catch (Exception $e){             throw $e;         }     }      function deleteAllComments()     {/*      *  Return one record from the database by its id */          $db = null;         try         {             $db = parent::getConnection();             $query = 'delete from comments where article_id = $this->id';             $results = parent::execSQL($query);             if(!$results) {                 throw new Exception ('Deletion Query Failed', EX_QUERY_FAILED);             }             parent::closeConnection($db);         }         catch (Exception $e){             throw $e;         }     }       function getAllComments($conditions = ' ')     {         /* Retrieve all the records from the          * database according subject to          * conditions          */         $conditions = 'where article_id = $this->id';         $comments = Comment::getAll($conditions);         return $comments;      }       static function getTestData($url)     {         $page = file_get_contents($url);         }  } ?> 

Any suggestion or comment is appreciated, Thanks.

  • 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. 2026-05-11T06:29:21+00:00Added an answer on May 11, 2026 at 6:29 am

    make another table user_votes for example with structure: user_id int not null article_id int not null primary key (user_id, article_id)

    in vote function first try to insert and if insert is successfull then increase $this->points

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 62k
  • Answers 62k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer It's not expensive to construct a data context regardless of… May 11, 2026 at 10:15 am
  • added an answer A simple cast is all you need. Since it's possible… May 11, 2026 at 10:15 am
  • added an answer Easy done. INSERT INTO mytable (a, b, c) SELECT 2,… May 11, 2026 at 10:15 am

Related Questions

I'm trying to build the search for a Django site I am building, and
I am trying to build a function in C/C++ to sort an array and
I am trying to build a dependency graph of tables based on the foreign
I am trying to build a dropdown list for a winform interop, and I
I am trying to build a homebrew web brower to get more proficient at
I am trying to build a CAML query for SharePoint 2007 environment, to get
I am trying to build a board game ... And looks like it has
I am trying to build a working encrypted signature for the Amazon S3 web
I am using Exuberant Ctags 5.7. I am trying to build a tag database
I am trying to build out a useful 3d game engine out of the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.