I know very little about PHP, but recently split the menu portion on my static web pages to a header.php and this works. I now want to change the font color of active page. I saw an example here but cannot get the php code to work. Here is my current menu before any changes
<div id="menu">
<ul>
<li>Home</li>
<li>Lessons</li>
<li>Contact</li>
</ul>
</div> <!-- end menu div -->
I saw this code in StackOverflow (modified for my menu)
<?php # Using REQUEST_URI
$currentpage = $_SERVER['REQUEST_URI'];?>
<div class="nav">
<div class="tab
<?php
if(preg_match("/index/i", $currentpage)||($currentpage=="/"))
echo " currentpage";
?>">Home
</div>
<div class="tab
<?php
if(preg_match("/about/i", $currentpage))
echo " currentpage";
?>">Lessons
</div>
<div class="tab
<?php
if(preg_match("/contact/i", $currentpage))
echo " currentpage";
?>">Contact
</div>
</div> <!--nav-->
When I tried to substitute the php between the ul and the /ul, I get an error message:
line 2: Undefined Index: REQUEST_URI
I read about REQUEST_URI but do not understand why it is not working.
Any help would be appreciated. Thanks.
To check available $_SERVER variables you can do <?php print_r($_SERVER) ?>
I heard that on IIS server REQUEST_URI will not be set. IF that's the case you can do the following...
http://davidwalsh.name/iis-php-server-request_uri
I found another Q&A on this site similar to my question
How add class='active' to html menu with php
I used the example provided by Toader Mihai Claudiu, although the last author preferred the approach I was trying to use. Since I am a novice and have a simple site for my students I went with Toader's approach and it worked. Maybe someday I will try the suggestion provided by Marcin
Thank you contributors.
Related
I'm creating a theme for my php project. I want to decrease my code amount by creating a simple theme.html page and include that to every page.
Can you help me guys?
Example theme.html:
<html>
<body>
<h1>Welcome to panel</h1>
<ul>
<li><a class="home" href="#">Home</a></li>
<li><a class="about" href="#">About us</a></li>
<li><a class="contact" href="#">Contact us</a></li>
</ul>
<!-- content -->
<div class="content"><!-- other page codes will write here --></div>
<!-- content -->
</body>
<html>
And now i want to include this code to index.html and write form codes to content div? Help me...
You can do it using php
for that you have to save the files .php like index.html into index.php
then include the file like below
<?php include 'theme.php';?>
You can also use twig which is a great template manager for php (initially made for Symfony) and really easy to use.
I was wondering if someone can help me out with this. For my system I made i'm using .tpl files for viewer files. The problem underlays in using if else statements.
Like this would be an example of an tpl file:
<div class="menu">
<li>Home</li>
<!-- Checking if has admin rights -->
[IF='isadmin']
<li>Admin</li>
[ENDIF]
<!-- End of checking -->
</div>
But how can I implent this in PHP?
I already searched in stackoverflow, but only found answers as you just need to use PHP for this. This isn't the right way for me to use PHP. The system is already too complicated to change everything to PHP and using PHP as viewer doesn't pass the requirements.
Thanks in advance!
Your file extension must be not ".html". You need to change it to ".php"
Then you can use PHP in HTML code like :
<div class="menu">
<li>Home</li>
<!-- Checking if has admin rights -->
<?php if ($isAdmin) { ?> // $isAdmin is a boolean
<li>Admin</li>
<?php } ?>
<!-- End of checking -->
</div>
You can affect a boolean into $isAdmin as you want like $isAdmin = true;. If this var is true, <li>Admin</li> will display.
PS : This notation if ($isAdmin) it's the same thing as if ($isAdmin == true).
I have researched some answers that talk about php, javascript, iframes etc. but I have tried a couple and none of them work. I am new to HTML coding.. and coding in general!
<link rel="menu" href="menu.html"> does nothing
<!--#include virtual="/menu.html" --> does nothing (presumably because its a comment?)
<iframe src="page.html"></iframe>
or object... both place the menu in a silly little scroll box.
I want to run my menu on my page as if it were a function in C. Where I can just include it, and it be there, or just link it.
Thanks for the help!
Ryan
webpage file: biology.html
menu file: menu.html
<div class="container">
<img src="homeicon.jpg" width="50" alt="Home">
<div class="redhover">
<div class="dropdown">
<button class="dropbtn">GCSEs</button>
<div class="dropdown-content">
Chemistry
Biology
</div>
</div>
<div class="dropdown">
<button class="dropbtn">A-Levels</button>
<div class="dropdown-content">
Chemistry
Biology
</div>
</div>
<div class="dropdown">
<button class="dropbtn">University</button>
<div class="dropdown-content">
Telecommunications
Electronic Engineering
</div>
</div>
<div class="dropdown">
<button class="dropbtn">More</button>
<div class="dropdown-content">
About me
Youtube
</div>
</div>
</div>
</div>
You can use php to include files on other pages. Here is some example code to get you started:
<?php
require_once('menu.php');
?>
You can put this in your HTML page appropriately, however you must make sure that php can be processed on your server and the file containing php code must end in the .php extension.
There are also other methods of including files via php, see here:
http://php.net/manual/en/function.include.php
and
http://php.net/manual/en/function.require.php
Edit - I'm not a big fan of this approach, but it will work on Github pages.
Create a file called nav.js with your menu defined as a js variable, then use javascript to insert it into an empty div created on each page. This way to update your nav you only have to ever edit nav.js Not pretty but it works
nav.js
var navigation = "<nav>";
navigation += "<ul>";
navigation += "<li>Home</li>";
navigation += "<li>About</li>";
navigation += "</ul>";
navigation += "</nav>";
document.getElementById("navigation").innerHTML = navigation;
Other pages
<div id="navigation"></div>
<!--rest of page goes here.-->
<script src="nav.js"></script>
Fiddle: https://jsfiddle.net/ze3hLxx8/1/
There are multiple ways to include a file into another depending on the backend technology you wish / want / need to use.
PHP
The most common way to do it in php is by using the include or require statement inside a php file.
In your specific case your biology.html file must be converted to a biology.php file and then you can add the relative code to include the file:
<?php include('menu.php');?>
This simple statement will add the content in your menu.php file to the current page. This will not work if php is not present on the server and obviously will not work locally without a local development environment
The differences between require and include can be found on the official documentation:
include: http://php.net/manual/en/function.include.php
require: http://php.net/manual/en/function.require.php
SSI
Another method is to use Server Side Includes. To use the SSI it must be supported and enabled on the webserver. To use SSI you need to change the extension from biology.html to biology.shtml and then add the following statement:
<!--#include file="menu.html" -->
More information on server side includes can be found on wikipedia: https://en.wikipedia.org/wiki/Server_Side_Includes
I have looked for a while for a solution to this, but have been unsuccessful so far. Not sure if it's me, or if there's a problem with the pagination functionality.
What I'm trying to do
When on a page within a certain channel, I want to have 'prev' and 'next' buttons to click through to the next article in the channel. These links must use the page_uri set for each page.
What I've tried
I have a channel full of pages. In the template, I have added:
{exp:channel:entries channel="project" dynamic="no" orderby="date" sort="desc" limit="1" paginate="bottom"}
<div class="container-fluid project-section pagination-bar">
<div class="container">
<div class="col-md-12">
{paginate}
{pagination_links}
<ul>
{previous_page}
<li>Previous Page</li>
{/previous_page}
{next_page}
<li>Next Page</li>
{/next_page}
</ul>
{/pagination_links}
{/paginate}
</div>
</div>
</div>
{/exp:channel:entries}
This is adding the links to the bottom of the page, but when clicking on them the URLs are just loading the same URL you're currently on, then adding /P1 to the end.
I need it to use the page_uri added in the CMS so that it clicks through properly and doesn't affect SEO on the site.
Any help appreciated on this one, thanks
Pagination isn't really the way around this. You should look into Next/Previous Entry Linking. It'll give you a full link to the previous and next entry. Just make sure to keep it outside your channel entries loop.
For example you'd want something like:
{exp:channel:prev_entry channel="project"}
View previous project
{/exp:channel:prev_entry}
{exp:channel:next_entry channel="project"}
View next project
{/exp:channel:next_entry}
Assuming your projects all live in a section called projects you'd be linking between
/projects/example-project-1
/projects/example-project-2
/projects/example-project-3
I'm developing a web-based application using Framework7 (http://www.idangero.us/framework7/).
I'm doing well but now that I have come to integrate my login/member system I have encountered a problem with hyperlinks.
I've spent couple of hours trying to figure it out but have still come up short.
Basically I have three links:
<div class="pages navbar-through toolbar-through">
<div data-page="index" class="page">
<div class="page-content">
<div class="content-block">
<div class="content-block-inner">
Hello <?php echo htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8'); ?>, secret content!<br />
Memberlist<br />
Edit Account<br />
Logout
</div>
</div>
</div>
Memberlist.php and edit_account.php do not work but logout does.
Visiting the files directly in my browser (eg domain.com/memberlist.php) loads the file. They just do not load from this page so something is clearly wrong with it.
I've tried to include target="_self" and use other styles of linking content as explained in the Framework7 documentation but there is nothing about the problem I'm experiencing.
it important to write exact page name in data-page attribute value
<div class="page" data-page="about">
After digging around some more in the documentation I was able to solve my issue.
Linking in Framework7 requires every page linked to to have some specific code for parsing over AJAX.
<!-- That is all we have in about.html file -->
<div class="page" data-page="about">
... About page content goes here
</div>