OOP Functions to get info from database into a drop down

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

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

OOP Functions to get info from database into a drop down

Post by HighScore80 »

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:

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;
returns:

Code: Select all

 
+---------+---------+
| brandID | brand   |
+---------+---------+
|      12 | Dell    |
|      13 | Lexmark |
|      14 | Xerox   |
|      15 | Fujitsu |
+---------+---------+
 
Here is my class and my code for the select form:

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)
    }
Here's the drop down form:

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>
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. :)
sparrrow
Forum Commoner
Posts: 81
Joined: Mon Oct 20, 2008 12:22 pm

Re: OOP Functions to get info from database into a drop down

Post by sparrrow »

You should print_r($br); before the drop down code to print out the entire array structure and contents to see exactly what it contains. That should help you debug what's going on. I don't code to OOP standards because I like job security. :mrgreen:
Post Reply