I have a menu generated in php this way.
<?php
while($rowMenu = mysql_fetch_array($rsMenu)){
$link="category.php?cat=".$rowMenu['MenuItemID'];
$name = utf8_encode($rowMenu['name']);
?>
<li><?php echo $name; ?></li>
<?php
}
?>
and now I want to add a background-color to the item of the present page.The background-color is defined in css ( .productActive )
I search for add a css class in php like I make with javascript but didnt find any solution, so I made this way
<?php
$cat=$_GET['cat']; /gets the id from the URL
while($rowMenu = mysql_fetch_array($rsMenu)){
$link="category.php?cat=".$rowMenu['MenuItemID'];
$name = utf8_encode($rowMenu['name']);
?>
<li><?php echo $name; ?></li>
<?php
if($cat == $rowMenu['MenuItemID']) {
echo"<li class='productActive'>".$nome."</li>";
}
}//end of while
?>
But this way add one more item to the menu. it repeats the present li item. Is there any other way??
Thanks
while($rowMenu = mysql_fetch_array($rsMenu)){
$link="category.php?cat=".$rowMenu['MenuItemID'];
$name = utf8_encode($rowMenu['name']);
if($cat == $rowMenu['MenuItemID']) {
echo"<li class='productActive'>".$name."</li>";
} else {
echo"<li>".$name."</li>";
}
}
That should work, I think
while($rowMenu = mysql_fetch_array($rsMenu)){
$link = "category.php?cat=".$rowMenu['MenuItemID'];
$name = utf8_encode($rowMenu['name']);
// add active class?
$class = $cat == $rowMenu['MenuItemID']
? ' class="productActive"'
: '';
?>
<li<?= $class; ?>><?= $name; ?></li>
<?php
}//end of while
Then if the item matches, it'll add the class to the tag, otherwise it won't add anything.
Although I would prefer this:
printf('<li%s>%s</li>', $class, $link, $name);
p.s. Anchors should always be inside the <li>, not outside.
Related
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
}
I'm listing all my tables (that I see in phpMyAdmin) names using HTML.
I would simply like to "hide" (just in the HTML) the ones which contain "XYZ" in the name of the table.
I made an attempt- but I have not been successful.
<?php foreach($tables as $table):?>
<li <?php if($table['XYZ'] == $theTable):?>class="active"<?php endif;?>>
<span class="fui-list-small-thumbnails"></span> <?php echo $table['table'];?>
</li>
<?php endforeach;?>
Sure it is possible, either include the list item in the HTML source, but hidden...
<?php
foreach($tables as $table) {
$pos = strpos($table['table'],'XYZ');
$class = $pos === false ? 'style="display:none"' : '';
$href = site_url('db/'.$theDB."/".$table['table']);
echo "<li {$class}><span class=\"fui-list-small-thumbnails\"></span> ".$table['table']."</li>";
}
?>
Or do not even include the list item in the HTML source
<?php
foreach($tables as $table) {
$pos = strpos($table['table'],'XYZ');
if ($pos === false) {
$href = site_url('db/'.$theDB."/".$table['table']);
echo "<li><span class=\"fui-list-small-thumbnails\"></span> ".$table['table']."</li>";
}
}
?>
Key function to test if a string contains another string is strpos. Also I have refactored your code so there is less switching between "view" and "controller" (where view is the HTML being echoed out and the controller is the logic) as this makes code much more readable.
http://php.net/manual/en/function.strpos.php
https://en.wikipedia.org/wiki/Model-view-controller
You can simply make the table visibility hidden like this
<li <?php if($table['XYZ'] == "XYZ"):?>style="visibility: hidden;"<?php endif;?>>
or you can change the visibility: hidden; to display:none;
I think you can achieve like this way.
<?php foreach($tables as $table):?>
$hideTable = "";
$hideTable = ($table['XYZ'] == $theTable)? "style='display:none'":'';
<li <?php echo $hideTable;?> >
<span class="fui-list-small-thumbnails"></span> <?php echo $table['table'];?>
</li>
<?php endforeach;?>
Try above code may helps you.
<?php foreach($tables as $table):?>
<li <?= (($table['XYZ'] == $theTable) ? 'style="display:none"' : "");?>
<span class="fui-list-small-thumbnails"></span> <?php echo $table['table'];?>
</li>
<?php endforeach;?>
Try to make the entry invisible, but it would be more performant if you
a) filter in the query or
b) filter before printing the HTML
I've been doing some researching and discovered this nice website http://validator.w3.org/
After running a test i see that my navigation get loads of error.
The error i get is the ul in the coding. I don't close it off because i give it it styling if a condition is met else close it.
But this is not a valid solution according to w3 which gives me some errors.
Can you help me find a fix for my navigation so it gets valid?
With thanks Jim
<ul id="nav-container">
<?php
$select_category = mysql_query("SELECT * FROM menu WHERE hidden = 0 ORDER BY menu ASC");
while ($ln = mysql_fetch_array($select_category)) {
$idcat = $ln['nmr'];
$catname = $ln['menu'];
$catsname = str_replace(' ', '-', $ln['menu']);
echo '<li>';
if($catname == "carparts") {
echo '<span><strong>'.$catname.'</strong></span>';
} else {
echo '<span><strong>'.$catname.'</strong></span>';
}
This gives me the errors: (fixed)
echo '<ul';
if(isset($_GET['cats'])) {
if ($_GET['cats'] == $idcat) {
echo ' style="display:block;">';
} else {
echo '>';
}
The fix: also deleted the closing bracket at the end.
echo '<ul';
if(isset($_GET['cats']) && $_GET['cats'] == $idcat){
echo ' style="display:block;">';
} else {
echo '>';
}
/fix finish
//Error finish
$select_sub = mysql_query("SELECT * FROM submenu WHERE nmrmenu = '$idcat' AND hidden = 0");
while ($lsub = mysql_fetch_array($select_sub)) {
$subname = $lsub['submenu'];
$subsname = str_replace(' ', '-', $lsub['submenu']);
$pil = 'ยป';
$brnr = $lsub['nmr'];
if(isset($_GET['cat'])) {
if ($_GET['cat'] == $brnr) {
$subname = '<u>'.$lsub['submenu'].'</u>';
} else {
$subname = $lsub['submenu'];
}
}
echo '<li><span><strong> '.$subname.' </strong></span></li>';
}
echo '</ul>';
echo '</li>'; // to this location
}
echo '</li>'; //relocated this up one line
} **//Deleted this one!**
?>
</ul>
Update
The output gets like this which is horrible: (fixed)
<ul</li><li><span><strong>name</strong></span>
<ul</li><li><span><strong>name</strong></span><ul</li>
New: now the ul problem is gone, but it's complaining about the li's. Any clue why?
Error: document type does not allow element "li" here; missing one of "ul", "ol", "menu", "dir" start-tag
<ul id="nav-container">
<li>
<span><strong>name</strong></span>
<ul>
<li>
<span><strong> name </strong></span>
</li>
</ul>
</li>
</ul>
/update
You have </ul> and then immediately follow it with a <li>. You can't have a list item outside of a list.
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>
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
}