Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class MyDB extends SQLite3 {
- function __construct() {
- $this->open('Rankings.db');
- }
- }
- //FUNCTIONS
- function usage(){
- echo("SYNOPSIS\n\t php ranking.php [OPTION]... [TABLE]internal/external");
- echo("\nDESCRIPTION");
- echo("\n\t-i\n\t\t Insert new participant [NAME]");
- echo("\n\t-a\n\t\t add points to specified id of a participant [ID] [POINTS]");
- echo("\n\t-d\n\t\t Delete a row given an ID [ID]");
- echo("\n\t-g\n\t\t Returns score and position of a participant for a given id [ID]");
- echo("\n\t-G\n\t\t Returns current ranking(internal/external)");
- echo("\n\t-ni\n\t\t Returns id by a given name");
- echo("\n\t-r\n\t\t Resets the score of a specified id. [ID]");
- echo("\n\t-R\n\t\t Reset the score of everyone at the table.");
- echo("\n");
- }
- //gets all values from all rows
- function getRanking($db, $table){
- $sql =<<<EOF
- SELECT * FROM $table;
- EOF;
- $ret = $db->query($sql);
- while($row = $ret->fetchArray(SQLITE3_ASSOC) ) {
- echo "ID: ". $row['ID'] . "\n";
- echo "NAME: ". $row['NAME'] . "\n";
- echo "SCORE: ". $row['SCORE'] . "\n";
- echo "POSITION: ". $row['POSITION'] . "\n\n";
- }
- echo("Operation done successfully\n");
- }
- //get score and position given an id
- function getRankingId($db, $table, $id){
- $sql =<<<EOF
- SELECT SCORE, POSITION FROM $table WHERE ID = $id;
- EOF;
- $ret = $db->query($sql);
- $row = $ret->fetchArray(SQLITE3_ASSOC);
- echo($row['SCORE'] . "\n");
- echo($row['POSITION'] . "\n");
- echo("Operation done successfully\n");
- }
- //get id by providen name
- function getNameId($db, $table, $name){
- $sql =<<<EOF
- SELECT ID FROM $table WHERE NAME = '$name';
- EOF;
- $ret = $db->query($sql);
- $row = $ret->fetchArray(SQLITE3_ASSOC);
- echo($row['ID'] . "\n");
- echo("Operation done successfully\n");
- }
- //insert new row on specified table
- function insertRow($db, $table, $name){
- $db->exec("INSERT INTO $table (NAME, SCORE, POSITION) VALUES ('$name', 0,9999)");
- }
- //delete row by id
- function deleteRow($db, $table, $id){
- $db->exec("DELETE FROM $table WHERE ID=$id");
- }
- //add points to current score by id
- function addScore($db, $table, $id, $points){
- $sql =<<<EOF
- UPDATE $table set SCORE = SCORE + $points WHERE ID=$id;
- EOF;
- $ret = $db->exec($sql);
- if(!$ret){
- echo $db->lastErrorMsg();
- } else {
- echo $db->changes();
- }
- }
- //Reset the score of all current users
- function resetAllScore($db, $table){
- $sql =<<<EOF
- UPDATE $table set SCORE=0;
- EOF;
- $ret = $db->exec($sql);
- if(!$ret){
- echo $db->lastErrorMsg();
- } else {
- echo $db->changes();
- }
- }
- //Reset the score of a concrent user
- function resetConcretScore($db, $table, $id){
- $sql =<<<EOF
- UPDATE $table set SCORE=0 WHERE ID = $id;
- EOF;
- $ret = $db->exec($sql);
- if(!$ret){
- echo $db->lastErrorMsg();
- } else {
- echo $db->changes();
- }
- }
- function showArgumentError(){
- echo("Something went wrong with arguments.");
- }
- //FUNCTIONS END
- //principal arguments
- if(isset($argv[1])){
- $command = $argv[1];
- if($argv[1] == "--help"){
- usage();
- exit;
- }
- }
- else{
- echo "You must specify a command. Type --help for help\n";
- exit;
- }
- if( isset($argv[2]) ) {
- if($argv[2] == "external")
- $table = "PUBLICSCORE";
- else if($argv[2] == "internal")
- $table = "PRIVATESCORE";
- }
- else
- echo "Bad arguments number.";
- //Database
- $db = new MyDB();
- if(!$db){
- echo $db->lastErrorMsg();
- } else {
- echo("Opened database succesfully\n");
- }
- $name = 'maria';
- //function calls and arguments assigns
- switch($argv[1]){
- //insert
- case "-i":
- //name
- if( isset($argv[3]) )
- $name = $argv[3];
- else{
- showArgumentError();
- exit;
- }
- insertRow($db, $table, $name);
- break;
- //ADD
- case "-a":
- //id
- if( isset($argv[3]) )
- $id = $argv[3];
- else{
- showArgumentError();
- exit;
- }
- //points to add
- if( isset($argv[4]) )
- $points = $argv[4];
- else{
- showArgumentError();
- exit;
- }
- addScore($db, $table, $id, $points);
- break;
- //delete row by id
- case "-d":
- if( isset($argv[3]) )
- $id = $argv[3];
- else{
- showArgumentError();
- exit;
- }
- deleteRow($db, $table, $id);
- break;
- //get ranking by id
- case "-g":
- //id
- if( isset($argv[3]) )
- $id = $argv[3];
- else{
- showArgumentError();
- exit;
- }
- getRankingId($db, $table, $id);
- break;
- //getranking
- case "-G":
- getRanking($db, $table);
- break;
- case "-ni":
- if( isset($argv[3]) )
- $name = $argv[3];
- else{
- showArgumentError();
- exit;
- }
- getNameId($db, $table, $name);
- break;
- //reset by id
- case "-r":
- //id
- if( isset($argv[3]) )
- $id = $argv[3];
- else{
- showArgumentError();
- exit;
- }
- resetConcretScore($db, $table, $id);
- break;
- //reset all rows
- case "-R":
- resetAllScore($db, $table);
- break;
- }
- $db->close();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement