In bootstrap 3 we have this <li> class set to active in order to display the current page highlighted on the navbar
<li class="active">
Link
</li>
the problem is when you are including the menu via include includes/header.php; on all your pages. i cant figure out how to put together a switch statement on the $actual_link and bring back some sort of call to insert the class active in the right place. this my so far attempt and im honestly stuck because i feel there is a better way. how can i can i put the li class to active with this switch
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$HTTPHost = $_SERVER[HTTP_HOST];
switch($actual_link)
{
case "http://{$HTTPHost}/index.php" ||
"http://{$_SERVER[HTTP_HOST]}/admin.php?edit_home":
//setToActive
break;
case "http://{$HTTPHost}/warrants.php" ||
"http://{$_SERVER[HTTP_HOST]}/admin.php?edit_warrants":
//setToActive
break;
case "http://{$HTTPHost}/faq.php" ||
"http://{$_SERVER[HTTP_HOST]}/admin.php?edit_faq":
//setToActive
break;
case "http://{$HTTPHost}/aboutus.php" ||
"http://{$_SERVER[HTTP_HOST]}/admin.php?edit_aboutus":
//setToActive
break;
}
?>
In your pages before including header set a variable to indicate the current page. For example in index page:
$active = 'index';
include('includes/header.php');
Now in your header.php while creating links use something like
<?php global $active ?>
<li <?php if( isset($active) && $active == 'index') echo 'class="active"'; ?>>
Index
</li>
you can't specify multiple expression in a case statement like this
case "http://{$HTTPHost}/index.php" ||
"http://{$_SERVER[HTTP_HOST]}/admin.php?edit_home":
the above expression evaluates to true.
use the following synatax
case "http://{$HTTPHost}/index.php":
case "http://{$_SERVER[HTTP_HOST]}/admin.php?edit_home":
//statements
break;
Related
I have a problem in my web app.
I'd like to make a menu, when menu clicked it'll be active in css.
Code below is my index.php
<?php
include 'menu.php';
$page = $_GET['page'];
switch ($page) {
case 'endCart':
include 'endCart.php';
break;
case 'trxLog':
include 'invoice.php';
break;
case 'graph':
include 'graph.php';
break;
case 'activeBasket':
include 'activeBasket.php';
break;
case 'logout':
include 'logout.php';
break;
default:
include 'endCart.php';
break;
}
?>
And code below is menu.php
<div class="column" id="sidebar">
<div class="ui secondary vertical fluid menu">
<?php
foreach($sidemenu as $arr){
echo '<a class="item" href="'.$arr[1].'">'.$arr[0].'</a>';
}
?>
</div>
</div>
I get variable $sidemenu from array with value below :
$sidemenu = array(
array('End Cart','index.php?page=endCart', 'endCart'),
array('Transaction Log','index.php?page=trxLog', 'trxLog'),
array('Graph','index.php?page=graph', 'graph'),
array('Active Basket','index.php?page=activeBasket', 'activeBasket')
);
As writen in file menu.php, there is tag <a> with item in its CSS class.
So my problem is when user clicked specific menu in that tag will append CSS class active.
Update!!
This problem has been solved, and here it is my update for menu.php. Big thanks for #Magnus and #Steve to help me solve this.
foreach($sidemenu as $arr){
echo ($page == $arr[2]) ? "<a class=\"item active\" href=\"".$arr[1]."\">".$arr[0]."</a>" : "<a class=\"item\" href=\"".$arr[1]."\">".$arr[0]."</a>";
}
First, change the order of the page and menu include. That way, $page will be available in your menu.php-file:
// I also added a check to see if that query string exists
$page = isset($_GET['page']) ? $_GET['page'] : '';
include 'menu.php';
In your menu.php, just check if the loop is printing the current url:
foreach($sidemenu as $arr){
echo '<a class="item ' . $arr[2] == $page ? "active" : null . '" href="'.$arr[1].'">'.$arr[0].'</a>';
}
This will give the current page link the class active.
Edit
I totally missed that $arr[2] already contains the page key. You don't need to build the url to compare it, as I did in my first example. Thanks #steve for pointing that out.
I have a wordpress theme displaying the Next / Previous post but having some trouble when you're viewing the first post.
Obviously on the first post there are no other posts previous, however I would like to display the next post 2 posts
My current code looks like:
<?php next_post('%','', TRUE, '1'); ?>
<?php previous_post('%','', TRUE, '1'); ?>
I usually get the current page and then apply a class to that page, I don't know if you can do this in wordpress.
// find the current page
$current_page = basename($_SERVER['SCRIPT_FILENAME']);
$class = '';
if($current_page === 'index.php'){
$class = ' hide'; //hide the previous link if the current page is index.php or the first page.
}
<ol>
<li class="<?php echo $class; ?>">prev</li>
<li>1</li>
<li>2</li>
</ol>
also check this php pagination script on github php paginator
Let'me try. You can check the position on the loop, to check if it is on beginning or in the end.
$firstPost=( $wp_query->current_post == 0 && !is_paged() );
$lastPost=( $wp_query->current_post == 0 && $wp_query->current_pos $wp->query->post_count-1 );
You can change something to get it better.
I have a question about how I can dynamically change the content to display in the webpages.
I have few portions of the website fixed - header, nav, footer, and splash and a side bar.
I only want the middle portion of my website to change based on the menu link what user clicks on.
Below is my code for index.php
<?php
include "/templates/header.php";
include "templates/menu.php";
include "/templates/splash.php";
$action = "index";
$disallowed_paths = array('header','menu','splash','bottom_page', 'footer');
if (!empty($_GET['action']))
{
$tmp_action = basename($_GET['action']);
if (!in_array($tmp_action, $disallowed_paths) && file_exists("/content/$tmp_action.php"))
$action = $tmp_action;
}
include "/content/$action.php";
include "/templates/bottom_page.php";
include "/templates/footer.php";
?>
My menu.php contains links for Home, About, products, services and login links
I just want to change the main_index.php to include the above based on what user clicks on.
please advise if this approach is good.
or should I create similar file as index.php multiple times with includes to each file as per the link clicked on menu
Your Answer is
GET method
You can use get method for that
<ul>
<li>example 1</li>
<li>example 2</li>
<li>example 3</li>
<li>example 4</li>
</ul>
After User Clicks on the link
<?php
if(isset($_GET['page']) && $_GET['page']!=""){
$page = "";
switch ($_GET['page']) {
case 'example_1':
$page = "Page_1.php";
break;
case 'example_2':
$page = "Page_2.php";
break;
case 'example_3':
$page = "Page_3.php";
break;
case 'example_4':
$page = "Page_4.php";
break;
default:
$page = "any_default_page.php";
break;
}
include($page);
}
?>
And there are other ways also. but this is the most easy and efficient
I think better approach would be whitelisting instead of blacklisting.
$existing_pages = array('home', 'contact', 'terms-of-service');
if(isset($_GET['action'] && in_array($_GET['action'], $existing_pages)
{
// load action here
} else {
//show 404 error
}
Note that your overall approach is not ideal, you could look like into modern frameworks like Laravel or Symphony which has templating systems which helps alot.
But for learning purposes this is fine:)
So I have this code that detects what page the user is on and then spits out a class of "active" if necessary.
<li <?php if (stripos($_SERVER['REQUEST_URI'],'index.php') {echo 'class="active"';} ?>>
So just to clarify, this code checks if the url has index.php in it, and if it does, it spits out the "active" class. What I need to do and don't know how to is add multiple instances to this code. So instead of just detecting index.php it needs to be able to detect other pages like about.php for example.
Sorry if this comes a very simple question to most of you but I am new to PHP.
Split your code from the layout.
Possible solution:
<?php
$active_flags = array('index.php','about.php','test.php');
$active = '';
foreach($active_flags as $item) {
if(stripos($_SERVER['REQUEST_URI'],$item)!==false) {
$active='active';
break;
}
}
?>
<li class="<?php echo $active?>">Your list Item</li>
As you are listing manually you just enter this one it really help you
<li
if(strpos($_SERVER['REQUEST_URI'],'index.php'))
{
echo 'class="active"';
}
else
{
echo 'class="inactive"';
}
</li>
I have a hard-coded menu in Drupal (as it's too complex for the standard Menu system in Drupal).
I would like to be able to say: If this page is contained within the /about/ directory, apply the class "active", so that all new pages created within this directory automatically highlight the current section.
Currently I have:
$current_page = $_SERVER['REQUEST_URI'];
<ul class="main">
<li class="home">Home</li>
<li class="about
<?php if ($current_page == "/xxxxxxx.com/dev/about/")
{
echo "active";
}
?>">About</li>
<li class="services">Services</li>
<li class="work">Work</li>
<li class="awards">Awards</li>
<li class="environment">Environment</li>
<li class="contact">Contact</li>
</ul>
I have tried a few variations of strpos and explode to get the right variable, but with no luck so far.
Thanks :)
I don't know anything about Drupal or your URL scheme, but the task of checking whether $current_page contains "/about/" you can do with:
if (strpos($current_page, '/about') !== false) echo "active";
You should probably listen to googletorp though.
Try this function. It's like arg function, but parse real path.
function real_arg($index = NULL) {
$ofset = strlen(base_path());
$q = explode('?', substr($_SERVER['REQUEST_URI'], $ofset));
$q = explode('/', trim($q[0], '/'));
return isset($index) ? $q[$index] : $q;
}
In your case:
if(real_arg(0) == 'about') echo 'active';
Use the menu api then theme your links to match what you want. You won't need to duplicate functionality that already exists. You'll acquire a skill you can reuse.
See:
http://api.drupal.org/api/function/theme_menu_item/6
http://api.drupal.org/api/function/theme_menu_item_link/6
It shouldn't take long and you'll remove a layer of workarounds.