patternphpMinor
Display PHP Menu Stored in an Array and Looped
Viewed 0 times
storedarrayphpmenuandloopeddisplay
Problem
I'm creating a PHP website for a non-profit. They have some restrictions (no MySQL or pre-installed CMS) so I'm creating a CSS menu displayed by an unordered list where all of the elements are stored in a multi-dimensional array.
I've gotten it to work but being new to PHP, I'm certain it's not optimized to run as smoothly as possible. Also, I feel I've hacked my way through the first and last elements.
Here's the array:
And here's the logic:
I had a hard time finding a relevant post. I'd appreciate some help or even just validation that I'm doing this correctly.
I've gotten it to work but being new to PHP, I'm certain it's not optimized to run as smoothly as possible. Also, I feel I've hacked my way through the first and last elements.
Here's the array:
And here's the logic:
".$menu[$i][0]."\n";
}
else {
echo "".$menu[$i][0]."\n";
}
for ($j = 0; $j ".$menu[$j][0]."\n";
}
else {
echo "".$menu[$j][0]."\n";
}
}
}
echo '';
}
$i++;
}
?>
I had a hard time finding a relevant post. I'd appreciate some help or even just validation that I'm doing this correctly.
Solution
I'm not sure for optimization but for readability and maintenance I would use a Class and create the menu items as objects instead of multi-dimension arrays.
And so your creation code would become:
Then you can access values in a more readable way. E.g.
class MenuItem {
protected $id;
protected $name;
protected $url;
protected $level;
protected $seq;
protected $under;
protected $target;
protected $display;
public function __construct($name, $url, $id, $level, $seq, $under, $target, $display) {
$this->id = $id;
$this->name = $name;
$this->url = $url;
$this->level = $level;
$this->seq = $seq;
$this->under = $under;
$this->target = $target;
$this->display = $display;
}
public function getId() {
return $this->id;
}
public function getName() {
return $this->name;
}
public function getUrl() {
return $this->url;
}
public function getLevel() {
return $this->level;
}
public function getSeq() {
return $this->seq;
}
public function getUnder() {
return $this->under;
}
public function getTarget() {
return $this->target;
}
public function getDisplay() {
return $this->display;
}
}And so your creation code would become:
$siteURL = "http://www.mysite.org/";
$menu = array
(
//"Name","URL",ID,Level,Seq,Under,Target,Display
//First Menu
new MenuItem("TOP1",$siteURL."top1.php",1,1,1,0,0,true),
new MenuItem("SUB1a",$siteURL."sub1a.php",2,2,1,1,0,true),
new MenuItem("SUB1b",$siteURL."sub1b.php",3,2,2,1,0,true),
new MenuItem("SUB1c",$siteURL."sub1c.php",4,2,3,1,0,true),
new MenuItem("SUB1d","http://www.externalsite.org/",5,2,4,1,1,true),
new MenuItem("SUB1e","http://www.externalsite.org/",6,2,5,1,1,true),
new MenuItem("SUB1f",$siteURL."sub1f.php",7,2,6,1,0,true),
//Second Menu
new MenuItem("TOP2",$siteURL."top2.php",8,1,2,0,0,true),
new MenuItem("SUB2a","http://www.externalsite.org/",9,2,1,8,1,true),
new MenuItem("SUB2b",$siteURL."sub2b.php",10,2,2,8,0,true),
new MenuItem("SUB2c","http://www.externalsite.org/",11,2,3,8,1,true),
new MenuItem("SUB2d","http://www.externalsite.org/",12,2,4,8,1,true),
//Third Menu
new MenuItem("TOP3",$siteURL."top3.php",13,1,3,0,0,true),
new MenuItem("SUB3a","http://www.externalsite.org/",14,2,1,13,1,true),
new MenuItem("SUB3b","http://www.externalsite.org/",15,2,2,13,1,true),
new MenuItem("SUB3c","http://www.externalsite.org/",16,2,3,13,1,true),
new MenuItem("SUB3d","http://www.externalsite.org/",17,2,4,13,1,true),
//Fourth Menu
new MenuItem("TOP4",$siteURL."top4.php",18,1,4,0,0,true),
new MenuItem("SUB4a",$siteURL."downloads/sub4a.pdf",19,2,1,18,0,true),
new MenuItem("SUB4b",$siteURL."sub4b.php",20,2,2,18,0,true),
//Fifth Menu
new MenuItem("TOP5",$siteURL."top5.php",21,1,5,0,0,true),
//Sixth Menu
new MenuItem("TOP6",$siteURL."top6.php",22,1,6,0,0,false),
//Seventh Menu
new MenuItem("TOP7",$siteURL."top7.php",23,1,7,0,0,false),
//Eighth Menu
new MenuItem("TOP8","http://www.externalsite.org/",24,1,8,0,1,true),
);Then you can access values in a more readable way. E.g.
foreach ($menu as $menuItem) {
if (($menuItem->getLevel() === 1) && ($menuItem->getDisplay() === true)) {
...
}
}Code Snippets
class MenuItem {
protected $id;
protected $name;
protected $url;
protected $level;
protected $seq;
protected $under;
protected $target;
protected $display;
public function __construct($name, $url, $id, $level, $seq, $under, $target, $display) {
$this->id = $id;
$this->name = $name;
$this->url = $url;
$this->level = $level;
$this->seq = $seq;
$this->under = $under;
$this->target = $target;
$this->display = $display;
}
public function getId() {
return $this->id;
}
public function getName() {
return $this->name;
}
public function getUrl() {
return $this->url;
}
public function getLevel() {
return $this->level;
}
public function getSeq() {
return $this->seq;
}
public function getUnder() {
return $this->under;
}
public function getTarget() {
return $this->target;
}
public function getDisplay() {
return $this->display;
}
}$siteURL = "http://www.mysite.org/";
$menu = array
(
//"Name","URL",ID,Level,Seq,Under,Target,Display
//First Menu
new MenuItem("TOP1",$siteURL."top1.php",1,1,1,0,0,true),
new MenuItem("SUB1a",$siteURL."sub1a.php",2,2,1,1,0,true),
new MenuItem("SUB1b",$siteURL."sub1b.php",3,2,2,1,0,true),
new MenuItem("SUB1c",$siteURL."sub1c.php",4,2,3,1,0,true),
new MenuItem("SUB1d","http://www.externalsite.org/",5,2,4,1,1,true),
new MenuItem("SUB1e","http://www.externalsite.org/",6,2,5,1,1,true),
new MenuItem("SUB1f",$siteURL."sub1f.php",7,2,6,1,0,true),
//Second Menu
new MenuItem("TOP2",$siteURL."top2.php",8,1,2,0,0,true),
new MenuItem("SUB2a","http://www.externalsite.org/",9,2,1,8,1,true),
new MenuItem("SUB2b",$siteURL."sub2b.php",10,2,2,8,0,true),
new MenuItem("SUB2c","http://www.externalsite.org/",11,2,3,8,1,true),
new MenuItem("SUB2d","http://www.externalsite.org/",12,2,4,8,1,true),
//Third Menu
new MenuItem("TOP3",$siteURL."top3.php",13,1,3,0,0,true),
new MenuItem("SUB3a","http://www.externalsite.org/",14,2,1,13,1,true),
new MenuItem("SUB3b","http://www.externalsite.org/",15,2,2,13,1,true),
new MenuItem("SUB3c","http://www.externalsite.org/",16,2,3,13,1,true),
new MenuItem("SUB3d","http://www.externalsite.org/",17,2,4,13,1,true),
//Fourth Menu
new MenuItem("TOP4",$siteURL."top4.php",18,1,4,0,0,true),
new MenuItem("SUB4a",$siteURL."downloads/sub4a.pdf",19,2,1,18,0,true),
new MenuItem("SUB4b",$siteURL."sub4b.php",20,2,2,18,0,true),
//Fifth Menu
new MenuItem("TOP5",$siteURL."top5.php",21,1,5,0,0,true),
//Sixth Menu
new MenuItem("TOP6",$siteURL."top6.php",22,1,6,0,0,false),
//Seventh Menu
new MenuItem("TOP7",$siteURL."top7.php",23,1,7,0,0,false),
//Eighth Menu
new MenuItem("TOP8","http://www.externalsite.org/",24,1,8,0,1,true),
);foreach ($menu as $menuItem) {
if (($menuItem->getLevel() === 1) && ($menuItem->getDisplay() === true)) {
...
}
}Context
StackExchange Code Review Q#42556, answer score: 2
Revisions (0)
No revisions yet.