Jump to content

conversion question


ruler

Recommended Posts

hello this is my first post here so i hope i have posted in the right section of the forum  :)

 

ive recently aquired a good working 5 star rating script written in php but its texed based and saves the data to a text file/s, i was wondering if it were posible to get it converted to work in a mysql database instead? when it saves to a text file it saves like this below...

 

2|bob

5|timmy

1|roger

1|edd

 

i have setup a database and a table for it already. connecting to the database is no problem at all. i was told there are some clever people on here that might be able to help me  :)

thank you for your time

Link to comment
https://forums.phpfreaks.com/topic/98073-conversion-question/
Share on other sites

maybe it might help if i posted the code.

 

$rater_id=$hubnum; // hub ID
$rater_item_name=$row['Name']; // hub name
$loggedinname = ($userdata['session_logged_in']) ? $userdata['username'] : ''; // get logged in users name

// User settings
$rater_nick_voting_restriction = true; // restrict nick address voting (true or false)
$rater_nick_vote_qty=1; // how many times an nick address can vote
$rater_already_rated_msg="You have already voted on this hub. You were allowed ".$rater_nick_vote_qty." vote ";
$rater_not_selected_msg="You have not selected a voting value.";
$rater_thankyou_msg="Thanks for voting.";
$rater_generic_text="this hub"; // generic item text
$newline="\n"; // may want to change for different operating systems

if(!isset($rater_id)) $rater_id=1; //set id to 1 if no hub id detected
if(!isset($rater_item_name)) $rater_item_name=$rater_generic_text; // use default poll name if none is present

// DO NOT MODIFY BELOW THIS LINE
$rater_filename='item_'.$rater_id.".rating"; // filename.ext
$rater_rating=0;
$rater_stars="";
$rater_stars_txt="";
$rater_votes=0;
$rater_msg="";

