PHP OOP - Get certain information from a site.

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
User avatar
HighScore80
Forum Newbie
Posts: 3
Joined: Wed Oct 17, 2007 3:46 am

PHP OOP - Get certain information from a site.

Post 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. :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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?
(#10850)
User avatar
HighScore80
Forum Newbie
Posts: 3
Joined: Wed Oct 17, 2007 3:46 am

Post 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.
Post Reply