OOP Functions to get info from database into a drop down
Posted: Thu Feb 12, 2009 2:30 am
I'm new to OOP programming and now I'm stuck in what to do. I can solve my problem sure, but I'm trying to make the code as easy to read as possible.
I want to fill a select form with different "Brands" from my database. At first I had everything in one method, and things worked but I felt there was too many unnecessary variables all over. Why not make a GetBrand() and a GetAllBrand() for future purposes. Now I feel like I have too many methods instead and I don't know if it follows any standards at all. Here's my database:
returns:
Here is my class and my code for the select form:
Here's the drop down form:
Now the code isn't even working cause I don't really know what every array contains, and returns. Could someone help me to simplify this code, and make it work according to OOP standards? Bare with me on the programming terms, cause I'm not that advanced when it comes to it. I want someone to show me edited code of how I can do it rather then tell me what to do. That would make me understand better. 
I want to fill a select form with different "Brands" from my database. At first I had everything in one method, and things worked but I felt there was too many unnecessary variables all over. Why not make a GetBrand() and a GetAllBrand() for future purposes. Now I feel like I have too many methods instead and I don't know if it follows any standards at all. Here's my database:
Code: Select all
CREATE TABLE brands (brandID int(11) NOT NULL AUTO_INCREMENT, brand varchar(30) NOT NULL DEFAULT '', PRIMARY KEY (brandID)) TYPE=MyISAM;Code: Select all
SELECT * FROM brands;Code: Select all
+---------+---------+
| brandID | brand |
+---------+---------+
| 12 | Dell |
| 13 | Lexmark |
| 14 | Xerox |
| 15 | Fujitsu |
+---------+---------+
Code: Select all
function GetBrandCount()
{
$sql = "SELECT COUNT(*) FROM brands";
$result = mysql_query($sql);
$this->$count = @mysql_fetch_array($result);
return $this->$count[0];
}
function GetBrand($value)
{
$sql = "SELECT DISTINCT brand FROM brands WHERE brandID='$value' ORDER BY brand";
$result = mysql_query($sql);
$this->brand = @mysql_fetch_array($result);
return $this->$brand[0];
}
function GetAllBrandID()
{
$sql = "SELECT brandID FROM brands ORDER BY brand";
$result = mysql_query($sql);
$this->brandid = @mysql_fetch_array($result);
return array($this->brandid[0]);
}
function GetAllBrands()
{
´ $this->$count = GetBrandCount();
$this->$brandid = GetAllBrandID();
for($i=1; $i<=$count; $i++)
{
$brand[$i] = GetBrand($brandid[$i]);
}
return array($this->$brandid, $this->$brand)
}Code: Select all
$Brand = new Brand();
$br = $Brand->GetAllBrands();
$brcount = $Brand->GetBrandCount();
<select name="select" tabindex="3">
<? for ($i=1; $i<=$brcount; $i++){?>
<option value="<?php echo $br[0][$i]; ?>"><?php echo $br[1][$i]; ?></option>
<?php } ?>
</select>