Page 1 of 1

PHP OOP - Get certain information from a site.

Posted: Wed Oct 17, 2007 4:06 am
by HighScore80
This is one of my first Object oriented tests and I've figured out how to get the information I want and use it.

I want some kind of review on this simple algoritm. It's pretty messy and I would like to know what I can do better. I had a hard time getting good variablenames for example.

This class reads from pages in Goldeneye ranking on the-elite.net. http://www.the-elite.net/GE/stage12.htm for example it looks up the name and returns the points the time is worth from the three diffrent difficults.

Code: Select all

<?php
class Goldeneye
{	
	var $homepage;
	var $a;
	var $sa;
	var $ooa;
	var $tr;
	var $endtr;
	var $nextrow;
	var $data;
	var $check;

	/**********************
	Function name: Goldeneye()
	Description: Constructor
	Purpose: Set values to the variables.
	Arguments: Nothing
	Returns: Nothing
	************************/
	function Goldeneye()
	{
		$this->a = 0;
		$this->sa = 0;
		$this->ooa = 0;
		$this->tr = 0;
		$this->endtr = 0;
		$this->nextrow = 0;
		$this->check = 0;
		$this->homepage = "http://www.the-elite.net/GE/";
	}

	/**********************
	Function name: GetColor()
	Description: Get colors from ranking.
	Purpose: Get colors from external homepage.
	Arguments: $stage, $player
	Returns: $apoints, $sapoints, $ooapoints
	************************/
	function GetColor($stage, $player)
	{
		if($stage == "all")
		{
			GetAllColor();
		}
		
		$fp = fopen($this->homepage . $stage . ".htm", 'r');
		
		while(!feof($fp))
		{
			$this->data = fgets($fp, 1024);
	
			if(stristr($this->data, "</tr>"))
			{
				$this->endtr = 1;
				$this->tr = 0;
			}
		
			if($this->nextrow == 1)
			{
				if($this->check == 1)
				{
					if($this->endtr == 1)
					{
						$tempooa = $temp;
						$this->check = 0;
						$this->nextrow = 0;
						$this->endtr = 0;
					}
					elseif($this->endtr == 0)
					{
						$tempsa = $temp;
						$this->check = 0;
						$this->nextrow = 0;
					}
				}
				elseif($this->tr == 1)
				{
					$tempa = $this->data;
					$this->tr = 0;
					$this->nextrow = 0;
				}
				else
				{
					$temp = $this->data;
					$this->check = 1;
				}
			}
	
			if(stristr($this->data, "<tr>"))
			{
				$this->tr = 1;
				$this->endtr = 0;
			}
			elseif(stristr($this->data, " <font color=\"#FFFF80\">"))
			{
				$this->tr = 0;
			}
			elseif(stristr($this->data, "$player</td>"))
			{
				$this->nextrow = 1;
			}
		}
		fclose($fp);
		
		$this->a = substr($tempa,-8,2);
		$this->sa = substr($tempsa,-8,2);
		$this->ooa = substr($tempooa,-8,2);
		
		if($this->a == "00")
		{
			$this->a = 100;
		}
		elseif($this->sa == "00")
		{
			$this->sa = 100;
		}
		elseif($this->ooa == "00")
		{
			$this->ooa = 100;
		}
		
		return array ($this->a, $this->sa, $this->ooa);
	}
}
?>
And the usage of the class:

Code: Select all

<?php
include("inc/class_goldeneye.php");

$Goldeneye = new Goldeneye();

$result = $Goldeneye->GetColor("stage1", "Patrik Nilsson");
$result = $Goldeneye->GetColor("stage3", "Axel Andersson");
?>
I would prefer code examples on improvements, thanks. :)

Posted: Wed Oct 17, 2007 12:20 pm
by Christopher
I think the constructor should parse the remote page into a data array. Then the information methods would access that data. Though I am not clear why you want the color? What does this do?

Posted: Thu Oct 18, 2007 1:35 am
by HighScore80

Code: Select all

$result = $Goldeneye->GetColor("stage1", "Patrik Nilsson");
Gives me this output:

81
82
76

from http://www.the-elite.net/GE/stage1.htm

It's named color because the function of this class is to get the point to calculate what color me own timepage should have. Let say 1:02 on a level is worth over 75pts. Then it should be green for example, and if its over 60 it should be blue etc.

An example of my page:
http://www.geocities.com/highscore80/re ... deneye.htm

I just thought of another sulotion to get the information that i want. Let' say I save stage1's agent difficult in a file, stage1's secret agent difficult in another file etc. Cause if the rankings is not yet updated it will show the color based on the points I already have. I would not be able to update my new time with the new points directly.

My server is down atm due to new appartment and internet not having arrived quite yet.