Hi, im new to php and im trying to learn by building a text based car game. The problem im having is i designed a class to list all the cars a player has or can purchase new or is in the scrapyard. The problem is i have really badly designed it and i found a huge flaw.
This is the class
It is accessed on a page called garage, which is all the cars a player owns, like this. This will also work for cars in the player scrapyard.PHP Code:class CarList
{
//class attributes
var $title;
var $description;
var $sql;
var $nores;
var $i = '0';
var $n = '4'; //starting row cars
var $s = '4'; //cars per row
public function __construct($title, $description, $sql, $nores = 'No Cars to Display!'){
$this->title = $title;
$this->sql = $sql;
$this->description = $description;
$this->nores = $nores;
$this->DisplayCars();
}
protected function DisplayCars()
{
echo '<h1>'.$this->title."</h1>\n".$this->description."\n";
echo "<table class=\"products\">\n";
echo "<tr>\n";
require('inc/db/dbconnect.inc.php');
@$query = $db_conn->query($this->sql)
or die('Failed to connect to database and retrieve your Cars! Please report this problem.');
while ($cars = $query->fetch_array())
{
$tbl = $this->DrawTable($cars, $this->DisplayImage($cars, $this->ImagesExists($cars)));
echo $tbl;
$this->i++;
}
if ($this->i == '0')
{
echo '<td><b>'.$this->nores."</b></td>\n";
}
echo '</tr></table>';
}
protected function ImagesExists($cars)
{
if (file_exists('images/cars/'.$cars['imgsrc']) && !empty($cars['imgsrc']))
{
return true;
}else{
return false;
}
}
protected function DisplayImage($cars, $exists = true, $sclass = 'gsel', $nclass = 'gnsel')
{
if ($exists == true)
{
if ( $cars['selected'] == '1')
{
$img = '<img src="images/cars/'.$cars['imgsrc'].'" '.ImageResize('images/cars/'.$cars['imgsrc'], 100).' alt="'.$cars['car'].'" class="'.$sclass.'" />';
}else{
$img = '<img src="images/cars/'.$cars['imgsrc'].'" '.ImageResize('images/cars/'.$cars['imgsrc'], 100).' alt="'.$cars['car'].'" class="'.$nclass.'" />';
}
}else{
if ( $cars['selected'] == '1')
{
$img = '<img src="images/cars/none.gif" alt="'.$cars['car'].'" class="'.$sclass.'" />';
}else{
$img = '<img src="images/cars/none.gif" alt="'.$cars['car'].'" class="'.$nclass.'" />';
}
}
return $img;
}
protected function DrawTable($cars, $img)
{
if ($this->i == $this->n)
{
$tbl = "\n</tr>\n<tr>\n<td><a href=\"index.php?p=cardet&item=".$cars['carid'].'&tid='.$_SESSION['tid'].'">'.$img.'</a><br />'.$cars['car'].' - $'.$cars['price'].'.</td>';
$this->n += $this->s;
}else{
$tbl = "\n<td><a href=\"index.php?p=cardet&item=".$cars['carid'].'&tid='.$_SESSION['tid'].'">'.$img.'</a><br />'.$cars['car'].' - $'.$cars['price'].'.</td>';
}
return $tbl;
}
}
PHP Code:$sql = "SELECT
cars.carid,
cars.car,
cars.description,
cars.`type`,
cars.imgsrc,
garage.price,
garage.bhp,
garage.speed,
garage.selected
FROM
garage
Inner Join cars ON garage.carid = cars.carid
WHERE
garage.userid = '{$_SESSION['uid']}'";
$garagelist = new CarList('Welcome to Your Grage', 'Below are your cars.', $sql, 'You need to Buy a Car!');
The huge problems is that, because i am using a ref table to hold the players cars and a car table to hold the cars, i cannot list all the cars a player does not have.









Bookmarks