this is my second question here, first one was resolved quite quickly and I appreciate any help I get. Here's the thing:
I have a folder views in which I have a folder named _global in which I have my beforeContent.php and afterContent.php those are my header and footer of actual Web pages. The main content in my pages is set in other view folders and I have no problem there.
I have this script in my beforeContent.php (this one shows my navbar):
<?php if (Session::exists('user_id')): ?>
<?php include 'app/views/_global/menu-session.php'; ?>
<?php else: ?>
<?php include 'app/views/_global/menu-no-session.php'; ?>
<?php endif; ?>
basically, if a user is logged in, it will show menu-session.php, if there's no user logged in, it will show menu-no-session.php.
my menu-session has this in it:
<ul>
<li><a <?php if ($FoundRoute['Controller'] == 'Main') echo
'class="active";'?> href="<?php echo Configuration::BASE_URL; ?>">Home</a>
</li>
<li><a <?php if ($FoundRoute['Controller'] == 'Overview') echo
'class="active";'?> href="<?php echo Configuration::BASE_URL; ?
>overview">Overview</a></li>
</ul>
it has more tabs, but you get the point.
I had to use this function that chooses the controller that will decide if the page is active or not. basically: If i'm on a page Overview, the script asks if the active Controller is named 'Overview' and if it is, it will select the tab as class="active".
However, this is wrong: my teacher said I can't have scripts in my view files (it's not wrong, it's just bad practice) and I need another method of doing this:
So, I created a method class Misc.php with a function Misc::url:
public static function url($link, $text){
echo '<a href="' . Configuration::BASE_URL . $link . '">' . $text .
'</a>';
}
this way my menu-session can just be:
<ul>
<li>Misc::url('', 'Home')</li>
<li>Misc::url('overview', 'Overview')</li>
</ul>
Now: I need a correct function in Misc.php where I have the >>> $FoundRoute['Controller'] <<< if statement implanted, something like:
Misc::urlWithActive($link, $text){
if ($foundRoute['Controller] = *thiscontroller*) {
echo '<a href="' . Configuration::BASE_URL . $link . '">' . $text .
'</a>';
}
I actually don't know how to write that.
I Really hope you guys understand what I need to do here and help me.
Related
I am trying to create a body class name based on a specific page in php. What I need to do is first define a variable and then check if the variable exists, and then if it exists display that one, otherwise if it is something else, then do something else, or if nothing then do not show any.
<body<?php if (defined('PAGE_KEY') && var == "homepage") echo " class=\"homepage\"";
elseif (defined('PAGE_KEY') && var == "page3") echo " class=\"page3\"";
elseif (defined('PAGE_KEY') && var == "page12") echo " class=\"page12\""; ?>>
This code will be located in my head.php.
My thought was to first check if variable was defined and if so then check what the variable was defined as and then based on that variable it will display a respective class.
The goal is for example on the page12 for the body tag to look like this:
<body class="page12">
But for example for the body tag on page55 (which I don't want to show a class for) to look like this:
<body>
In doing so I am able to now define css specifically for a page within the header where the body tag happens to be located.
Problem is first I don't know how to define the variable in the page, and second I don't know exactly how to write the php code above properly.
Attempt, for example on page12 I would have this code:
<?php PAGE_KEY = "page12" ?>
This code would be for example located in page12.php.
Also keep in mind, that the variable will come AFTER the body tag.
I also thought of trying to see what the page URL is but I think that's just making things too complicated.
Based on #Jordi's suggestion, how about this:
<body class="<?php echo PAGE_KEY ?>">
on head.php.
And then on page12.php, this:
<?php PAGE_KEY = "page12" ?>
and for example on page5.php this:
<?php PAGE_KEY = "page5" ?>
so that on those respective pages the body tag shows this:
on page5.php:
<body class="page5">
and on page12.php the body tag will show this:
<body class="page12">
Is this right?
#Jose made this suggestion, is this correct?
for example, on page12.php, to define the variable as 'page12', this:
<?php define("PAGE_KEY", "page12"); ?>
Is that what you were suggesting to do?
Ok! This problem is solved. I just needed to add in the code in the individual pages before the head.php include so I could define it. Thanks for your help! :)
You can use and define a constant like this :
<?php
define( "PAGE_KEY","homepage" );
?>
.
.
.
<body<?php echo " class=\"" . constant( "PAGE_KEY" ) . "\""; ?>>
For another page :
<?php
define( "PAGE_KEY","page5" );
?>
.
.
.
<body<?php echo " class=\"" . constant( "PAGE_KEY" ) . "\""; ?>>
You only change the constant, the rest is the same for every page.
Edit #1:
<body
<?php
define( "PAGE_KEY","homepage" );
echo " class=\"" . constant( "PAGE_KEY" ) . "\"";
?>
>
Edit #2:
<?php
define( "PAGE_KEY","homepage" );
?>
.
.
.
<?php
include( "head.php" >
?>
Now, head.php is something like this :
echo "<body class=\"" . constant( "PAGE_KEY" ) . "\">";
Start with something like this.
<?php
//each page and its class. Many pages can share the same class. If a page doesn't
//have a class, don't include it.
$classes = [
'homepage'=>'home_class',
'page1'=>'base_class',
'page2'=>'home_class',
'page3'=>'special_class'
];
//Adjust this from one page to the next
$this_page = 'homepage';
//get the class corresponding to current page, or '' if no class
$this_class = isset($classes[$this_page])? $classes[$this_page] : '';
?>
//insert the class for this page.
<body class="<?=$this_class ?>">
You can improve on it by moving the $classes array to another file (eg a config file) and including it in all your pages. This way you don't have to rewrite the array on every page (a bad idea because difficult to make a change, easy to make a mistake)
I have some code where i need to insert a class called active within the link tag. But for some really weird reason it wont work even though the values match and it really should only make the beef menu item blue and not the others. Screenshot attached.
if($menuitems->title==$menutitle) {
$activemenu='active';
}
echo '<a href="#menu_'.$cid.'" class="list-group-item list-group-item-success '.$activemenu.'" data-toggle="collapse" data-parent="#MainMenu" >'.$menuitems->title.' / '.$menutitle.' / '.$menuitems->title.'</a>';
$menu->title does only equal beef but yet its inseting teh active classs intoall the other top level menus.
Thanks for your help :)
Jonny
Resetting $activemenu variable will fix it before if statement or inside foreach.
$activemenu = '';
<?php
$activemenu = '';
if($menuitems->title==$menutitle)
{
$activemenu='active';
}
?>
<a href="#menu_<?php echo $cid ;?>" class="list-group-item list-group-item-success <?php echo $activemenu; ?>" data-toggle="collapse" data-parent="#MainMenu" ><?php echo $menuitems->title . ' / ' . $menutitle .' / '. $menuitems->title; ?></a>
I'd suggest you to go with this. Honestly, I myself got stuck in these type of situations before. It's always better to have <a> tags outside php.
This is my first question.
I'm building a simple dynamic menu using <li>
I'm working on a PHP based CMS (Kirby)
Kirby has predefined PHP functions (helpers)
I'm trying to output a <li> for each page on the website:
<li class='active'><a href='page1'></a></li>
<li><a href='page2'></a></li>
<li><a href='page3'></a></li>
...
Im using a PHP function e($condition, $value) to style the menu item only if that page isOpen()
// I need help here
<?php
foreach ($pages->visible() as $p):
echo "<li" . e($p->isOpen(), ' class="active"') . "><a href='" . $p->url() . "'></a></li>";
endforeach;
?>
The function is working but the css part class="active" is printing OUTSIDE the <li> on the final code
class="active"
<li>...</li>
<li>...</li>
<li>...</li>
I had this previous code that worked fine, but since i'm using display: inline-block the menu had spaces betwes each block, since the following code was placing each <li>in a new line.
// This code works
<?php foreach($pages->visible() as $p): ?>
<li <?php e($p->isOpen(), ' class="active"') ?> ></li>
<?php endforeach ?>
The reason i'm rewrinting the code is to remove the white space between the inline: blockelements.
I'm failing to concatenate the string in a way that the function works and print its results inside the <li>tag.
I've searched here and also have read lots of Docs in php.net but nothing worked to me, I'm struggling with this for 2 days.
I'm expecting to learn better how and when to use concatenation and string operators.
The problem is that Kirby's e() function already has the echo in the routine, instead of just simply returning the value.
http://getkirby.com/docs/cheatsheet/helpers/e
If you were to change your output loop to something more like this, echoing separately, you'll get the results in the desired order:
foreach($pages->visible() as $p)
{
echo "<li";
e($p->isOpen(), ' class="active"');
echo "> and the rest of your line </li>";
}
That said, perhaps using e() isn't the most elegant in this case. Maybe try the r() function instead:
http://getkirby.com/docs/cheatsheet/helpers/r
foreach($pages->visible() as $p)
{
echo "<li ".r($p->isOpen(), ' class="active"').">more text</li>";
}
hi i need help with a small bit of code for my wordpress theme.
<a href="<?php echo $link; ?>">
<?php
global $post;
foreach(get_the_tags($post->ID) as $tag) {
echo ' <li>'.$tag->name.', </li>';
}
?>
</a>
the code
<?php echo $link; ?>
this is for a link of a website that is already on the page the link part works great the only issue is i want the links (tags that are being used as keywords/anchor text)to be seo friendly for outbound links
what changes are needed? feel free to spice the link up for seo :)
If you're wanting the same link for each of the tags, then this is a solution:
<ul>
<?php
global $post;
foreach( get_the_tags($post->ID) as $tag ) {
echo '<li>' . $tag->name . '</li>';
}
?>
</ul>
Please note: I've added the unordered list start and end elements as these were missing, these could also be ordered list start and ends.
EDIT:
to reflect your code changes.
The code still looks the same...I would assume it should look something like this: (and I could be completely wrong) :)
<?php
global $post;
foreach(get_the_tags($post->ID) as $tag) {
echo ' <li>'.$tag->name.'</li>';
}
?>
Hi
I am going to highlight a menu item according to the page that is reading currently, when user click on different page through the menu, that menu item will be highlighted, example is http://templates.joomlart.com/ja_pyrite/index.php?option=com_content&view=article&id=44&Itemid=53.
If I use PHP/jQuery to check the url and highlight the menu, it will be good if the url look like "http://example.com/contact", but the example above is bad.
If I don't going to check the url and highlight the menu item, could someone give me a idea/method that can be done with the same effect?
Thank you
I found this on 960 Development, been googling for a while for this, so happy when I finally found it!
<ul class="sub-nav" >
<?php
$full_name = $_SERVER['PHP_SELF'];
$name_array = explode('/',$full_name);
$count = count($name_array);
$page_name = $name_array[$count-1];
?>
<li><a class="<?php echo ($page_name=='where-to-buy.php')?'active':'';?>" href="where-to-buy.php">WHERE TO BUY</a></li>
<li><a class="<?php echo ($page_name=='about.php')?'active':'';?>" href="about.php">ABOUT US</a></li>
<li><a class="<?php echo ($page_name=='contact.php')?'active':'';?>" href="contact.php">CONTACT US</a></li>
It is working fully for me, get the pages I need to be "active" to be active and dosent active any of thoes who got the class when I'm in another page!
Take a look at it!
Edit:
Even if you got a (in this example) contact.php?person=John, it will "active" the contact.php link!
do something like this
<div id="nav_menu">
<?php
$currentFile = $_SERVER['REQUEST_URI'];
$pages = array(
array("file" => "/index.php", "title" => "Home"),
array("file" => "/about.php", "title" => "About Us"),
array("file" => "/schedule.php", "title" => "Schedule")
);
$menuOutput = "<ul>";
foreach ($pages as $page) {
$activeAppend = ($page["file"] == $currentFile) ? " id='active' " : "class='nav_button'";
$currentAppend = ($page["file"] == $currentFile) ? " id='current' " : "class='nav_button'";
$menuOutput .= "<li " . $currentAppend . ">"
. "<a href='" . $page["file"] . "' id='".$page["id"]."'>" . $page["title"] ."</a>"
. "</li>";
}
$menuOutput .= "</ul>";
echo $menuOutput;
?>
</div>
i hope you get the idea, i had this on stackoverflow a while ago but i forgot what was the question
edit:
here i finnally found the original question
In the HTML code you use to generate your navigation, add some PHP logic that will add a selected class to the button of the page that you are currently on. Then just add some CSS for the selected class.
Can you do something like this? It should select any link that points at the current page - so you can apply whatever you like to highlight it.
$('a[href="'+window.location+'"]').addClass('menu-highlight');
A good technique is to add a specific class or id attribute to body element and then style it in CSS. It requires minimum of server side programming and keeps all the presentation logic in CSS as it should be done.
<style>
.contact #contact { background:#000; }
...
</style>
<body class="contact">
<ul>
<li id="homepage">Homepage</li>
...
<li id="contact">Contact</li>
</ul>
...