Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2008
    Posts
    16

    Post Unanswered: posible to convert script?

    hello again
    ive tried and ive tried but to no avail, im getting nowhere so i thought i'd ask here (if this is the right place to ask)

    i have a working voting script but it saves/loads results to/from text files is it posible for someone to help convert this script to save to a MySQL db? i have setup the db already but just need help converting this. There are 2 files, one small.

    rater.php
    Code:
    <?
    
    // User settings
    $rater_ip_voting_restriction = true; // restrict ip address voting (true or false)
    $rater_ip_vote_qty=1; // how many times an ip address can vote
    $rater_already_rated_msg="You have already rated this item. You were allowed ".$rater_ip_vote_qty." vote(s).";
    $rater_not_selected_msg="You have not selected a rating value.";
    $rater_thankyou_msg="Thankyou for voting.";
    $rater_generic_text="this item"; // generic item text
    $rater_end_of_line_char="\n"; // may want to change for different operating systems
    
    
    
    if(!isset($rater_id)) $rater_id=1;
    if(!isset($rater_item_name)) $rater_item_name=$rater_generic_text;
    
    
    // DO NOT MODIFY BELOW THIS LINE
    $rater_filename='item_'.$rater_id.".rating";
    $rater_rating=0;
    $rater_stars="";
    $rater_stars_txt="";
    $rater_rating=0;
    $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_ip = getenv("REMOTE_ADDR"); 
      $rater_file=fopen($rater_filename,"a+");
      $rater_str="";
      $rater_str = rtrim(fread($rater_file, 1024*8),$rater_end_of_line_char);
      if($rater_str!=""){
       if($rater_ip_voting_restriction){
        $rater_data=explode($rater_end_of_line_char,$rater_str);
    	$rater_ip_vote_count=0;
        foreach($rater_data as $d){
    	 $rater_tmp=explode("|",$d);
    	 $rater_oldip=str_replace($rater_end_of_line_char,"",$rater_tmp[1]);
    	 if($rater_ip==$rater_oldip){
    	  $rater_ip_vote_count++;
    	 }
        }
    	if($rater_ip_vote_count > ($rater_ip_vote_qty - 1)){
         $rater_msg=$rater_already_rated_msg;
    	}else{
         fwrite($rater_file,$rater_rating."|".$rater_ip.$rater_end_of_line_char);
         $rater_msg=$rater_thankyou_msg;
    	}
       }else{
        fwrite($rater_file,$rater_rating."|".$rater_ip.$rater_end_of_line_char);
        $rater_msg=$rater_thankyou_msg;
       }
      }else{
       fwrite($rater_file,$rater_rating."|".$rater_ip.$rater_end_of_line_char);
       $rater_msg=$rater_thankyou_msg;
      }
      fclose($rater_file);
     }else{
      $rater_msg=$rater_not_selected_msg;
     }
    }
    
    // Get current rating
    if(is_file($rater_filename)){
     $rater_file=fopen($rater_filename,"r");
     $rater_str="";
     $rater_str = fread($rater_file, 1024*8);
     if($rater_str!=""){
      $rater_data=explode($rater_end_of_line_char,$rater_str);
      $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{
     $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"].'">';
    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" /> Ave. 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>';
    
    ?>
    test.php
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title> New Document </title>
    <meta name="Author" content="">
    <meta name="Keywords" content="">
    <meta name="Description" content="">
    </head>
    
    <body>
    
    <?
    $rater_id=1;
    $rater_item_name='Item 1';
    include("rater.php");
    ?>
    
    <?
    $rater_id=2;
    $rater_item_name='Item 2';
    include("rater.php");
    ?>
    
    </body>
    </html>
    when it saves to a text file it will save a value from 1-5 and then the user's ip address seperated by a pipe in the following manner

    2|teddy
    5|bob
    1|richard

    any help on this would be very welcome. thanks

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    what have you tried so far
    how do you stop someone voting more thant once

  3. #3
    Join Date
    Mar 2008
    Posts
    16
    hi, the line below (about 2nd or third from the top of the first script) limits the amount of votes an IP can make, the line above that enables vote limiting or disables it. here is the line that limits the votes:

    $rater_ip_vote_qty=1; // how many times an ip address can vote

    i will try what ever will work although my attempts are futile the lines in the last script:

    <?
    $rater_id=2;
    $rater_item_name='Item 2';
    include("rater.php");
    ?>

    can be obmitted as that will just add a new voting file for another topic. i havent tried much as i know little about mysql or database stuff. i am trying to learn the best i can so i dont have to bother others.
    thanks for your time

  4. #4
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    Don't bother converting, it'll cause you more headaches than it's worth.

    What is your database layout? This is key really to how you're going to interact with the information.

    There are a series of steps to take
    1) Connect to database
    2) Allow voter to choose option
    3) Take option and INSERT into database (with ip address)
    4) Display voters choice + general stats

    On display check whether voter IP exists, if it does, just display the results, otherwise display the voting options. Also, on insert to the same check.

    It's not that hard to be honest. First I recommend you begin with learning how to connect to a database. Then learn how to insert and select information using PHP+SQL.

  5. #5
    Join Date
    Mar 2008
    Posts
    16
    its ok its all sorted out now, someone did it through remote assistance. i was messing around with it for so long that it began to give me headaches and someone did it in less than half an hour
    i dont know what they did to be honist but they made several changes all over so its hard to list here all the changes they made but thanks for your replies and efforts, much appreciated
    i guess this topic is now solved. thanks once again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •