Simple Breadcrumb Issue - php

I've been trying to figure out how to make the last item in my breadcrumb have H1 tags in it. Here is a sample code without the H1.
// Each tree item has a URL and name. Some may have extra_before and extra_after.
foreach ($context['lateral_navigation'] as $link_num => $tree)
{
echo '
<li', ($link_num == count($context['lateral_navigation']) - 1) ? ' class="last"' : '', '>';
// Show something before the link?
if (isset($tree['extra_before']))
echo $tree['extra_before'];
// Show the link, including a URL if it should have one.
echo $settings['lateral_navigation_link'] && isset($tree['url']) ? '
<a href="' . $tree['url'] . '">' . $tree['name'] . '
</a>' : '' . $tree['name'] . '';
// Show something after the link...?
if (isset($tree['extra_after']))
echo $tree['extra_after'];
echo '
</li>';
I'm not a PHPer, and I've tried experimenting with it code on this line:
<li', ($link_num == count($context['lateral_navigation']) - 1) ? ' class="last"' : '', '>';
Because it shows the "last" only on the last item of the breadcrumb.
I tired this below, but obviously it didn't work:
// Each tree item has a URL and name. Some may have extra_before and extra_after.
foreach ($context['lateral_navigation'] as $link_num => $tree)
{
echo '
<li', ($link_num == count($context['lateral_navigation']) - 1) ? ' class="last"><h1>' : '', '>';
// Show something before the link?
if (isset($tree['extra_before']))
echo $tree['extra_before'];
// Show the link, including a URL if it should have one.
echo $settings['lateral_navigation_link'] && isset($tree['url']) ? '
<a href="' . $tree['url'] . '">' . $tree['name'] . '
<div id="arrow">
<div class="inside"></div>
<div class="border"></div>
</div>
</a>' : '' . $tree['name'] . '';
// Show something after the link...?
if (isset($tree['extra_after']))
echo $tree['extra_after'];
// Don't show a separator for the last one.
if ($link_num != count($context['lateral_navigation']) - 1)
echo '';
echo '
</h1', ($link_num == count($context['lateral_navigation']) - 1) , '></li>';
}
Any help with this?

Try this:
$count = count($context['lateral_navigation']); // get count
// Each tree item has a URL and name. Some may have extra_before and extra_after.
foreach ($context['lateral_navigation'] as $link_num => $tree)
{
$count--; // decrement by 1
echo ($count == 0) // if zero (last)
? '<li class="last"><h1>'
: '<li>';
// Show something before the link?
if (isset($tree['extra_before']))
echo $tree['extra_before'];
// Show the link, including a URL if it should have one.
echo $settings['lateral_navigation_link'] && isset($tree['url']) ? '
<a href="' . $tree['url'] . '">' . $tree['name'] . '
<div id="arrow">
<div class="inside"></div>
<div class="border"></div>
</div>
</a>' : '' . $tree['name'] . '';
// Show something after the link...?
if (isset($tree['extra_after']))
echo $tree['extra_after'];
// Don't show a separator for the last one.
if ($link_num != count($context['lateral_navigation']) - 1)
echo '';
echo ($count == 0) // if zero (last)
? '</h1></li>'
: '</li>';
}

Related

Show or hide certain menu in Yii header file

I want to make my website menu appear with the following conditions:
If user did not login, the menu will be like this:
Gallery | Contact Us | Login
If the user logged into the website, the menu will appear like this:
Gallery | Contact Us | Logout
This is my coding to call menu in header file:
<?php
$output = '';
$menus1 = MenuPortal::model()->findAll(array('condition' => "type='atas' AND display_status='1' AND parent_id=0 ORDER BY sort ASC"));
foreach ($menus1 as $menu) {
$submenu = MenuPortal::model()->findAll(array('condition' => "parent_id=$menu->id"));
if (sizeof($submenu) > 0) {
foreach ($submenu as $smenu) {
$output .= '<li><a href=' . $menu->url . '&id=' . PortalElement::encrypt_decrypt('encrypt', $smenu->id) . '>' . $menu->title_my . '</a></li>';
break;
}
}
else {
$output .= '<li><a href=' . $menu->url . '&id=' . PortalElement::encrypt_decrypt('encrypt', $menu->id) . '>' . $menu->title_my . '</a></li>';
}
}
echo $output;
?>
What I was thinking is to check if the session is active, then show menu #2. If there is no session, then show menu #1.
After Your foreach loop just apply following condition..
if(Yii::app()->user->isGuest()){
$output. = "<li><a href='login'>Login</a></li>";
}else{
$output. = "<li><a href='login'>Logout</a></li>";
}

issue with generating links from database in PHP

First of all, I will try and keep this as simple as possible.
This is part of a larger script that generates a breadcrumb link list depending on which category the user is currently in on the page.
Something like:
Hats > Mens > Green
Each of the categories also have their corresponding links so the user can click back on it.
The problem I'm having is that for some reason, I'm getting an additional link inserted that is the current category, right before the top category.
For example, the output, should be:
<b>Category:</b>
Hats >
Mens >
Green
But I am getting this:
<b>Category:</b>
Hats >
<a href="categories.php?parent_id=175828&name=Green&keywords_search=" title="">
Mens >
Green
</a>
Note that the difference is that the current category link (but without the name) gets inserted almost at the very top again, in addition to an additional link closing tag at the very end as well.
What could be the problem? I've spent nearly 6 hours on this and can't figure it out, so I'd really appreciate it if someone could tell me what is wrong. Maybe I spent too much looking at this and am "stuck inside the box" :)
function category_navigator ($parent_id, $show_links = true, $show_category = true, $page_link = null, $additional_vars = null, $none_msg = null, $reverse_categories = false)
{
global $reverse_category_lang, $category_lang, $db;
(string) $display_output = NULL;
(int) $counter = 0;
$none_msg = ($none_msg) ? $none_msg : GMSG_ALL_CATEGORIES;
$page_link = ($page_link) ? $page_link : $_SERVER['PHP_SELF'];
if($parent_id > 0)
{
$root_id = $parent_id;
while ($root_id > 0)
{
$row_category = $db->get_sql_row("SELECT category_id, name, parent_id, hover_title FROM
" . DB_PREFIX . (($reverse_categories) ? 'reverse_categories' : 'categories') . " WHERE
category_id=" . $root_id . " LIMIT 0,1");
if($counter == 0)
{
$display_output = ($reverse_categories) ? $reverse_category_lang[$row_category['category_id']] : $category_lang[$row_category['category_id']];
$display_output = (!empty($display_output)) ? $display_output : $row_category['name'];
$ace = $row_category['category_id'];
$acename = $row_category['name'];
}
else if($parent_id != $root_id)
{
$hover = $db->get_sql_field("SELECT hover_title FROM " . DB_PREFIX . "categories WHERE category_id='".$ace."'","hover_title");
$category_name = ($reverse_categories) ? $reverse_category_lang[$row_category['category_id']] : $category_lang[$row_category['category_id']];
$category_name = (!empty($category_name)) ? $category_name : $row_category['name'];
$display_output = (($show_links) ? '<a href="' . $page_link . '?parent_id=' . $row_category['category_id']
. '&name=' . $category_name . ((!empty($additional_vars)) ? ('&' . $additional_vars) : '')
. '" title="'.$row_category['hover_title'].'">' : '') . $category_name . (($show_links) ? '</a>' : '</a>')
. ' > <a href="'. $page_link . '?parent_id=' . $ace . '&name=' . $acename . ((!empty($additional_vars)) ? ('&' . $additional_vars) : '')
. '" title="'.$hover.'">' . $display_output . '</a>';
}
}
$counter++;
$root_id = $row_category['parent_id'];
}
$display_output = (($show_links && $show_category) ? '<b>Category:</b>' : '') . $display_output;
}
$display_output = (empty($display_output)) ? $none_msg : $display_output;
return $display_output;
}

PHP/WordPress: override page_id in foreach loop

I have a WordPress site and currently the code it set to create titles based on the page title. The titles are: Interior House Painting, Exterior House Painting and Commercial Painting. I would like to override the titles to remove the word "house". This is the code currently:
<?php
// interior_painting: 18
// exterior_painting: 25
// other services: 36
$page_ids = array(18, 25, 36);
$images = array('servicesInterior.jpg', 'servicesExterior.jpg', 'servicesOther.jpg');
foreach ($page_ids as $key => $page_id) {
$page_post = get_post($page_id);
$page_custom_key = 'home_page_info';
$page_link = $page_post->post_name;
$li_class = $page_id == 36 ? 'noMargin' : '';
$title = $page_id == 18 ? 'Interior Painting' : $page_post->post_title . " ";
$title = $page_id == 36 ? 'Commercial Projects' : $page_post->post_title . " ";
$title = $page_id == 25 ? 'Exterior Painting' : $page_post->post_title . " ";
echo '<li class="' . $li_class . '"> <a href="' . $page_link . '">';
echo '<img class="alignright size-full wp-image-349" title="servicesInterior" src="/wp-content/uploads/2011/04/' . $images[$key] . '" alt="" width="200" height="95" /></a>';
echo '<h2>' . $title . '</h2>';
echo get_post_meta($page_id, $page_custom_key, true);
echo '<a class="btn-find-more" href="' . $page_post->post_name . '">FIND OUT MORE</a></li>';
}
?>
The output is this: Interior House Painting, Exterior Painting, Commercial Painting. How do I get "house" removed from "Interior House Painting"?
$title = ucwords(trim(strtolower(str_replace('house', '', $page_post->post_title))));
Though, I am a little curious as to why you would not prefer to just remove the word from the title altogether.
ADDITIONALLY: Somebody else mentioned using preg_replace. That's also entirely possible, but it would be best to do a case-insensitive search:
$title = trim(preg_replace('/house/i', '', $page_post->post_title));
Just use preg_replace('House'|| 'house','',/*Variable Page title is in*/);
You could try replacing the line:
echo '<h2>' . $title . '</h2>';
With something like this:
if (is_page('Interior House Painting')) { echo '<h2>Interior Painting</h2>'; }
if (is_page('Exterior House Painting')) { echo '<h2>Exterior Painting</h2>'; }
else { echo '<h2>' . $title. '</h2>'; }

PHP Dynamic Count & Limit Menu items

I want to modify this code which works pretty good but (or I don't know because I'm new with php) I can't limit the number of li's displayed for the main elements in the menu. The actual code will echo all elements it finds, I want to limit the times
<li><a href='{$sLink}' {$sOnclick} target='_parent'>{$sPictureRep}{$sText}</a>
this line is echoed.. let's say to echo just the first 15 elements + a "MORE" button under which to display the rest of the elements as sub-menus.. (this is a 2 level horizontal menu). Can someone please help me? I really tried a lot but I'm not an expert in PHP..
Thanks!
<?php
require_once( '../../../inc/header.inc.php' );
require_once( DIRECTORY_PATH_INC . 'membership_levels.inc.php' );
require_once( DIRECTORY_PATH_ROOT . "templates/tmpl_{$tmpl}/scripts/TemplMenu.php" );
class SimpleMenu extends TemplMenu
{
function getCode()
{
$this->iElementsCntInLine = 100;
$this->getMenuInfo();
$this->genTopItems();
return $this->sCode;
}
function genTopItem($sText, $sLink, $sTarget, $sOnclick, $bActive, $iItemID, $isBold = false, $sPicture = '')
{
$sActiveStyle = ($bActive) ? ' id="tm_active"' : '';
if (!$bActive) {
$sAlt= $sOnclick ? ( ' alt="' . $sOnclick . '"' ) : '';
$sTarget = $sTarget ? ( ' target="_parent"' ) : '';
}
$sLink = (strpos($sLink, 'http://') === false && !strlen($sOnclick)) ? $this->sSiteUrl . $sLink : $sLink;
$sSubMenu = $this->getAllSubMenus($iItemID);
$sImgTabStyle = $sPictureRep = '';
if ($isBold && $sPicture != '') {
$sPicturePath = getTemplateIcon($sPicture);
$sPictureRep = "<img src='{$sPicturePath}' style='vertical-align:middle;width:16px;height:16px;' />";
$sText = ' ';
$sImgTabStyle = 'style="width:38px;"';
}
$sMainSubs = ($sSubMenu=='') ? '' : " {$sSubMenu} </a>";
$this->sCode .= "
<li><a href='{$sLink}' {$sOnclick} target='_parent'>{$sPictureRep}{$sText}</a>
<div id='submenu'>
<ul>
<li>{$sMainSubs}</li>
</ul>
</div>
</li>
";
}
}
$objMenu = new SimpleMenu();
echo "<ul id='ddmenu'>";
echo $objMenu->getCode();
echo "</ul>";
?>
It's difficult to tell exactly where you want to do this in your code, but I figure you're looking for something like:
<?php
for ($i = 0; i < 15; i ++){
echo "<li><a href='{$sLink}' {$sOnclick} target='_parent'>{$sPictureRep}{$sText}</a>"
}
echo "<li><a href='more' target='_parent'>More...</a>"
?>

How to properly build a navigation menu that highlights the current page

I've setup a menu for a fairly simple site based on icant.co.uk. It's fairly simple with maybe 5 pages. The small site is mainly a mysql browser for a few tables using MATE. Theres a common.php file that contains the header & footer HTML so thats where I put the code below.
The code below highlights the current page on the menu. Its ugly and I'm sure there has to be a better way to do it.
Any help is appreciated, thank you!
heres my code
<?php
$currentFile = Explode('/', $_SERVER["PHP_SELF"]);
$currentFile = $currentFile[count($currentFile) - 1];
if ($currentFile == "orders.php"){
echo '<li id="active">Orders</li>';
}
else{
echo '<li>Orders</li>';
}
if ($currentFile == "customers.php"){
echo '<li id="active">Customer List</li>';
}
else{
echo '<li>Customer List</li>';
}
if ($currentFile == "order_details.php"){
echo '<li id="active">Order Details</li>';
}
else{
echo '<li>Order Details</li>';
}
?>
UPDATE For those curious, below is the working code!
<?php
$currentFile = Explode('/', $_SERVER["PHP_SELF"]);
$currentFile = $currentFile[count($currentFile) - 1];
// easier to manage in case you want more pages later
$pages = array(
array("file" => "orders.php", "title" => "Orders"),
array("file" => "order_details.php", "title" => "Order Details"),
array("file" => "customers.php", "title" => "Customer List")
);
$menuOutput = '<ul>';
foreach ($pages as $page) {
$activeAppend = ($page['file'] == $currentFile) ? ' id="active"' : "";
$currentAppend = ($page['file'] == $currentFile) ? ' id="current' : "";
$menuOutput .= '<li' . $activeAppend . '>'
. '' . $page['title'] .''
. '</li>';
}
$menuOutput .= '</ul>';
echo $menuOutput;
?>
What I normally do is something like (for all elements...):
<li class="<?php if (condition) echo 'selected'; ?>">content part, links, etc.</li>
Not sure if that's what you meant, but this way you'll get rid of this ugly if-else:
$currentFile = Explode('/', $_SERVER["PHP_SELF"]);
$currentFile = $currentFile[count($currentFile) - 1];
// easier to manage in case you want more pages later
$pages = array(
array("file" => "orders.php", "title" => "Orders"),
array("file" => "customers.php", "title" => "Customer List")
);
$menuOutput = '<ul>';
foreach ($pages as $page) {
$activeAppend = ($page['file'] == $currentFile) ? ' id="active"' : "";
$menuOutput .= '<li' . $activeAppend . '>'
. '' . $page['title'] .''
. '</li>';
}
$menuOutput .= '</ul>';
echo $menuOutput;
A more concise way of doing it (if you have short tags enabled) would be:
<li class="<?= $test=="your_page_name" ? 'selected' : 'not_selected'?>">Link Name</li>
It performs the same function as the first answer, just more concisely.
Here's a snippet from a project of mine. It it old ugly code, and uses tables, but you can just as easily use the idea for divs and cleaner markup. The trick is to make the navigation use a different class if the current page matches it's url.
<td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_home.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_home.php'>Billing Home</a></td></tr>
<td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_schedules.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_schedules.php'>Billing Schedules</a></td></tr>
<td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_outstanding.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_outstanding.php'>Outstanding</a></td></tr>
<td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_list.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_list.php'>List All</a></td></tr>
<td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_history.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_history.php'>Billing History</a></td></tr>
<td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_statement_history.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_statement_history.php'>Statement History</a></td></tr>

Categories