Jump to content

Extracting Results from Text File


Moneyman2010mn

Recommended Posts

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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";
        }
?>

Link to comment
Share on other sites

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 8).

 

 

how did that code work moneyman?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.