// Rating action
	if(isset($_REQUEST["rate".$rater_id])){
		if(isset($_REQUEST["rating_".$rater_id])){
			while(list($key,$val)=each($_REQUEST["rating_".$rater_id])){
			$rater_rating=$val;
			}
		$rater_nick = $loggedinname; 
		$rater_file=fopen($rater_filename,"a+");
		$rater_str="";
		$rater_str = rtrim(fread($rater_file, 1024*,$newline);
			if($rater_str!=""){
				if($rater_nick_voting_restriction){
				$rater_data=explode($newline,$rater_str);
				$rater_nick_vote_count=0;
					foreach($rater_data as $d){
					$rater_tmp=explode("|",$d);
					$rater_oldnick=str_replace($newline,"",$rater_tmp[1]);
						if($rater_nick==$rater_oldnick){
						$rater_nick_vote_count++;
						}
					}
					if($rater_nick_vote_count > ($rater_nick_vote_qty - 1)){ //if user exceeds vote limit
					$rater_msg=$rater_already_rated_msg;
					}else{
					fwrite($rater_file,$rater_rating."|".$rater_nick.$newline);
					$rater_msg=$rater_thankyou_msg;
					}
				}else{
				fwrite($rater_file,$rater_rating."|".$rater_nick.$newline);
				$rater_msg=$rater_thankyou_msg;
				}
			}else{
			fwrite($rater_file,$rater_rating."|".$rater_nick.$newline);
			$rater_msg=$rater_thankyou_msg;
			}
		fclose($rater_file);
		}else{
		$rater_msg=$rater_not_selected_msg;
		}
	}

// Get current rating
if(is_file($rater_filename)){ // if file exists then do
	$rater_file=fopen($rater_filename,"r"); // open file
	$rater_str=""; // empty string variable
	$rater_str = fread($rater_file, 1024*; //read whole file
		if($rater_str!=""){ // if string not empty then do
		$rater_data=explode($newline,$rater_str); // split string
		$rater_votes=count($rater_data)-1;	
		$rater_sum=0;
			foreach($rater_data as $d){
			$d=explode("|",$d);
			$rater_sum+=$d[0];
			}
		$rater_rating=number_format(($rater_sum/$rater_votes), 2, '.', '');
		}
	fclose($rater_file);
	}else{
echo "$rater_file";
	$rater_file=fopen($rater_filename,"w");
	fclose($rater_file);
}

// Assign star image
if ($rater_rating <= 0  ){$rater_stars = "./img/00star.gif";$rater_stars_txt="Not Rated";}
if ($rater_rating >= 0.5){$rater_stars = "./img/05star.gif";$rater_stars_txt="0.5";}
if ($rater_rating >= 1  ){$rater_stars = "./img/1star.gif";$rater_stars_txt="1";}
if ($rater_rating >= 1.5){$rater_stars = "./img/15star.gif";$rater_stars_txt="1.5";}
if ($rater_rating >= 2  ){$rater_stars = "./img/2star.gif";$rater_stars_txt="2";}
if ($rater_rating >= 2.5){$rater_stars = "./img/25star.gif";$rater_stars_txt="2.5";}
if ($rater_rating >= 3  ){$rater_stars = "./img/3star.gif";$rater_stars_txt="3";}
if ($rater_rating >= 3.5){$rater_stars = "./img/35star.gif";$rater_stars_txt="3.5";}
if ($rater_rating >= 4  ){$rater_stars = "./img/4star.gif";$rater_stars_txt="4";}
if ($rater_rating >= 4.5){$rater_stars = "./img/45star.gif";$rater_stars_txt="4.5";}
if ($rater_rating >= 5  ){$rater_stars = "./img/5star.gif";$rater_stars_txt="5";}

// Output
echo '<div class="hreview">';
echo '<form method="post" action="'.$_SERVER["PHP_SELF"].'?id='. $hubnum .'">';
echo '<h3 class="item">Rate <span class="fn">'.$rater_item_name.'</span></h3>';
echo '<div>';
echo '<span  class="rating"><img src="'.$rater_stars.'?x='.uniqid((double)microtime()*1000000,1).'" alt="'.$rater_stars_txt.' stars" /> Overall rating: '.$rater_stars_txt.'</span> from <span class="reviewcount"> '.$rater_votes.' votes</span>.';
echo '</div>';
echo '<div>';
echo '<label for="rate5_'.$rater_id.'"><input type="radio" value="5" name="rating_'.$rater_id.'[]" id="rate5_'.$rater_id.'" />Excellent</label>';
echo '<label for="rate4_'.$rater_id.'"><input type="radio" value="4" name="rating_'.$rater_id.'[]" id="rate4_'.$rater_id.'" />Very Good</label>';
echo '<label for="rate3_'.$rater_id.'"><input type="radio" value="3" name="rating_'.$rater_id.'[]" id="rate3_'.$rater_id.'" />Good</label>';
echo '<label for="rate2_'.$rater_id.'"><input type="radio" value="2" name="rating_'.$rater_id.'[]" id="rate2_'.$rater_id.'" />Fair</label>';
echo '<label for="rate1_'.$rater_id.'"><input type="radio" value="1" name="rating_'.$rater_id.'[]" id="rate1_'.$rater_id.'" />Poor</label>';
echo '<input type="hidden" name="rs_id" value="'.$rater_id.'" />';
echo '<input type="submit" name="rate'.$rater_id.'" value="Rate" />';
echo '</div>';
if($rater_msg!="") echo "<div>".$rater_msg."</div>";
echo '</form>';
echo '</div>';

 

if i am posting in the wrong section i do appologise, im not sure of the right place to ask. thank you

Link to comment
https://forums.phpfreaks.com/topic/98073-conversion-question/#findComment-501947
Share on other sites

Hello,

 

This is my first post too-  Hello all you insane coders!

 

Here's kinda what I'd do; I admit I'm not 100% on what your code is doing, and I haven't tested this yet, but it should get you somewhere near the right track...

 

require_once("db.php");

// ....

$rater_id=$hubnum; // hub ID
$rater_item_name=$row['Name']; // hub name
$loggedinname = ($userdata['session_logged_in']) ? $userdata['username'] : ''; // get logged in users name

// User settings
$rater_nick_voting_restriction = true; // restrict nick address voting (true or false)
$rater_nick_vote_qty=1; // how many times an nick address can vote
$rater_already_rated_msg="You have already voted on this hub. You were allowed ".$rater_nick_vote_qty." vote ";
$rater_not_selected_msg="You have not selected a voting value.";
$rater_thankyou_msg="Thanks for voting.";
$rater_generic_text="this hub"; // generic item text
$newline="\n"; // may want to change for different operating systems

if(!isset($rater_id)) $rater_id=1; //set id to 1 if no hub id detected
  if(!isset($rater_item_name)) $rater_item_name=$rater_generic_text; // use default poll name if none is present

    // DO NOT MODIFY BELOW THIS LINE
    $rater_rating=0;
    $rater_stars="";
    $rater_stars_txt="";
    $rater_votes=0;
    $rater_msg="";

    db_Open();

    // Rating action
    if(isset($_REQUEST["rate".$rater_id])) {

      $db_table = 'Table Name';
      $db_rater_id = 'Rater Id column name';
      $db_rater_rating = 'Rater rating column name';
      $db_rater_nick = 'Rater nick column name';

      if(isset($_REQUEST["rating_".$rater_id])) {

        while(list($key,$val)=each($_REQUEST["rating_".$rater_id])){
          $rater_rating=$val;
}

$rater_nick = $loggedinname;
        
        if ($rater_nick_voting_restriction) {
          $rater_hasAlreadyVoted_result = db_Query('SELECT * FROM ' . $db_table . ' WHERE ' . $db_rater_id . '=' . $rater_id . ' AND ' . $db_rater_nick . '=' . $rater_nick);

          if (db_countRows($rater_hasAlreadyVoted_result) > $rater_nick_vote_qty - 1) {
            $rater_msg = $rater_already_rated_msg;
          }
        }

        if ($rater_msg != $rater_already_rated_msg) {
          db_Query('INSERT INTO ' . $db_table . ' VALUES(' . $rater_id . ', ' . $rater_rating . ', ' . $rater_nick . ')';
          $rater_msg = $rater_thankyou_msg;
        }
      }
    }

    else {
      $rater_msg = $rater_not_selected_msg;
    }


    // Get current rating
    
    $rater_rating_result = db_Query('SELECT (SUM(' . $db_rater_rating . ') / COUNT(*)) FROM ' . $db_table . ' WHERE ' . $db_rater_id . '=' . $rater_id);

    dbClose();

    if ($rater_rating_arr = db_FetchArray($rater_rating_result)) {
      $rater_rating = $rater_rating_arr[0];
    
      if (!$rater_rating) {
        $rater_rating = 0;
      }
    }
    else {
      $rater_rating = 0;
    }

    $rater_rating = number_format($rater_rating, 2, '.', '');
      
    // Assign star image
    if ($rater_rating <= 0  ){$rater_stars = "./img/00star.gif";$rater_stars_txt="Not Rated";}
    if ($rater_rating >= 0.5){$rater_stars = "./img/05star.gif";$rater_stars_txt="0.5";}
    if ($rater_rating >= 1  ){$rater_stars = "./img/1star.gif";$rater_stars_txt="1";}
    if ($rater_rating >= 1.5){$rater_stars = "./img/15star.gif";$rater_stars_txt="1.5";}
    if ($rater_rating >= 2  ){$rater_stars = "./img/2star.gif";$rater_stars_txt="2";}
    if ($rater_rating >= 2.5){$rater_stars = "./img/25star.gif";$rater_stars_txt="2.5";}
    if ($rater_rating >= 3  ){$rater_stars = "./img/3star.gif";$rater_stars_txt="3";}
    if ($rater_rating >= 3.5){$rater_stars = "./img/35star.gif";$rater_stars_txt="3.5";}
    if ($rater_rating >= 4  ){$rater_stars = "./img/4star.gif";$rater_stars_txt="4";}
    if ($rater_rating >= 4.5){$rater_stars = "./img/45star.gif";$rater_stars_txt="4.5";}
    if ($rater_rating >= 5  ){$rater_stars = "./img/5star.gif";$rater_stars_txt="5";}

    // Output
    echo '<div class="hreview">';
    echo '<form method="post" action="'.$_SERVER["PHP_SELF"].'?id='. $hubnum .'">';
    echo '<h3 class="item">Rate <span class="fn">'.$rater_item_name.'</span></h3>';
    echo '<div>';
    echo '<span  class="rating"><img src="'.$rater_stars.'?x='.uniqid((double)microtime()*1000000,1).'" alt="'.$rater_stars_txt.' stars" /> Overall rating: '.$rater_stars_txt.'</span> from <span class="reviewcount"> '.$rater_votes.' votes</span>.';
    echo '</div>';
    echo '<div>';
    echo '<label for="rate5_'.$rater_id.'"><input type="radio" value="5" name="rating_'.$rater_id.'[]" id="rate5_'.$rater_id.'" />Excellent</label>';
    echo '<label for="rate4_'.$rater_id.'"><input type="radio" value="4" name="rating_'.$rater_id.'[]" id="rate4_'.$rater_id.'" />Very Good</label>';
    echo '<label for="rate3_'.$rater_id.'"><input type="radio" value="3" name="rating_'.$rater_id.'[]" id="rate3_'.$rater_id.'" />Good</label>';
    echo '<label for="rate2_'.$rater_id.'"><input type="radio" value="2" name="rating_'.$rater_id.'[]" id="rate2_'.$rater_id.'" />Fair</label>';
    echo '<label for="rate1_'.$rater_id.'"><input type="radio" value="1" name="rating_'.$rater_id.'[]" id="rate1_'.$rater_id.'" />Poor</label>';
    echo '<input type="hidden" name="rs_id" value="'.$rater_id.'" />';
    echo '<input type="submit" name="rate'.$rater_id.'" value="Rate" />';
    echo '</div>';
    if($rater_msg!="") echo "<div>".$rater_msg."</div>";
    echo '</form>';
    echo '</div>';
  }
}

 

And the db file...

 

<?php
//db.php

$serverName = 'Your server';
$userName = 'Your username';
$passWord = 'Your pw';

function db_Open() {
  mysql_connect($serverName, $userName, $passWord);
}

function db_Close() {
  mysql_close();
}

function db_CountRows($result) {
  return mysql_num_rows($result);
}

function db_FetchArray($result) {
  return mysql_fetch_array($result);
}

function db_Query($query) {
  return mysql_query($query);
}
?>

 

I'm really tired so the obvious may be overlooked.  I'm assuming your mysql table has 3 columns for the id, rating, and nickname, and that duplicates on all columns are allowed.  In any case, I'll check back tomorrow evening.

Link to comment
https://forums.phpfreaks.com/topic/98073-conversion-question/#findComment-502013
Share on other sites

$rater_id is basically getting the page id such as index.php?id=6 so it knows the ID number of the item being voted on

$rater_item_name gets the name from of the item being voted on, if there is no name in the db then it will use a default name in its place.

 

the above are actually being pulled from another mysql db that is fully working.

my mysql db looks something like below...

 

 

db = forum

pbpbb_users

phpbb_topics

phpbb_votes_hubs

***id

***name

***voting character set utf8 collate utf8_unicode_ci NOT NULL

 

its been intergrated into a phpbb forum . i hope this makes a little more sense. it should only read and write from the collum 'voting' so it saves/gets the info the same as if it were from the text file. it should store only what the user rated and the user who rates name so if the user voted a 5 then it will save like this 5|roger

thanks for your reply

Link to comment
https://forums.phpfreaks.com/topic/98073-conversion-question/#findComment-502049
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.