PHP active section after page refresh - php

I am trying to create a one page navigation menu in PHP. The reason I want it to be in PHP is because I can add other code later and I want the user to be on the same page even after a refresh.
I have an unordered list with generated list items. The list items are generated with an array.
(I want the key/values separated because the actual code will be different. This is just an example.)
$nav = array(
"home" => "home",
"about" => "about",
"contact" => "contact"
);
echo '<ul>';
foreach( $nav as $id => $name )
{
echo '<li>'.$name.'</li>';
}
echo '</ul>';
// outputs:
<ul>
<li>home</li>
<li>about</li>
<li>contact</li>
</ul>
Then I want some divs to be the sections
foreach( $nav as $s_id => $s_name)
{
echo '<div id="'.$s_id.'">Some section text</div>';
}
Now I need to know how can I set the display to either block or none based on current id active?

While I personally think this is more of a job for JavaScript, in the question and a comment above you specify that you want to do this in PHP. In that case those anchor tags will need to be actual links to the page. For the sake of this example I'll assume the page is called index.php. (If it's something different, just use whatever your page is.)
Update the anchors to be links to the page with query string parameters:
<li>home</li>
<li>about</li>
<li>contact</li>
Then in your PHP code you'd determine which one is "active" by the active query string parameter. Start with a default of "home" in case no parameter is specified (it's the user's first time on the page):
$active = "home";
if (isset($_GET["active"])) {
$active = $_GET["active"];
}
Then when rendering the <div> elements you can specify their style:
foreach( $nav as $s_id => $s_name)
{
echo '<div id="'.$s_id.'" style="display:'.($s_id == $active ? "block" : "none").'">Some section text</div>';
}

Related

Bootstrap navbar dropdown not working only from one page

I have a navigation bar with 3 button, saying Home, Films and Genres. Clicking the Genre should open the dropdown with the list of all Genres generated from database and on click to each genre, genrepage containing the list of films of selected genre shows up. But the Genre button (with dropdown) is not working when I am trying to access it from home page. But it works fine for any other pages. I am using Bootstrap 3.3.7 navbar and any help will be appreciated.
My navbar.php
<?php
/*
array of pages
this builds the navigation list
format:
filename => URL name
*/
$navArray = array(
'index' => 'Home',
'film' => 'Films',
'genre' => 'Genre',
);
?>
//some html code here
<?php
foreach($navArray as $key => $nav){
//assign active class to currently active page
if(strstr($_SERVER['SCRIPT_NAME'], $key))
$active = ' active';
else $active = '';
//if Genre is selected, display dropdown menu
if($nav=='Genre'){
echo '<li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#">Genre<span class="caret"></span></a>
<ul class="dropdown-menu">';
//generate dropdown from database, giving the list of genres
$queryGenreList="SELECT genre.`name`
from genre;
";
$resultGenreList=$db->query($queryGenreList);
if($resultGenreList->num_rows > 0){
while($row = $resultGenreList->fetch_assoc()){
echo '<li>'.$row["name"].'</li>';
}
}
echo '</ul>
</li>';
}
echo"<li class='nav-item".$active.";'>";
if($nav!='Genre'){
echo"<a class='nav-link' href=".$key.".php>".$nav."</a>";
}
}
?>
</li>
If the bootstrap menu is failing to work when navigating away from the homepage, as it sounds like bootstrap.js isn't running on your subpages for the dropdown to work. Check your network tab > js and see if bootstrap.min.js is being loaded in or view the source.

Can I show a list of contacts within category, on the 'List all contact categories' page in Joomla?

So in a nutshell, you have two options for displaying Contacts in Joomla:
Show all Joomla Contact Categories.
Show all Joomla Contacts in a single Category.
I want to use the first option, but merge a list underneath each Category showing the list of contacts within that category, and a link to their profile.
The simplest way I thought of this was to edit a template override of the file com_contact/categories/default_items.php
I found a point where I want the list to appear, and then copied and pasted the code from the Category view (that generates the list of contacts).
<ul>
<?php // Add list of contacts for each category
foreach ($this->items as $i => $item) : ?>
<li>
<a href="<?php echo JRoute::_(ContactHelperRoute::getContactRoute($item->slug, $item->catid)); ?>">
<?php echo $item->name; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
But I am assuming I can't just copy and paste, as there needs to be an extra node added to $this->items.
At the moment, no list is being generated, just the <ul> outside the foreach loop.. but also interestingly, the <li> and the <a> IS being generated.. but linking to the current page I'm on (Probably because $item->slug is still being seen as the category).
So can anyone point me in the right direction as to how to reference the contacts within a category? All I'm after is the name and the slug/URL.
UPDATE:
I saw this in the same file (default_items.php) and although I realise it's referring to child categories... would this be a place to start for the actual contacts within the categories?
<?php if (count($item->getChildren()) > 0) :?>
<div class="collapse fade" id="category-<?php echo $item->id;?>">
<?php
$this->items[$item->id] = $item->getChildren();
$this->parent = $item;
$this->maxLevelcat--;
echo $this->loadTemplate('items');
$this->parent = $item->getParent();
$this->maxLevelcat++;
?>
</div>
<?php endif; ?>
BUMP - Does anyone have any experience with this? Or being able to call individual contacts when viewing a category? How are they linked?
For Category view in file default_children.php after tag <li... add code:
<?php
// Get Category Model data
$categoryModel = JModelLegacy::getInstance('Category', 'ContactModel', array('ignore_request' => true));
$categoryModel->setState('category.id', $child->id);
$categoryModel->setState('list.ordering', 'a.name');
$categoryModel->setState('list.direction', 'asc');
$categoryModel->setState('filter.published', 1);
$contacts = $categoryModel->getItems();
?>
For Custom Fields add this after previus code:
JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
foreach($contacts as $contactItem) {
$currentContFields[] = FieldsHelper::getFields('com_contact.contact', $contactItem, true);
}

