PHP - Classes & Arrays

Associate
Joined
28 Nov 2004
Posts
1,237
Location
Birmingham
Hi all,
I'm very new to PHP so I'm teaching myself by rebuilding a site i have in it and also giving myself little problems to solve. I've been doing work with array's and classes but i've hit a problem i'm sure is very simple but i need a little nudge in the right direction.

What I have is a menu which has a number of items in it.
The following code works fine. It's a manual loading of an array of menu items where "item" is in my menu class:
PHP:
$allItems=array();
    
    $temp=new item();
    $temp->set_title("test 1");
    $temp->set_url("http://www.google.co.uk");
    $allItems[]=$temp;
    
    $temp=new article();
    $temp->set_title("test 2");
    $temp->set_url("http://www.google.co.uk/maps");
    $allItems[]=$temp;
    
    $temp=new article();
    $temp->set_title("test 3");
    $temp->set_url("http://www.google.co.uk/base");
    $allItems[]=$temp;
    
    foreach($allItems $i => $value) {
        echo "<p>".$i." - ".$allItems[$i]->get_title()."</p>";
        echo "<p>".$i." - ".$allItems[$i]->get_url()."</p>";
    }

Ok, brill. Next step is to create a class function to pull an entire menu back by id. This is where I'm going wrong but I'm not sure where:

In my class, I declare "var $items=array();" at the top then this is my function:

PHP:
public function get_menu($by_id) {
        $menu=array();
        $cid = mysql_connect($GLOBALS['host'],$GLOBALS['usr'],$GLOBALS['pwd']);
        if($cid) {
            $SQL='[SELECT STATEMENT HERE]';
            $retid = mysql_db_query($GLOBALS['db'], $SQL, $cid);
            if ($retid) { 
                $row = mysql_fetch_array($retid);
                while ($row = mysql_fetch_array($retid)) { 
                    $temp=new item();
                    $temp->title=$row["tx_name"];
                    $temp->url=$row["tx_url"];
                    
                    $this->items[]=$temp;
                }                
            } else {
                echo(mysql_error()); 
            }
        } else {
                echo(mysql_error()); 
        }
        
        return $this->items;
    }
so, i load up my $items array a menu item at a time and return it after.
then, in my code i can loop through the array as before. only problem is, i don't get an array back - just the last menu item. if i try to treat it as an array i get an error.

Here's the call to the above function:
PHP:
$testing=new menu();
    $testing->get_menu(1);
    
    foreach($testing as $i => $value) {
        echo "<p>".$i." - ".$testing[$i]->get_title()."</p>";
        echo "<p>".$i." - ".$testing[$i]->get_url()."</p>";
    }

"get_title" and "get_url" etc are declared in the class by the way.
 
This line:
$this->items[]=$temp;

replaces the contents of $this->items with $temp. This is why you only ever get the last item in your array, because it wipes out the old ones.

Try using this instead:
array_push($this->items, $temp);

array_push will add to the existing array without replacing the items in it.
 
superb. cheers!
I knew there were loads of cool pre-existing methods in php but as a newbie, it's just a matter of knowing the exact ones.
 
A bit of a follow up to this.
array_push worked but it's also worth noting that using:


PHP:
$this->items[]=$temp;


does in fact append to the array and not replace the array.

I've discovered my problem was in the page referencing the class.

I did have:

PHP:
$testing=new menu();
$testing->get_menu(1);

...but that isn't correct. I should be loading my instance of menu items into an array.

PHP:
$testing=array();
 $myMenu=new menu();
 $testing=$myMenu->get_menu(1);


So, I've got one more question here. What would be considered best practice in this? Presumably calling array_push would be more overhead than using the code $this->items[]=$temp;? I'm guessing in the real world, there's probably little difference so it's down to personal preference.
 
Last edited:
Back
Top Bottom