Jump to content

Help with this script I have written PLEASE


grunshaw

Recommended Posts

Hello there,

Ok, so here is the background for you good guys here at PHPFreaks. Basically, I have a radio station (http://www.natureradio.net) Nature Radio, which plays nature sounds, meditation msuic etc etc. Now, it has a voting script I have written up to rate the tracks that listeners hear. Now, what happens, is for some reason, the first person to vote always seems to cast 2 votes into the database. It seems that instead of taking a track that has 0 votes and adding 1 to make 1, it adds 2 to that 0. After this, the number does incriment by 1 each time as it should.

 

Here is the script, its a bit of a long one, but if anybody could recommend any changes to enhance it and certainly iron out this issue, that would be great. I have tried a few things, as i'm sure you can see, but it doesn't seem to fix this.

 

<div align="left">

<?

// Turn of Error Reporting
error_reporting(0);

// Style File
include("styles.php");

// Database File
include("db_connect.php");

// Track Data File
include("getsongdata.php");
?>

<div align="left">
Now Playing: <? echo $track ?></div>
<div align="left">
<?

// check to see if the current track is in the database

$query="SELECT * FROM trackrating where title='$track'";
$result=mysql_query($query) or die(mysql_error());
// Count the rows
$num=mysql_numrows($result);

// Enter into Loop for Values
$i=0;
while($i < $num){

// Get Values from Database
$id=mysql_result($result,$i, "id");
$like=mysql_result($result,$i, "like");
$dontlike=mysql_result($result,$i,  "dontlike");
$totalvotes=mysql_result($result, $i, "totalvotes");

$i++;
}






// if track found, show rating options

if($num == "1"){

// Calculate Percentages
$sum1=$like/$totalvotes*100;

$sum2=$dontlike/$totalvotes*100;

$sum1=number_format($sum1, 1, '.', '');

$sum2=number_format($sum2, 1, '.', '');


// show Rating Buttons
echo "<div class='mattblacktabs'>
<ul>
<li><strong>Rate This Track:</strong></li>
<li><a href='?like=$id'>Like It ($sum1%)</a></li>
<li><a href='?dontlike=$id'>Dont Like It ($sum2%)</a></li>
<li>(Total Votes $totalvotes)</li>
</ul>
</div>

";

// Get the Rating from the appropriate button
$getlike=$_GET['like'];
$getdontlike=$_GET['dontlike'];


// if the rating is a like
if($getlike == ""){ echo ""; } else {

if($like == "0"){
$update="UPDATE `trackrating` SET `like`='1' WHERE id='$getlike'";
mysql_query($update) or die(mysql_error()); 
echo "<meta http-equiv='refresh' content='0;url=index.php'>";}
else {
$new1=$like+1;
$update="UPDATE `trackrating` SET `like`='$new1' WHERE id='$getlike'";
mysql_query($update) or die(mysql_error());
echo "<meta http-equiv='refresh' content='0;url=index.php'>"; }

if($totalvotes == "0"){
$update2="UPDATE trackrating SET totalvotes='1' WHERE id='$getlike'";
mysql_query($update2) or die(mysql_error());
echo "<meta http-equiv='refresh' content='0;url=index.php'>";} else { 
$new2=$totalvotes+1;
$update2="UPDATE trackrating SET totalvotes='$new2' WHERE id='$getlike'";
mysql_query($update2) or die(mysql_error());
echo "<meta http-equiv='refresh' content='0;url=index.php'>"; }




}



// if the rating is a not like
if($getdontlike == ""){ echo ""; } else {

$new3=$dontlike+1;
$new4=$totalvotes+1;

$update="UPDATE `trackrating` SET `dontlike`='$new3' WHERE id='$getdontlike'";
mysql_query($update) or die(mysql_error());
$update2="UPDATE trackrating SET totalvotes='$new4' WHERE id='$getdontlike'";
mysql_query($update2) or die(mysql_error());

echo "<meta http-equiv='refresh' content='0;url=/'>";
}



}




// if track not found, Insert into database
else {

$insert="INSERT INTO trackrating(id, title) VALUES('0', '$track')";
mysql_query($insert);

echo "<meta http-equiv='refresh' content='0'>";

}


?>
</div>
</div>

 

It is much appriciated. Thank you.

Should you be checking if the votes in the table if($totalvotes == "") instead of 0? I don't know if you pre-fill the fields or not and if there are no votes then is it blank or is it 0 and if it is 0 then try if($totalvotes == 0) instead of if($totalvotes == "0")

Yes, the tables i pre-populated with a 0 for $totalvotes and $like and $dontlike. When a person clicks on Like or Dont Like, its supposed to incriment that value bye 1 and also the totalvotes by 1.

 

I have tried $totalvotes == 0 and $totalvotes == "0" but this doesn't seem to make a difference. The first vote always receives 2.

 

I've read over and over the code and I just cant find whats causing this, but there must be something.

 

Any help is appriciated. Thanks.

Yeah. I believe you.

 

I asked those question because I looked over it and couldn't see any reason either.

 

Well short of another pair of eyes spotting something.... assign a dummy variable to the default value in the table and step that variable through the process as well and echo it out in the end then eliminate it from areas to see if you can get some different results to that variable... has to be doubling up someplace I just can't see where. Damn wish I was more helpful... I'll look over it again though too.

 

Hey one more thing, sure you've tried it but just incase. Have you tried just doing a straight up addition? Since the table is preset there is not need for an if/else statement?

 

If that gets you the same result you could use a work around just use if statement to see if the value == 0 and if it is mark a flag. Do not check for if/else for the updating aspect just update as usual then after the updates, check to see if your flag was triggered and if so then update again with setting it to 1. If that still produces 2 then something is running the script twice here

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.