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>