How to optimize menu navigation - php

I'm building a menu options, having issue in last option, The Anchor method doesn't work as a link popup a new window. Besides, in option 1 and 2, I repeat those codes which is not look great.
Is there a better way to optimize those codes? make it cleaner.
In my controller:
public function loadPage($name, $pageID) {
$data['title'] = $this->tabPageData;
$data['tabMenu'] = $this->model->getAllMenuItems();
if ($name == 'portfolio-1') {
// load portfolio 1, get the page content (photos) and its name
$data['tabPageContent'] = $this->model->getPageContentByPageID($pageID);
$data['pageName'] = $this->model->getPageNameByID($pageID);
} elseif ($name == 'portfolio-2') {
$data['tabPageContent'] = $this->model->getPageContentByPageID($pageID);
$data['pageName'] = $this->model->getPageNameByID($pageID);
} elseif ($name == 'contact') {
// load Contact page
$data['tabContact'] = $this->model->getContactByPageID($pageID);
} else {
// load a Blog site
echo anchor('http://mysite.tumblr.com', 'target=_blank');
}
$this->load->view('content', $data);
}
In my View:
<div id="menu">
<ul>
<?php foreach ($tabMenu as $item) : ?>
<?php
$url = "<li><a href='" . base_url();
$url .= str_replace("+", "-", urlencode(strtolower($item->name))) . "/". ($item->cat_id) . "'>";
$url .= strtoupper($item->name) . "</a></li>";
echo $url;
?>
<?php endforeach; ?>
</ul>
</div> <!-- end of Menu -->

I would suggest that you clean up your view by creating a helper method that generates a list item for your navigation.
Put the following code in a file named navigation_helper.php in application/helpers/.
if (!defined('BASEPATH')) exit('No direct script access allowed');
if (!function_exists('build_list_item'))
{
function build_list_item ($item) {
$url_item_name = str_replace('+', '-', urlencode(strtolower($item->name)));
$url = base_url() . $url_item_name . "/". $item->cat_id;
return '<li>' . strtoupper($item->name) . '</li>';
}
}
Make sure you are loading the helper in your controller or autoloading it if you use it often.
$this->load->helper('navigation_helper');
Then in your view you could do this:
<div id="menu">
<ul>
<?php foreach ($tabMenu as $item): ?>
<?php echo build_list_item($item); ?>
<?php endforeach; ?>
</ul>
</div>

Related

How to set "class = active" for second tab?

I have some tabs. In that sometimes first tab is "introduction" and sometimes "outline" is first tab. I have set introduction class=active when it comes first. But when outline comes first I am not getting how to set its class as active.
code is like below
<ul class="nav-tabs" role="tablist">
<?php
$tabs = array();
if(!$sessId == "" && !$checkcourse){
$tabs = array("introduction"=>true, "outline"=>true,"announcement"=>false,"discussion"=>false,"review"=>true, "student"=>true, "comment"=>true);
} else if($sessId == "") {
$tabs = array("introduction"=>true, "outline"=>true,"announcement"=>false,"discussion"=>false,"review"=>true, "student"=>false, "comment"=>true);
} else {
$tabs = array("introduction"=>false, "outline"=>true,"announcement"=>true,"discussion"=>true,"review"=>true, "student"=>true, "comment"=>false);
}
?>
<?php if($tabs['introduction']) { ?>
<li class="active">Introduction</li>
<?php } ?>
<?php if($tabs['outline']) { ?>
<li>Outline</li>
<?php } ?>
<?php if($tabs['review']) { ?>
<li>Review</li>
<?php } ?>
<?php if($tabs['student']) { ?>
<li>Student</li>
<?php } ?>
<?php if($tabs['comment']) { ?>
<li>Comment</li>
<?php } ?>
<?php if($tabs['announcement']) { ?>
<li>Announcement</li>
<?php } ?>
<?php if($tabs['discussion']) { ?>
<li>Discussion</li>
<?php } ?>
</ul>
Simple check with ternary operator is:
<?php if($tabs['outline']) {?>
<li <?=$tabs['introduction']? '' : ' class="active"';?>>Outline</li>
<?php } ?>
So you check if $tabs['introduction'] isset then you don't need class="active", else - add this class.
Update:
But as first item of tab can change, I advise to create simple foreach:
$is_first = true;
foreach ($tabs as $tab_name => $value) {
if ($value) {
echo '<li' . ($is_first? ' class="active"' : '') . '>' . $tab_name . '</li>';
$is_first = false; // fix that we have first value here
}
}
Here your main problem is how to link href value and caption.

Displaying nav if path equals path