how to get a wordpress blog page to show differnt nav bar from maine page

Hi im a web dev an im trying to get word press to use a 2nd navigation for my blog section. i have the main site all set up but what i want is when i hit the blog in my top nav to show a differnt navigation for the blog section. i have tried using the following code in the header.php
<?php
if (is_single('blog')){
wp_nav_menu(array('menu'=>'secondary' ));
}
?>
but this code does not seem to work even though the theme supports 2 navigations.
<nav class="Seo-nav">
<div class="Seo-nav-inner">
<?php
echo theme_get_menu(array(
'source' => theme_get_option('theme_menu_source'),
'depth' => theme_get_option('theme_menu_depth'),
'menu' => 'primary-menu',
'class' => 'Seo-hmenu'
)
);
get_sidebar('nav');
?>
the above code is the code i use to call my navigation. is there any way to get a specific page or pages to show the one menu? any help on this would be great. never had to make certain pages have different navigation before so this is a new one on me.
Like was said in comments earlier, you'll need Conditional Tags. Probably for you specifically, the is_page() conditional.
<?php
if (is_page('blog')) {
// Echo this menu on the blog slug page
wp_nav_menu(array(
'theme_location' => '[name_of_menu]'
));
} else {
// Otherwise, echo this one
wp_nav_menu(array(
'theme_location' => '[name_of_main_menu]'
));
}
See the codex for the wp_nav_menu attributes. Can print out the menu other ways, that's just the one I recommend.
Depending how you blog is set up, is_page('blog') may need to be is_home(), 'blog' == get_post_type(), or some other variation.
I think you can go here two ways.
One is to only show children of current page on the second like
function show_different_nav(){
global $post;
$sec_nav=wp_list_pages("child_of=".$post->ID);
return $sec_nav;
}
now call show_different_nav() in your template.
The other way would be to write an own filter. I modified the http://wordpress.org/plugins/get-different-menus/ in this case.
add_filter( 'wp_nav_menu_items', 'get_different_custom_menu_item_according_to_page_slug', 10, 2 );
function get_different_custom_menu_item_according_to_page_slug ( $items, $args ) {
$current_id=(int)$GLOBALS['post']->ID;
$current_slug=$GLOBALS['post']->slug;
$the_nmo = wp_get_nav_menu_object($current_slug);
if ($the_nmo==""){}else{
$menu_id=$the_nmo->term_id;
$items_2 = wp_get_nav_menu_items($menu_id);
$items="";
if ($args->theme_location == 'secondary') {
foreach ($items_2 as $item){
$link_page_id = (int)($item->object_id);
if ($current_id == $link_page_id){$cur=" current-menu-item current_page_item";}else{$cur="";
}
$items .= '<li class="menu-item menu-item-type-post_type menu-item-object-page'.$cur.'">'.$item->title.'</li>';
}
}
}
return $items;
}
In my opinion you need a menu for this called exactly the same as the page slug.

How to list values of one php file in another php file

I am not sure if this is possible !
I have two files say main.php and submenu.php
In main.php i have the following mainMenu
and in another file called submenu.php i have a list say
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
If i click on Pricelist in main menu i want to display the content of the submenu as a list under PriceList as
The simple solution may be including the list view in menu.php itself, But that's not the situation
Thank you.
You should use arrays and recursion. Like so:
(code is rough, sorry)
$menu = array(
'About Us',
'Categories',
'Price List' => array(
'1','2','3','4'
)
);
function loop_menu($menu){
foreach($menu as $m):
if(is_array($m))
return '<ul>' . loop_menu($m) . '</ul>';
else
return '<li>' . $m . '</li>';
endforeach;
}
echo loop_menu($menu);
Then use CSS to make it look however your want.
If you want to include a page just use
include('page.php');
But using JS & CSS is the best way
Here is a very helpful plugin
http://users.tpg.com.au/j_birch/plugins/superfish/
And don't forget to get jQuery

HighLighted item menu when clicked in codeIgniter

how can i make the active menu in codeIgniter, its not when hover, but when clicked, for example if we are in some category, that item in the menu is highlighted, how can be this done in CI?
Depends on your routing and menu generation script. Esiest method is to check for segments in uri. For example for static menu You can do this:
<?php $class = $this->uri->segment(1)=="someController"?"highlighted":""; ?>
Menu item
There are a few steps involved here.
Firstly, you need to determine which is the 'current' menu item or category. If you can structure your site so that there's a direct relationship between your URL structure and your top level menu items (and / or your categories) then this will help a lot.
You'll need a common section of code that generates your main menu. This code could iterate through an array of menu item titles to produce the HTML for the menu. If the array had keys for the URL segment and values for the menu item text...
$menuItems = Array(
"/home" => "Home",
"/products" => "Products",
"/faq" => "FAQ",
"/aboutus" => "About Us"
);
(Leading slashes included for clarity as to which are the URI segments and which are the Menu Titles only - you would usually omit the leading slash)
... then, while you're iterating through, you could check each item against the relevant segment of the current URL.
Secondly, having worked out which is the current item, you could add a css class to the relevant HTML element.
e.g.
$menuHtml = "<ul class='menu'>\r\n";
foreach($menuItems as $segment => $title) {
if($segment == $this->uri->segment(1)) {
$menuHTML .= "<li class='current'>$title</li>";
} else {
$menuHTML .= "<li>$title</li>\r\n";
}
}
$menuHtml .= "</ul>";
You'd then need to apply the required highlight styles in CSS to the li.current element:
li.current {
<your-highlight-css-styles-here>
}

Categories