Page 1 of 1

conversion question

Posted: Wed Mar 26, 2008 6:40 pm
by ruler
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

Re: conversion question

Posted: Thu Mar 27, 2008 1:05 am
by flying_circus
Not sure what your qustion is. Of course you can convert your script to access a db but we cant help you without having seen the code.

Re: conversion question

Posted: Thu Mar 27, 2008 1:26 am
by ruler

Code: Select all

maybe it might help if i posted the code.
 

Code: Select all

$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*8),$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*8); //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

i think thats pretty much most of it. $hubnum is basically just something like index.php?id=2 where the bold part is the $hubnum. ive been looking at the same code for most of the week :roll: im still learning php and have a long way to go. maybe a finnished working version might be of some use to someone else? i know this works perfect as it is on mine.
thanks for your time

Re: conversion question

Posted: Sun Mar 30, 2008 6:44 pm
by AMCH
I had a brief look down your code and came to this on line 59:

Code: Select all

fwrite($rater_file,$rater_rating."|".$rater_nick.$newline);
$rater_rating and $rater_nick need to be written to your database at this point.

Code: Select all

// assign database connection variables
$host = something;
$username = something;
$password = something;
 
// connect to your database
mysql_connect("$host", "$username", "$password") or die(mysql_error());
 
// select the database you will work with
mysql_select_db("database_to_work_with") or die(mysql_error());
 
// insert into the table
mysql_query("INSERT INTO your_table_name VALUES('$rater_rating','$rater_nick')") or die(mysql_error());
Hope this helps

Kind Regards
AMCH