Hi Everyone,
I have just started to teach myself oop in php and may have no chosen a good example to start with.
what i want to do is be able to generate navigation tabs for my website by doing something similar to the following
$tabs->newtab("Tab Name","Tab index");
$tabs->newtab("Tab Name","Tab index");
$tabs->newtab("Tab Name","Tab index");
$tabs->draw();
by invoking the tab draw this should draw the tab table,
with every newtab i would be thinking that this would put the data into a form of array within the class
I have tried every combination i can think of but just cant seem to work it out, get thrown heaps of errors, ive tried googling and searching forums but not 100% sure what im trying to look for, if someone can point me in the right direction that would be great, in general just want to know how to add an item to an array in a class
thanks all
dynamic array issue in class
Moderator: General Moderators
-
Mark Baker
- Forum Regular
- Posts: 710
- Joined: Thu Oct 30, 2008 6:24 pm
Re: dynamic array issue in class
So what classes do you have? and what attributes and methods are defined in those classes?
Re: dynamic array issue in class
i have a class and i have called it tabs.
ive tried declaring an array in there
in that i have a function called add tab with some parametres, this is the function that i want to add an item to the array.
i would post my code but i have desected it so much that its of no use.
as i said im new to this so dont even know which terminology to use.
even an example of using an array in a class would be helpfull, sorry for the lack of details
mabey somthing like this
class tabs {
public $tabs = array()
function addtab($tabname,tabindex){
$i = Next index in array
$tabs[$i][tabname] = $tabname;
$tabs[$i][tabindex] = $tabindex;
}
function showtabs(){
loop through all array items and echo output
}
}
ive tried declaring an array in there
in that i have a function called add tab with some parametres, this is the function that i want to add an item to the array.
i would post my code but i have desected it so much that its of no use.
as i said im new to this so dont even know which terminology to use.
even an example of using an array in a class would be helpfull, sorry for the lack of details
mabey somthing like this
class tabs {
public $tabs = array()
function addtab($tabname,tabindex){
$i = Next index in array
$tabs[$i][tabname] = $tabname;
$tabs[$i][tabindex] = $tabindex;
}
function showtabs(){
loop through all array items and echo output
}
}
-
Mark Baker
- Forum Regular
- Posts: 710
- Joined: Thu Oct 30, 2008 6:24 pm
Re: dynamic array issue in class
Code: Select all
class tabs {
private $tabs = array()
public function addtab($tabname,$tabindex) {
$i = count($this->tabs);
$this->tabs[$i][tabname] = $tabname;
$this->tabs[$i][tabindex] = $tabindex;
} // function addtab
public function showtabs() {
foreach($this->tabs as $tab) {
echo $tab[tabname].'<br />';
}
} // function showtabs
} // class tabs
$tabs = new tabs();
$tabs->addtab("Tab Name 1","Tab index 1");
$tabs->addtab("Tab Name 2","Tab index 2");
$tabs->addtab("Tab Name 3","Tab index 3");
$tabs->showtabs();
Re: dynamic array issue in class
Thank you to much Mark Baker,
This was definately a learning curve for me, the problem was that i was addressing the variable with the $, for example.
$this->$tabsarr[$i][tabname] .. i have now learned that the $ sign is not needed.
I cant believe it was that simple, but i thankyou for your help, i hope this can help someone else
Cheers
This was definately a learning curve for me, the problem was that i was addressing the variable with the $, for example.
$this->$tabsarr[$i][tabname] .. i have now learned that the $ sign is not needed.
I cant believe it was that simple, but i thankyou for your help, i hope this can help someone else
Cheers