I have a php page with two navs. One is an admin nav, the other is public. using the code below I'm trying to determine the directory i'm in and depending on that show the proper nav. I feel like this php snippet should work.
<?php
$public = APP_PUBLIC_PATH;
$admin = APP_ADMIN_PATH;
if(is_dir($public)) {
$publicnav = "showme";
$adminnav = "hideme";
}
else if (is_dir($admin)) {
$publicnav = "hideme";
$adminnav = "showme";
}
?>
<nav class="<?php echo $publicnav; ?">
<nav class="<?php echo $admin; ?">
I've also tried the following:
<?php
$public = APP_PUBLIC_PATH;
$admin = APP_ADMIN_PATH;
if(is_dir($public)) {
$publicnav = "showme";
$adminnav = "hideme";
}
else {
$publicnav = "hideme";
$adminnav = "showme";
}
?>
<nav class="<?php echo $publicnav; ?>">
<nav class="<?php echo $admin; ?>">
is_dir only checks to see if the path you provided is a directory or not. Your goal is to check whether the current path is for the admin or for a regular user?
Look into the $_SERVER superglobal variable on how to get the current URI. I believe something like this.
$currentPath = $_SERVER['REQUEST_URI'];
if ($currentPath == $public) {
// do logic
} else if ($currentPath == $admin) {
// other logic
}

OOP PHP MySQL return multiple rows and variable

I am new w/ OPP and big pardon if my question maybe too simple :)
Table category, navigation, etc contains multiple rows (category : samsung, apple, etc; and navigation : about us, terms, etc) and both stand as Menu in all pages (home, product,etc)
My old php code and work good is below
<div id="categories">
<ul>
<?
$mydbcategories = new myDBC();
$resultcategories = $mydbcategories->runQuery("SELECT * FROM `category`");
while ($rowcategories = $mydbcategories->runFetchArray($resultcategories)) {
echo '<li>'.$rowcategories[title].'</li>';
}
?>
</ul>
</div>
<div id="navigation">
<ul>
<?
$mydbnavigation = new myDBC();
$resultnavigation = $mydbnavigation->runQuery("SELECT * FROM `navigation`");
while ($rownavigation = $mydbnavigation->runFetchArray($resultnavigation)) { echo '<li>'.$rownavigation [title].'</li>';
}
?>
</ul>
</div>
I would like to implement OOP PHP and create class then store in classes.php
<?
class Menu{
var $title;
var $url;
function setMenu($db){
$mydbMenu= new myDBC();
$resultmenu = $mydbMenu->runQuery("SELECT * FROM `$db`");
$resultmenurows = mysqli_num_rows($resultmenu);
while ($rowmenu = $mydbMenu->runFetchArray($resultmenu)){
$this->title = $rowmenu[title];
$this->url = $rowmenu[url];
}
}
function getTitle() { return $this->title;}
function getUrl() { return $this->url;}
}
?>
Then i'm edit my old code with new one below;
<div id="categories">
<ul>
<?
$catmenu = new Menu();
while ($catmenu ->setMenu('category')) {
echo '<li>'.$catmenu->getTitle().'</li>';
}
?>
</ul>
</div>
<div id="navigation">
<ul>
<?
$navmenu = new Menu();
while ($navmenu ->setMenu('category')) {
echo '<li>'.$navmenu ->getTitle().'</li>';
}
?>
</ul>
</div>
I tested and error maybe because there are multiple rows (from table) in the setMenu func.
How can i return this multiple rows ? should i use array ?
Please help me to solve this and any reply really very appreciate
You are coding PHP4 OOP style, this is very outdated. Don't use var, use public, protected, private.
$this->title = $rowmenu[title] in here, title is used as a constant (no quotes), proper: $this->title = $rowmenu['title'], same with $rowcategories[title]
"SELECT * FROM $db" is this correct? Or do you mean SELECT * FROM menu WHERE xxx='" . $db . "', do you catch errors if the lookup fails?
You should also look at PHP design patterns and code style to improve!
Try following PHP code
<?
class Menu {
var $title;
var $url;
function setMenu($db) {
$mydbMenu = new myDBC();
$resultmenu = $mydbMenu->runQuery("SELECT * FROM `$db`");
$resultmenurows = mysqli_num_rows($resultmenu);
$this->title = array();
$this->url = array();
while ($rowmenu = $mydbMenu->runFetchArray($resultmenu)) {
$this->title[] = $rowmenu['title'];
$this->url[] = $rowmenu['url'];
}
}
function getTitle($ind) {
return $this->title[$ind];
}
function getUrl($ind) {
return $this->url[$ind];
}
}
?>
And HTML
<div id="categories">
<ul>
<?
$catmenu = new Menu();
$catmenu->setMenu('category');
$i = 0;
while ($catmenu->getTitle($i)) {
echo '<li>' . $catmenu->getTitle($i) . '</li>';
$i++;
}
?>
</ul>
</div>
<div id="navigation">
<ul>
<?
$navmenu = new Menu();
$navmenu->setMenu('navigation');
while ($navmenu->getTitle($i)) {
echo '<li>' . $navmenu->getTitle($i) . '</li>';
$i++;
}
?>
</ul>
</div>

