I have a situation whereby i want two of my pages to look different form the rest but they are not front pages. If they were it would be easy as the code below would do the trick.
$menu = & JSite::getMenu();
if ($menu->getActive() == $menu->getDefault()) {
echo 'This is the front page';
esle(do something else)
}
?>
In short i want a similar approach but this time, to get the menu by ID/URL. Any ideas?
I got the answer to this...all you need to check is the menu ID then put the code below.
<?php
//This is the code for the page with menu ID 6
$menuID = JSite::getMenu()->getActive()->id ;
if ($menuID == '6')
{
echo '';
}
elseif ($menuID == '2') //This is the HTML for page with menu ID 2
{
echo '';
}
elseif ($menuID == '4') //THis is the html for page with menu id 4
{
echo '';
}
else //This is the HTML code for the rest of the pages
{
echo '';
}
?>
Page ItemId can be obtained using $itemid = JRequest::getInt( 'Itemid' );
Related
I am currently working on a school project and appreciate your help.
HTML Code:
LINK 1
LINK 2
LINK 3
Now as I am relatively new to PHP, I need clue wehere to start or which topics are relevant for my solution. I have already searched the Forum here and with google, but unforutenly, didn't find anything, that fits my needs.
What I need, is a PHP-Code, that contains 6 href Links and every Link has a priority. So every time somebody clicks on "LINK 1" on the html file, he gets directed to the PHP-File "change-1.php", where one of the six href Links get activated.
PHP CODE "change-1.php":
<?php
if (condition) {
echo "http://www.someotherwebsite-1.com";
}
elseif (condition) {
echo "http://www.someotherwebsite-2.com";
}
elseif (condition) {
echo "http://www.someotherwebsite-3.com";
}
elseif (condition) {
echo "http://www.someotherwebsite-4.com";
}
elseif (condition) {
echo "http://www.someotherwebsite-5.com";
}
elseif (condition) {
echo "http://www.someotherwebsite-6.com";
}
?>
Perhaps this should help
LINK 1
LINK 2
LINK 3
Just create one change.php file
//to get change
<?php
if ($_GET['change'] == 1) {
echo "http://www.someotherwebsite-1.com";
}
elseif ($_GET['change'] == 2) {
echo "http://www.someotherwebsite-2.com";
}
elseif ($_GET['change'] == 3) {
echo "http://www.someotherwebsite-3.com";
}
elseif ($_GET['change'] == 4) {
echo "http://www.someotherwebsite-4.com";
}
elseif ($_GET['change'] == 5) {
echo "http://www.someotherwebsite-5.com";
}
elseif ($_GET['change'] == 6) {
echo "http://www.someotherwebsite-6.com";
}
?>
Now this is the basic idea. You can manipulate the data however you want.
I hope you got the concept.
There are two ways of writing php snippets to place html on the home page and then html on all other pages... This is for Joomla 3.++
<?php
$menu = & JSite::getMenu();
if ($menu->getActive() == $menu->getDefault()) {
echo 'HTML for HOME PAGE HERE';
} else {
echo 'HTML for ALL OTHER PAGES HERE';
} ?>
This code allows for just focusing on the home page only:
<?php
$menu = & JSite::getMenu();
if ($menu->getActive() == $menu->getDefault()) {
echo 'HTML FOR HOME PAGE HERE';
}
?>
What I would like to do, specifically like the second code snippet, is focus on specific pages on the site....
The reason being is A. I don't want to create a template for every other page type I am creating... And B. I don't want to have to place template elements of styling in my html module positions. I feel the custom HTML or blog / article posts should simply be for content insertion.
So is there a way to instead of calling on the $menu->getDefault()) <<<< make that getPage???
Use JInput to get the page information from the request.
Found the answer. You can do a direct page edit and ifelse edits for multiple pages.
<?php
//This is the code for the page with menu ID 102
$menuID = JSite::getMenu()->getActive()->id ;
if ($menuID == '102')
{
echo '<strong>long</strong>';
}
?>
Here is the code for multiple pages.
<?php
//This is the code for the page with menu ID 6
$menuID = JSite::getMenu()->getActive()->id ;
if ($menuID == '6')
{
echo '';
}
elseif ($menuID == '2') //This is the HTML for page with menu ID 2
{
echo '';
}
elseif ($menuID == '4') //THis is the html for page with menu id 4
{
echo '';
}
else //This is the HTML code for the rest of the pages
{
echo '';
}
?>
Found the answer here.
Joomla if else for menu by ID or url
I've created a navigation menu by PHP.
1) I need help how can i change link class for current page. I mean for example when HOME PAGE is open link should be like class="bla bla CURRENT"
2) Is there any suggest to better way for the LINK to that buttons.
HERE IS THE CODES
<?php
require_once('../config.php');
$sql= "SELECT * FROM veri_kategori";
foreach ($dbh->query($sql) as $row)
{
echo "<li class=\"dropdown\">".$row["tr"]."</li>";
}
?>
Try this solution:
<?
require_once('../config.php');
// Get the current page.
$pag = $_GET['pag'];
if (isset($pag)) {
if ($page == 'about') {
// Redirect or include your page.
} else if ($page == 'contact') {
// Redirect or include your page.
}
} else {
// Redirect or include your home page.
}
$sql = "SELECT * FROM veri_kategori";
foreach ($dbh->query($sql) as $row) {
// Set default class.
$class = "dropdown-toggle";
// If home page, set another class.
if (!isset($pag)) {
$class = "bla bla CURRENT";
}
echo "<li class=\"dropdown\">{$row["tr"]}</li>";
}
?>
I am building a navigation system on a webpage using PHP, that basically queries data from MySQL database and display as ul, li menu on main menu. But I couldn't figure out how do I make navigation menu selected when user goes to that page. I have one idea but that doesn't make home menu selected by default when user enters that page.
I have also a CSS class written to give that class to the anchor tag if it is that page.
here is my PHP code:
//Query Functions
function queryMenu() {
$query = "SELECT * FROM menu_en";
return $query;
}
// Render Functions
function renderNav() {
$menus = queryMenu();
$queryMenu = mysql_query($menus);
while( $navigation = mysql_fetch_array($queryMenu) ){
if( $navigation['id'] == $_GET['pageId'] ){
echo '<li>
<a href="index.php?pageId="'.$navigation['id'].' class="selected" >'.$navigation['menu_title'].
'</a></li>';
}else{
echo '<li><a href="index.php?pageId="'.$navigation['id'].'>'.$navigation['menu_title'].'</a></li>';
}
}
}
Suggest me a better way to do this, as I want to enhance my skill in PHP. And also please let me know if there is any wrong approach in above code.
If the home page is when $_GET['pageId'] is not set you could check using isset()
if(isset($_GET['pageId'])) {
$page = $_GET['pageId']
} else {
$page = $homePageId; // the ID of the home page.
}
Then you would replace this line:
if( $navigation['id'] == $_GET['pageId'] ){
with this line:
if( $navigation['id'] == $page ){
I think this would work, if i understand you correctly.
So prior to being introduced to CakePHP, I'd highlight the appropriate navigation tab according to the url with the following (rather sloppy) code I wrote (fyi absolute_url was a function I wrote to get the absolute path) :
$page = $_SERVER['PHP_SELF'];
// add all possible states for the navigation to an array
$checkNav = array(
"index" => "index",
"new" => "new",
"random" => "random",
"submit" => "submit"
);
$compareAgainst = strpos($page, $checkNav['index']);
if ($compareAgainst == 0) {
echo "<li><span class=\"navBorder\">Popular</span></li>\n";
} else {
echo "<li>Popular</li>\n";
}
$compareAgainst = strpos($page, $checkNav['new']);
if ($compareAgainst == 0) {
echo "<li><span class=\"navBorder\">New</span></li>\n";
} else {
echo "<li>New</li>\n";
}
$compareAgainst = strpos($page, $checkNav['random']);
if ($compareAgainst == 0) {
echo "<li><span class=\"navBorder\">Random</span></li>\n";
} else {
echo "<li>Random</li>\n";
}
$compareAgainst = strpos($page, $checkNav['submit']);
if ($compareAgainst == 0) {
echo "<li><span class=\"navBorder\">+ Submit a Link</span></li>\n";
} else {
echo "<li>+ Submit a Link</li>\n";
}
Now, I've noticed that in Cake, to determine the relative path, I can just go:
<?= $this->here; ?>
Is there a better way to do this, or should I just implement this (new) method with the old code?
You can do the following
Add this to app_helper.php if you need it in multiple pages. You feed this function with the controller and the action you want to check you want to compare against. The function compares it with the current page and return true if they match.
function isActive($controller, $actions = array())
{
foreach ($actions as $action)
{
if ($controller == $this->params['controller'] && $action == $this->params['action'])
{
return true;
}
}
return false;
}
And then generate your links like so:
<ul class="left">
<li <?php if($html->isActive('controller_name', array('index'))) { echo 'class="active"'; } ?>><?php echo $html->link('Index', '/index'); ?></li>
<li <?php if($html->isActive('controller_name', array('new'))) { echo 'class="active"'; } ?>><?php echo $html->link('New', '/new'); ?></li>
<li <?php if($html->isActive('controller_name', array('random'))) { echo 'class="active"'; } ?>><?php echo $html->link('Random', '/random'); ?></li>
<li <?php if($html->isActive('controller_name', array('submit'))) { echo 'class="active"'; } ?>><?php echo $html->link('Submit', '/submit'); ?></li>
</ul>
If the function returns true, the link will have class="active". Adapt it to your needs.
The way I've always done this is to give your body tag an id, and use css to target it. If your views are all separate then you can hard code the body id. If you are using some sort of template that adds in the header, content, footer etc., then just pass the id as a variable to the header view or wherever the body tag is (really any outer container/div that will be on every view and contain your navigation tabs). Also you will need to give your navigation tab id's to target each one.
Then just some css like this:
#homepage a#hometab,
#aboutpage a#abouttab,
#productpage a#productstab,
#contactpage a#contacttab
{
special active styling here
}