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:
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:
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:
"get_title" and "get_url" etc are declared in the class by the way.
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;
}
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.