[Solved]Trying to make a Rating code, problem with files...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ChipChamp
Forum Newbie
Posts: 13
Joined: Sun May 20, 2007 8:40 pm

[Solved]Trying to make a Rating code, problem with files...

Post by ChipChamp »

I am fairly new to PHP (not programming), and I am trying to make a rating code. I have put together this code:

Code:

Code: Select all

<?php

if (!file_exists("rate.txt"))
{
 $file=fopen("rate.txt", "w");
 for ($i=0;$i<1000;$i+=1) {
  $newline+="0^^0||";
 }
 fputs($file,$newline);
 fclose ($file);
}
else {
 $filename = "rate.txt";
}
$filename = "rate.txt";
$content = file("rate.txt");

//put content in array
$array = explode("||", $content[0]);
for ($i=0;$i<1000;$i+=1) {
 $miniarray[$i] = explode("^^", $array[$i]);
}

$miniarray[$showtopic][0] += 1;
$miniarray[$showtopic][1] += $rating;

$array[$showtopic] = $miniarray[$showtopic][0]."^^".$miniarray[$showtopic][1];

$fp = fopen($filename,"w");
insert votes to txt file
for ($i=0;$i<1000;$i+=1) {
 $insertvote += $array[$showtopic]."||";
}
fwrite($fp,$insertvote);
//fclose($fp);

echo "Total votes: ".$miniarray[$showtopic][0];
echo " Rating: ".$miniarray[$showtopic][1]/$miniarray[$showtopic][0];
?>

What it's supposed to do is check if there is a file, if not, make a new one with 1000 sets of "0^^0||" inside the file. Then I use an explode function to make an array with each value of the array displaying something that should look like this: "0^^0". Then I do another explode, 1000 times something such as "0". Now depending on what the person put in the url for $showtopic and $rating, it adds to miniarray[$showtopic][0] and miniarray[showtopic][1]. After this it is supposed to write the arrays back into the file, with the "updates". Then show the rating and number of votes on the page.

It doesn't work... It doesn't save the values into the file.

If I didn't lose you there... Is there anyone who can help me with this? Any help would be appreciated. :)
Last edited by ChipChamp on Tue Jun 26, 2007 2:29 pm, edited 1 time in total.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Is the file being created? Does it have proper permissions? Where exactly does the script fail?
ChipChamp
Forum Newbie
Posts: 13
Joined: Sun May 20, 2007 8:40 pm

Post by ChipChamp »

The file is created, and always ends up only saying 1000. It has these permission (at least this is what my hosting service says): -rw-r--r--
I'm not sure where it fails... The code doesn't come up with any errors or anything, it just doesn't make a file say what it is supposed to say... And because of that that it doesn't load any values from the file.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Have you printed the final result (before trying to write it to the file) to the screen?
ChipChamp
Forum Newbie
Posts: 13
Joined: Sun May 20, 2007 8:40 pm

Post by ChipChamp »

Well, I print it to the screen at the very end of the file, which is after I write the files and stuff like that. If I were to have typed in this: url.com/rate.php?rating=5&showtopic=10

It would show this:

Total votes: 1 Rating: 5

Now, if it were working and I reloaded the page, it should say:

Total votes: 2 Rating: 5

Because I loaded the page two times, and since each time I added 5 to the rating, then I divide the rating by the number of votes, the rating is 5. But, it just shows 1 vote and rating 5 when I reload the page.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Trying to make a Rating code, problem with files...

Post by superdezign »

Thanks for that then. I think I see the problem.
ChipChamp wrote:

Code: Select all

for ($i=0;$i<1000;$i+=1) {
 $insertvote += $array[$showtopic]."||";
}
$insertvote is a number because you use the plus operator. In PHP, you can't concatenate strings using the plus operator. There is a special concatenation operator, the dot/period.

Code: Select all

$insertvote .= $array[$showtopic]."||";
See if that fixes it.
ChipChamp
Forum Newbie
Posts: 13
Joined: Sun May 20, 2007 8:40 pm

Post by ChipChamp »

Thanks! That made the votes go up and made the ratings part work! Now, I only have one more problem, which I might be able to figure out on my own. :) This problem is that depending on the value $showtopic which was put in the url, it has a different value of votes and ratings. For example:

url.com/rate.php?rating=5&showtopic=1

Total votes: 1 Rating: 5

url.com/rate.php?rating=2&showtopic=1

Total votes: 2 Rating: 3.5

url.com/rate.php?rating=8&showtopic=2

Total votes: 1 Rating: 8

etc.

If you have any idea on how to fix that, then please say. :) Otherwise, thanks for the first question! :)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Well, what does showtopic mean and where are you getting the data from?
ChipChamp
Forum Newbie
Posts: 13
Joined: Sun May 20, 2007 8:40 pm

Post by ChipChamp »

Nevermind, I figured it out. On this line of code

$insertvote .= $array[$i]."||";

it looked like this:

$insertvote .= $array[$showtopic]."||";

which is in a for statement, but was only writing the showtopic array number to the file 1000 times, and not all 1000 array options.

Thanks for all your help! :)
User avatar
idevlin
Forum Commoner
Posts: 78
Joined: Tue Jun 26, 2007 1:10 pm
Location: Cambridge, UK

Post by idevlin »

I was going to say, $showtopic is being used to index into an array, so changing its value is going to potentially reference a different value within that array.

But yes, as superdezign says what does it mean?

**EDIT**
Ah he fixed it :-)
ChipChamp
Forum Newbie
Posts: 13
Joined: Sun May 20, 2007 8:40 pm

Post by ChipChamp »

Well, someone was asking for a code for an invision free forum. Invisionfree uses showtopic to show what topic you are viewing, and he wanted to be able to have people rate topics on his site.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Wait, show topic is directly from the URL? No $_GET involved? register_globals is not recommended, and FYI, you don't clean any of your values.
ChipChamp
Forum Newbie
Posts: 13
Joined: Sun May 20, 2007 8:40 pm

Post by ChipChamp »

For testing showtopic is directly from the url, but later I will probably change it to $_GET. And I don't really know what you mean by the rest of that... (I am pretty (very) new to this. :) Lol)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

The fact that $showtopic does not have to be accessed as $_GET['showtopic'] means that you have register_globals on.

And not cleaning your data means that you could put in anything in showtopic in the URL, and you automatically accept it as valid. What if you were to type a string into the url?
ChipChamp
Forum Newbie
Posts: 13
Joined: Sun May 20, 2007 8:40 pm

Post by ChipChamp »

Hmm... Okay. I get what you mean. :) Thanks for the information.
Post Reply