i have created an application where i want to apply active class on active page. Like if fike name starts with me_ then how can i apply class "active" to it.
here is my code:
$pageAttr = basename($_SERVER['REQUEST_URI']);
$files = array("me_dashboard.php","me_my_team.php","me_others.php","me_inbox.php","me_outbox.php");
<li class="<?php echo($pageAttr == $files[0] || $pageAttr == $files[1] || $pageAttr == $files[2] || $pageAttr == $files[3] || $pageAttr == $files[4]) ? "active":"" ?>"><i class="icon-user icon-large"></i> Me</li>
this is not correct code to use. Please suggest me some other ways to achieve the solution in PHP.
If you have array of files like this
$files = array("me_dashboard.php",
"me_my_team.php",
"me_others.php",
"me_inbox.php",
"me_outbox.php"
);
Than you can use in_array() like
<li class="<?php echo (in_array(basename($_SERVER['PHP_SELF']), $files) ? 'active' : '')?>">
It will be even better if you port the class= attribute in the if condition as well, so that you do not get an empty class="" markup.
If you want to refactor your code more, consider porting entire code in a function, and then return the value if condition goes true.
As commented, you wanted to fetch the prefix of the file than you can use substr()
<?php
$page_name = 'me_dashboard.php';
$fetch_prefix = substr($page_name, 0, 3);
echo $fetch_prefix;
?>
<li class="<?php echo (($fetch_prefix == 'me_') ? 'active' : '')?>">A</li>
Demo - Code
you can try this one
<?php
$pageAttr = basename($_SERVER['REQUEST_URI']);
$files = array("me_dashboard.php","me_my_team.php","me_others.php","me_inbox.php","me_outbox.php");
foreach($files as $file)
{
$class_text = "";
if($file==$pageAttr)
{
$class_text = ' class="active" ';
}
?>
<li <?php echo $class_text;?> ><i class="icon-user icon-large"></i> Me</li>
<?php
}
?>
Related
Problem: One piece (That we can identify currently) is causing a clients product list to call the database over and over again (3600 times) at points when loading a longer list of products.
Code:
<?php foreach ($cats as $cat) :
if (in_array($cat->getId(), [68, 28, 27, 59, 79, 80, 119])) :
$cat_show = Mage::getModel('catalog/category')->load($cat->getId());
$children = $cat_show->getChildrenCategories($cat->getId());
$url1 = $cat_show->getURL();
$showAnyways = in_array(strtolower($cat_show->getName()), ["hats", "juniors", "accessories"]);
if ($cat_show->getShowSidebar() == 1 || $showAnyways) : ?>
<li class="current<?php if ($cat->getId() == $current_cat) { ?> active <?php } ?>">
<?php echo $cat->getName() ?>
<ul>
<?php if ($cat_show->getID() != 68 && $cat_show->getID() != 59) { ?>
<li class="current<?php if ($cat->getId() == $current_cat && $j == 0) {
$j++; ?> active<?php } ?>"><a class="view_all" href="<?php echo $url1 ?>"><?php echo $this->__("View All"); ?></a></li>
<?php } ?>
<?php foreach ($children as $subcat) {
$subcat_show = Mage::getModel('catalog/category')->load($subcat->getId());
if ($subcat_show->getShowSidebar() == 1 || in_array($subcat_show->getID(), [84])) {
$grand_children = Mage::getModel('catalog/category')->getCategories($subcat->getId());
if ($grand_children) {
$cats_displayed = 0;
foreach ($grand_children as $grand_child) {
$grand_child_show = Mage::getModel('catalog/category')->load($grand_child->getId());
if ($grand_child_show->getShowSidebar() == 1) {
$url = Mage::getModel('catalog/category')->load($grand_child_show->getId())->getURL();
?>
<li class="current<?php if ($grand_child->getId() == $current_cat && $j == 0) {
$j++; ?> active<?php } ?>">
<?php echo $grand_child_show->getName() ?>
</li>
<?php $cats_displayed++;
}
}
}
if ($cats_displayed == 0 || !$grand_children) {
$url = Mage::getModel('catalog/category')->load($subcat->getId())->getURL();
?>
<li class="current<?php if ($subcat->getId() == $current_cat && $j == 0) {
$j++; ?> active<?php } ?>">
<?php echo $subcat->getName() ?>
</li>
<?php }
}
} ?>
</ul>
</li>
<?php endif;?>
<?php endif;?>
<?php endforeach; ?>
Can anyone provide me with some pointers on how to make this FAR more efficient and not make so many DB calls.
Should note, I am not an amazing php developer by trade. Main language is python so I am trying to get some advice on the best way to go about fixing this given my less that great knowledge of php itself.
You should never have a database query call inside a for loop. You need to build a query at the start that will get all the data required before the for loop.
Some instant pointers I can see are:
$grand_child_show = Mage::getModel('catalog/category')->load($grand_child->getId());
if ($grand_child_show->getShowSidebar() == 1) {
$url = Mage::getModel('catalog/category')->load($grand_child_show->getId())->getURL();
This is calling the database twice for no reason, you should be able to do this:
$grand_child_show = Mage::getModel('catalog/category')->load($grand_child->getId());
if ($grand_child_show->getShowSidebar() == 1) {
$grand_child_show->getURL();
You should be able to drop all these 'GetModel' functions if at the start of the script you call something like:
$all_grand_children = Mage::getModel('catalog/category')->getAllCategories();
This would return a hash array which you would be able to access relevant items by doing the following inside the for loop:
$grand_children = $all_grand_children[$subcat->getId()];
This would replace
$grand_children = Mage::getModel('catalog/category')->getCategories($subcat->getId());
You should also do a initial call for all of the grand_child and cat_show objects. If you are skilled at SQL you can call just the relevant information by joining the tables in one SQL query.
I am trying to highlight currently open menu item using PHP.
HTML for my menu items.
<ul class="menu">
<li>Edit Profile</li>
<li>Edit Contact</li>
<li>Edit Facilities</li>
<li>Edit Location</li>
<li>Manage Images</li>
</ul>
This is how I tried it in PHP:
if ($_SERVER['QUERY_STRING']) {
list($queryString) = explode('&',$_SERVER['QUERY_STRING']);
$openPage = $queryString;
list($key, $value) = explode('=',$openPage);
$currentPage = $value;
// Determine what menu item to be highlight:
switch ($currentPage) {
case 'edit-profile':
$class1 = 'class="active"';
break;
case 'edit-contact':
$class2 = 'class="active"';
break;
case 'edit-facilities':
$class3 = 'class="active"';
-----------
// Default is to include the main page.
default:
$class = 'class=""';
break;
} // End of main switch.
} else {
//Determine The Index page
$path = $_SERVER['PHP_SELF'];
$indexPage = basename($path);
$indexPage = basename($path, '.php');
}
This is how I echo these classes in my menus:
<li <?php if(isset($class1)) echo $class1; ?>>Edit Profile</li>
<li <?php if(isset($class2)) echo $class2; ?>>Edit Contact</li>
This is solution is working for me. But my problem is, if I have lot of pages I need to use many class variables in SWITCH case.
Can anybody tell me is there alternative solution for this to minimize my PHP code?
Hope somebody may help me out.
Thank you.
One simple solution would be to store the menu items in a map, and iterate over them:
$menuItems = [
'edit-profile' => [
'url' => 'index.php?p=edit-profile&error=message',
'name' => 'Edit Profile'
],
'edit-contact' => [
'url' => 'index.php?p=edit-contact',
'name' => 'Edit Contacts'
],
...
]
Then iterate over the items.
<ul class="menu">
<?php
foreach($menuItems as $menuItem => $desc) {
// You get $currentPage from the query string
$class = ($currentPage === $menuItem)? 'class="active"': '';
echo '<li '.$class.'>'.$desc['name'].'</li>';
}
?>
</ul>
With this you don't need SWITCH case.
<li <?php if($currentPage=='edit-profile') echo 'class="active"'; ?>>Edit Profile</li>
<li <?php if($currentPage=='edit-contact') echo 'class="active"'; ?>>Edit Contact</li>
You can use array and try like this
if ($_SERVER['QUERY_STRING']) {
list($queryString) = explode('&',$_SERVER['QUERY_STRING']);
$openPage = $queryString;
list($key, $value) = explode('=',$openPage);
$currentPage = $value;
// Determine what menu item to be highlight:
// Store in array, you will be having only one item in array
$class[$currentPage] = 'class="active"';
} else {
//Determine The Index page
$path = $_SERVER['PHP_SELF'];
$indexPage = basename($path);
$indexPage = basename($path, '.php');
}
and the menu can be
<li <?php if(isset($class['edit-profile'])) echo $class['edit-profile']; ?>>Edit Profile</li>
<li <?php if(isset($class['edit-contact'])) echo $class['edit-contact']; ?>>Edit Contact</li>
Note: this not tested, as I have no access to PHP right now.
Rather than doing all this, just take all your menu items in an array and loop over it.
You are sending page parameter with variable.
So, instead have a control over it while sending itself.
Array of page will have page title as key and page url as value.
This way, you do not need separate variables, single variable in loop will serve the job.
<?php
$pages = array(); // Get all page titles and urls in array.
$pages['Edit Profile'] = 'edit-profile&error=message';
$pages['Edit Contact'] = 'edit-contact';
$pages['Edit Facilities'] = 'edit-facilities';
$pages['Edit Location'] = 'edit-location';
$pages['Manage Images'] = 'edit-images';
if (! empty($pages)) {
$current = (isset($_GET['p'])) ? $_GET['p'] : '';
?>
<ul>
<?php
foreach ($pages as $title => $url) {
$active = ($current == $url) ? 'active' : '';
?>
<li><?php echo $title;?></li>
<?php
}
?>
</ul>
<?php
}
?>
Use PHP to generate your own menu structure but for highlighting we can use Jquery.
See example below:
<style>
.active{ background-color:#3F6}
</style>
<ul class="menu">
<li>Home</li>
<li>Edit Profile</li>
<li>Edit Contact</li>
<li>Edit Facilities</li>
<li>Edit Location</li>
<li>Manage Images</li>
</ul>
<script type='text/javascript' src='//code.jquery.com/jquery-1.11.0.js'></script>
<script>
var aurl = window.location.href; // Get the absolute url
$('.menu li a').filter(function() {
return $(this).prop('href') === aurl;
}).parent('li').addClass('active');
</script>
I'm using include for the header
and it's have this
<li><span>Arrangements</span></li>
<li><span>Build-a-Bouquet</span></li>
<li><span>Special Events</span></li>
and I want only any current opened page get the class="current"
like this
<li class="current"><span>Build-a-Bouquet</span></li>
You can do it like this:
<?php
$menus = array();
$menus['Arrangements'] = 'arrangements.php';
$menus['Build-a-Bouquet'] = 'bouquet.php';
$menus['Special Events'] = 'events.php';
?>
<ul>
<?php
$currnet_page = $_SERVER['PHP_SELF'];
if (! empty($menus)) {
foreach ($menus as $menu => $menu_link) {
$isCurrent = ($currnet_page == $menu_link) ? 'current' : '';
?>
<li class="<?php echo $isCurrent;?>"><span><?php echo $menu;?</span></li>
<?php
}
}
?>
</ul>
Try something like this:
$active = "bouquet";
include("navigation.php");
Navigation:
<li<?php if( isset($active) && $active == "arrangements") echo " class='active'";?>><span>Arrangements</span></li>
... And so on.
This is just a quick example, $_SERVER['PHP_SELF'] can do the job.
<li><span>Arrangements</span></li>
<?php
if($_SERVER['PHP_SELF'] == '/bouquet.php'){
echo '<li class="current"><span>Build-a-Bouquet</span></li>';
}else{
echo '<li><span>Build-a-Bouquet</span></li>';
}
?>
<li><span>Special Events</span></li>
There are a few ways you can do it. Choose the one you like:
1)
Set $current_ var you want and use:
<li><a<? echo isset($current_arrangements) ? 'class="current"' : ''; ?>href="arrangements.php"><span>Arrangements</span></a></li>
<li><a<? echo isset($current_bouquet) ? 'class="current"' : ''; ?>href="bouquet.php"><span>Build-a-Bouquet</span></a></li>
<li><a<? echo isset($current_special_events) ? 'class="current"' : ''; ?>href="events.php"><span>Special Events</span></a></li>
2) same as above but use one variable and check for its value (exactly the way Niet the Dark Absol wrote)
3) wrap it to some function
<?
// Echo Class If Current
function ecic($page) {
echo eregi($page, $_SERVER['PHP_SELF']) ? 'class="current"' : '';
}
?>
<li><a <? ecic('arrangements'); ?>href="arrangements.php"><span>Arrangements</span></a></li>
<li><a <? ecic('bouquet'); ?>href="bouquet.php"><span>Build-a-Bouquet</span></a></li>
<li><a <? ecic('events'); ?>href="events.php"><span>Special Events</span></a></li>
BTW: In most common PHP configurations you can use just <? instead of <?php.
I have a class name that should change based on certain conditions but either my syntax or my logic seems to be incorrect as it doesn't work:
if ( $hometeam && ($homescore > $awayscore) || $awayteam && ($awayscore > $homescore)){
$status= 'win';
}
if ( $hometeam && ($homescore < $awayscore) || $awayteam && ($awayscore < $homescore)) {
$status= 'lose';
}
if ($homescore == $awayscore) {
$status= 'draw';
}
My elements each have a class="<?php echo $status; ?>" and I want them to be styled differently depending on the value of $status. I didn't use else if because I didn't want it to apply the value and stop but it looks like that is what is happening.
Edit: Some clarification:
<ul class="match group">
<li>
<ul class="team1 <?php echo $status; ?>">
<li class="teamname"><h2><?php echo $homename; ?></h2></li>
<li class="teamscore"><?php echo $homescore; ?></li>
</ul>
</li>
<li>
<ul class="team2 <?php echo $status; ?>">
<li class="teamname"><h2><?php echo $awayname?></h2></li>
<li class="teamscore"><?php echo $awayscore?></li>
</ul>
</li>
<li><ul class="matchinfo">
<li><button>Get Report</button></li>
</ul>
</li>
</ul>
By your logic, there are only two possible outcomes for the value of $status: 'lose' or 'draw'.
Why is this? Basic logic says that unless there is a tie, a game will always have a winner. Your if statement lines up with this. Either $homescore > $awayscore or $homescore < $awayscore (or there is a tie). So in the event of a non-tie, one of the sides of your first if will evaluate to true, and therefore the entire condition will be true since || needs only one truthy. So unless there is a tie, $status will be set to 'win'.
Moving on, basic logic will also tell us that a game always has a loser unless there is a tie. Again, your code conforms to this. In the event of a non-tie, one of the score will be less than the other and therefore your second if will evaluate to true like the first did. Now $status = 'lose'.
Now if there is a tie, $status would equal 'draw', but otherwise $status will always be 'lose' because if the game has a winner, it also has a loser and $status is set to 'lose' after it is set to 'win'.
What you need here is a variable for the winner, loser, and for a draw. Like so:
$winner = '';
$loser = '';
$draw = false;
if($homescore > $awayscore) {
$winner = 'home';
$loser = 'away';
} else if($homescore < $awayscore) {
$winner = 'away';
$loser = 'home';
} else if($homescore === $awayscore) {
$draw = true;
}
(Codepad Demo)
This code also makes use of else if, because you don't need to evaluate the other conditions if you've determined who won and lost.
You need to group your conditions better.
if ( ($hometeam && ($homescore > $awayscore)) || ($awayteam && ($awayscore > $homescore)))
{
$status= 'win';
}
else if ( ($hometeam && ($homescore < $awayscore)) || ($awayteam && ($awayscore < $homescore)))
{
$status= 'lose';
}
else
{
$status= 'draw';
}
if ($homescore > $awayscore) {
$homestatus = 'win';
$awaystatus = 'lose';
} else if ($homescore < $awayscore) {
$homestatus = 'lose';
$awaystatus = 'win';
} else {
$homestatus = 'draw';
$awaystatus = 'draw';
}
<ul class="match group">
<li>
<ul class="team1 <?php echo $homestatus; ?>">
<li class="teamname"><h2><?php echo $homename; ?></h2></li>
<li class="teamscore"><?php echo $homescore; ?></li>
</ul>
</li>
<li>
<ul class="team2 <?php echo $awaystatus; ?>">
<li class="teamname"><h2><?php echo $awayname?></h2></li>
<li class="teamscore"><?php echo $awayscore?></li>
</ul>
</li>
<li><ul class="matchinfo">
<li><button>Get Report</button></li>
</ul>
</li>
</ul>
I'm so used to writing for Wordpress that I don't know how to do a simple PHP logic statement by itself!
For a very basic project, I'm trying to include an unordered list on the index.php page. Here's an example of code that I am trying to use:
<?php
$index = $_SERVER['REQUEST_URI'];
if ($index == "/") {
?><ul id="links">
<li>Facebook</li>
<li>Example</li>
<li>Test</li>
<li>Page</li>
</ul>
<?php } ?>
I know this needs to work for index pages ending with the actual URI of index.php as well as a blank suffix without index.php. How can I write this code to work correctly?
You need to use basename() function to get actual page name like this:
<?php
$page = basename($_SERVER['REQUEST_URI']);
if ($page == 'index.php' || $page == "/" || $page == '') {
?><ul id="links">
<li>Facebook</li>
<li>Example</li>
<li>Test</li>
<li>Page</li>
</ul>
<?php } ?>
It does work however if you use SCRIPT_NAME with the slashes following.
<?php
$page = basename($_SERVER['SCRIPT_NAME']);
if ($page == 'index.php' || $page == "/" || $page == '')
{ ?>
Hi This should show up.
<?php }?>