Add the page name to the body tag

I have the following PHP script that will add a class of .Active to the current open page - this bit works but I am also trying to also add the page name to the body tag as an ID "#", but it does not seem to be working how I do it. Can anyone please advice me?
<!--add class .active to current page-->
<?php
$directoryURL = $_SERVER['REQUEST_URI'];
$path = parse_url($directoryURL, PHP_URL_PATH);
$components = explode('/', $path);
$currentPage = preg_replace("/\\.[^.\\s]{3,4}$/", "", end($components));
if ($currentPage == "") {
$currentPage = "index";
}
function href($url) {
global $currentPage;
$path = explode('/', $url);
$page = preg_replace("/\\.[^.\\s]{3,4}$/", "", end($path));
echo 'href="' . $url . '" ';
if ($page == $currentPage) {
echo 'class="active"';
}
}
?>
Here is the menu:
<li><a <?php href('index.php'); ?>>Home</a></li>
<li><a <?php href('about.php'); ?>>About</a></li>
<li><a <?php href('treatments.php'); ?>>Treatments</a></li>
And the HTML code:
<html>
<head>
<title></title>
</head>
<body>
</body>
</html>
It is very simple add that variable $page in echo
echo "class='active' id='$page'";
Basically, if you don't want to modify the template, you can't.
The solution would be to do this in your template, but it must be obvious to you:
<body <?php echo $id; ?>>
What seems less obvious to you, is just that you can't do this without touching the template.
Also, you should avoid global variables.

Best way to highlight tabs according to the page you're on with CakePHP?

So prior to being introduced to CakePHP, I'd highlight the appropriate navigation tab according to the url with the following (rather sloppy) code I wrote (fyi absolute_url was a function I wrote to get the absolute path) :
$page = $_SERVER['PHP_SELF'];
// add all possible states for the navigation to an array
$checkNav = array(
"index" => "index",
"new" => "new",
"random" => "random",
"submit" => "submit"
);
$compareAgainst = strpos($page, $checkNav['index']);
if ($compareAgainst == 0) {
echo "<li><span class=\"navBorder\">Popular</span></li>\n";
} else {
echo "<li>Popular</li>\n";
}
$compareAgainst = strpos($page, $checkNav['new']);
if ($compareAgainst == 0) {
echo "<li><span class=\"navBorder\">New</span></li>\n";
} else {
echo "<li>New</li>\n";
}
$compareAgainst = strpos($page, $checkNav['random']);
if ($compareAgainst == 0) {
echo "<li><span class=\"navBorder\">Random</span></li>\n";
} else {
echo "<li>Random</li>\n";
}
$compareAgainst = strpos($page, $checkNav['submit']);
if ($compareAgainst == 0) {
echo "<li><span class=\"navBorder\">+ Submit a Link</span></li>\n";
} else {
echo "<li>+ Submit a Link</li>\n";
}
Now, I've noticed that in Cake, to determine the relative path, I can just go:
<?= $this->here; ?>
Is there a better way to do this, or should I just implement this (new) method with the old code?
You can do the following
Add this to app_helper.php if you need it in multiple pages. You feed this function with the controller and the action you want to check you want to compare against. The function compares it with the current page and return true if they match.
function isActive($controller, $actions = array())
{
foreach ($actions as $action)
{
if ($controller == $this->params['controller'] && $action == $this->params['action'])
{
return true;
}
}
return false;
}
And then generate your links like so:
<ul class="left">
<li <?php if($html->isActive('controller_name', array('index'))) { echo 'class="active"'; } ?>><?php echo $html->link('Index', '/index'); ?></li>
<li <?php if($html->isActive('controller_name', array('new'))) { echo 'class="active"'; } ?>><?php echo $html->link('New', '/new'); ?></li>
<li <?php if($html->isActive('controller_name', array('random'))) { echo 'class="active"'; } ?>><?php echo $html->link('Random', '/random'); ?></li>
<li <?php if($html->isActive('controller_name', array('submit'))) { echo 'class="active"'; } ?>><?php echo $html->link('Submit', '/submit'); ?></li>
</ul>
If the function returns true, the link will have class="active". Adapt it to your needs.
The way I've always done this is to give your body tag an id, and use css to target it. If your views are all separate then you can hard code the body id. If you are using some sort of template that adds in the header, content, footer etc., then just pass the id as a variable to the header view or wherever the body tag is (really any outer container/div that will be on every view and contain your navigation tabs). Also you will need to give your navigation tab id's to target each one.
Then just some css like this:
#homepage a#hometab,
#aboutpage a#abouttab,
#productpage a#productstab,
#contactpage a#contacttab
{
special active styling here
}

Categories