Moneyman2010mn Posted March 10, 2007 Share Posted March 10, 2007 Hi, I'm new here but I'm not new to forums and online communities. A friend of mine asked for my help because I know some php. He used a rating/counting script from hscripts.com and modified it a bit to fit is website. Here's a link to a page and how he's using the script. Alright so what he wants to do is create a homepage with the top 20 results with the highest averages being shown on the home page. I'd have an idea on how to do it using a database but am clueless using text files. He does know C and Visual Basic so he knows how the code should flow: the script would need to - start with the total figure 0 for each page and the count 0 for each page - search through the file for each unique page name (e.g. "section1_simcityworld.php") - if the IP address has not yet appeared for that page, then add the relevant rating to the total figure each time that page name appears, and add one to the count for that page each time it appears - divide the total figure by the count, for each page, to work out the average - sort the page names in order of their average (with the highest average first) - output the highest ranked 20 pages to the main page, sorted in order of rank. also include the full article name rather than just the page name. the full article names may need to be stored in a file somewhere with their associated page name? I don't need nor want exact code but how would I extract the data out of this text file and then display the top 20 results? Like what functions would I want to use? Quote Link to comment Share on other sites More sharing options...
Greaser9780 Posted March 10, 2007 Share Posted March 10, 2007 I know to use fopen() to open the file and fclose() to close it when finished but not sure how to extract the data. Here is a link to different filesystem commands to get you in the right direction: http://www.phpfreaks.com/phpmanual/page/ref.filesystem.html Quote Link to comment Share on other sites More sharing options...
per1os Posted March 10, 2007 Share Posted March 10, 2007 To read contents of a file use file() for it to be in an array or file_get_contents() for a string. --FrosT Quote Link to comment Share on other sites More sharing options...
Moneyman2010mn Posted March 12, 2007 Author Share Posted March 12, 2007 Alright so here is what I have so far which came with the script. After that there's some html displaying the results. Tell me if you'd want to see it. $lines = file("./HRAT/rateval.txt"); $count = 0; $ratc = 0; $fname = $_SERVER['SCRIPT_NAME']; //echo($fname); foreach ($lines as $line_num => $line) { //echo $line."<br>"; $firstPos = strpos($line,'****'); $id = substr($line,0,$firstPos); //echo($id."...<br>"); //echo($fname."<br>"); //echo("-----------"); if($id == $fname) { $firstPos = strpos($line,'%%%%'); $rat = substr($line,$firstPos+4,$line.length-1); $count = $count+1; $ratc = $ratc+$rat; //echo($rat."********"); } } if($count==0) { $avg = 0; } else { $avg = round($ratc/$count*100)/100; } $perc = round( (100/5)*$avg); $rem = 100-$perc; Now my question for you guys is how would I break up the results so for each page ($fname) a separate box displaying the results will be displayed for only the top 20? Quote Link to comment Share on other sites More sharing options...
Moneyman2010mn Posted March 13, 2007 Author Share Posted March 13, 2007 *Bump* Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted March 13, 2007 Share Posted March 13, 2007 Wait, why aren't you using mysql? Using text files is a very unreliable (and slow) method of doing stuff like this. I used to have a click counter that used a text file and my page took forever to load, so I wrote a new click counter myself that used my database and it sped up a ton. Quote Link to comment Share on other sites More sharing options...
Moneyman2010mn Posted March 13, 2007 Author Share Posted March 13, 2007 My friend was using a free script which used a text file instead. If no one knows how to do what I want to do then I'll try to convince him to switch to mysql. Quote Link to comment Share on other sites More sharing options...
Moneyman2010mn Posted March 14, 2007 Author Share Posted March 14, 2007 *bump* Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted March 15, 2007 Share Posted March 15, 2007 I'm sure someone knows how, but no one really wants to condone using such an inefficient method, mysql is so much easier, faster, and safer, you are much less likely to delete a database than you are a text file stored on your server. Trust me, mysql is the way to go. I re wrote my entire site to run off my database, now my news, roster, affiliates, projects, tutorials, and click counter, as well as the forum, are all in a database, it keeps things very organized. Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted March 15, 2007 Share Posted March 15, 2007 Wait, why aren't you using mysql? Using text files is a very unreliable (and slow) method of doing stuff like this. I used to have a click counter that used a text file and my page took forever to load, so I wrote a new click counter myself that used my database and it sped up a ton. there is only one better use i see for a flat file compared to a databse... you can only store up to 255 characters in a table cell in a database. here's how you read from a flat file: <?php $fp = fopen("filename.txt", "r"); while(!feof($fp)){ $line = fread($fp, 1024); echo $line ."\n"; } ?> Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted March 15, 2007 Share Posted March 15, 2007 That is absolutely 100% not true, you can stor an infinite amount of data in a cell in a database, look at this page, all the tutorial text is stored in a database, its about 2000 characters, all in one cell. I don't know where you got your information but it is completely false. Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted March 15, 2007 Share Posted March 15, 2007 That is absolutely 100% not true, you can stor an infinite amount of data in a cell in a database, look at this page, all the tutorial text is stored in a database, its about 2000 characters, all in one cell. I don't know where you got your information but it is completely false. you're right, halo. i'm sorry for posting false info i've just been working on this damn project for a client for like 2 months now, and i'm working with an existing database that has a limit to 255 characters as a security feature (e-commerce site). i don't know why i thought that was the limit across every mysql database. i must have been stoned . how did that code work moneyman? Quote Link to comment Share on other sites More sharing options...
chrisuk Posted March 15, 2007 Share Posted March 15, 2007 it's not infinite but it's a massive amount!! A LONGTEXT allows you to store 4294967295 characters!! A hell of a lot more than 255 Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted March 17, 2007 Share Posted March 17, 2007 I just use text, its plenty for me. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.