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.
Related
I just try to bulid control panel using MVC (codeigniter), I just need way to how make a side menu in home page and when I click in the link in menu the page open in the right side, and the menu stay in the left side.
I need coorect way to do this using MVC.
I use this simple way to make Views to make exactly what your asking.
<?php
//This is mainpage.php
$data['page_id'] = $_GET['page_id'];
?>
<!DOCTYPE HTML>
<html>
<head>
<title></title>
</head>
<body>
<div>
<!-- HERE Comes Side Bar -->
<?php $this->load->view('sidebar'); ?>
</div>
<div>
<!-- HERE Comes Main Content -->
<?php $this->load->view('maincontent',$data) ?>
</div>
</body>
</html>
Mainpage.php is a master page who holds code for both, side menu as well as the content page
This code if for side bar sidebar.php in view folder
<div>
<ul>
<li>Dashboard</li>
<li>Settings</li>
<li>Logout</li>
</ul>
</div>
sidebar.php page only holds different links, but all links have GET variable named "page_id" which decides which page is to be displayed in the content page.
Now, in mainpage.php you can notice that maincontent.php is loaded as view with passing $data which has page_id as variable, which is drived from sidebar. this will help to display the content in content side.
This code is for maincontent.php in view folder
<?php
if(file_exists(APPPATH.'views/'.$page_id.'.php')){
$this->load->view($page_id);
}else{
show_404();
}
this has always worked for me in non ajax page displays...so will work for you to.
Thanks...
I've created a modular web page in which each component is within it's own html/php file. Example, index.html calls up header.html, content.php, etc. The reason, so I can keep each section clean, and separate.
My header.php includes a navigational bar (which also uses CSS3 to provide drop down menu (ex, DEF):
<div id="nav">
<ul><li class="navlist"ABC</li>
<li class="navlist">DEF
<ul>
<li><li>GHI</li>
<li>JKL</li>
</ul></li>
<li class="navlist">MNO</li>
</ul>
My dilemma is that I want the 'li class' to equal 'nav_active_menu' if it is the current page being viewed.
I'm assuming that PHP can take care of this, but unsure as to how. Can anyone provide any examples, or links on how to do this?
Hopefully this makes sense.. words....
You can do that by making $activePage variable before you include the header.php page
in you abc.php file:
$activePage = "abc";
include('header.php');
and in your header.php file:
<li class="<?php if ($activePage == "abc") echo 'nav_active_menu'; ?>">ABC</li>
The same way for other pages but with a different value with $activePage variable.
This is another solution, using javascript with jquery.
Add a specific CSS class to each parent li:
<div id="nav">
<ul>
<li class="navlist abc">ABC</li>
<li class="navlist def">DEF
<ul>
<li>GHI</li>
<li>JKL</li>
</ul>
</li>
<li class="navlist mno">MNO</li>
</ul>
</div>
Then add jquery javascript to your <head> in the HTML.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
Add this to your CSS:
.nav_active_menu {your css for active_item goes here}
And in each HTML page add the relevant jquery selector to activate:
<script>
$(".def a").first().addClass('nav_active_menu');
</script>
See fiddle: http://jsfiddle.net/tmfncbb7/2/ (fiddle updated with the correct class)
In ASP.NET MVC applications can use dynamic pages as follows:
<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
<div id="menu">
<ul>
<li>Home</li>
<li>Contact</li>
<li>About</li>
</ul>
</div>
<div id="content">
#RenderBody()
</div>
</body>
</html>
with RenderBody () method that renders the content of a page in another.
How I can render the content pages on a main (index.php) page PHP Case 1, with the Codeigniter Framework and Case 2, normally without the framework?
Excuse my English, as it is not my native language!!
Regards,
Mauriciohz
In Normal PHP,
You can use any of include, require , include_once or require_once.
In Codeigniter,
You can load content part alone with use of below code:
$this->load->view("view_file_name");
here view_file_name is your view file which exist inside the applications/view folder.
You have to give without extension.
For ex:
your content file name is "content.php". applications/view/content.php
You can load as:
$this->load->view("content");// without ".php" extension.
You could use the include or require function in php.
Example
include('path');
require('path');
include_once('path');
require_once('path');
The _once would make sure that the file is never included multiple times throughout the script.
I wrote a menu in one menu.php and called it to another file. For reduction of code, I have only included what seem minimal/sufficient.
This is definition in style of main page.
#menu{position:absolute;top:100;left:260;height:40;width:490;}
This is where I call the menu.php
<div id="menu"><?php include("menu.php")?></div>
This is how I have defined menu.
<ul id="menubar">
<li><a href=#>HOME</a></li>
<li><a href=#>NEWS</a></li>
<li><a href=#>RESOURCES</a></li>
<li><a href=#>ABOUT US</a></li>
</ul>
This is the style of menu in menu.php
*{margin:0px;padding:0px;}
body{font-size:120%;font-family:arial;background-color:#D4F1FA;padding:50px;}
ul#menubar{list-style-type:none;}
ul#menubar li{width:120px;text-align:center;position:relative;float:left;}
ul#menubar a{color:#DAECF2;text-decoration:none;display:block; width:120px;height:40px;line-height:40px;background-color:#153945}
ul#menubar li:hover>a{background-color:rgba(20,66,82,0.6);color:#C9E6F0}
The problem is The font size and color is not preserved in main file where it works in menu.php only. Any suggestions?
When you include a php file that includes other files you have to write the full path of your css.
One more advice is to use tools like the chrome inspector to see if all your files loaded correctly.
so for example :
Don't do
<?php
<link rel="stylesheet" type="text/css" href="../css/style.css">
?>
instead do
<?php
<link rel="stylesheet" type="text/css" href="/website/css/style.css">
?>
This is an unorthodox way of adding common content into file. You could make it more flexible by adding the html into a function and calling it at will.
function displayNav()
{
echo "<ul id='menubar'>
<li><a href=#>HOME</a></li>
<li><a href=#>NEWS</a></li>
<li><a href=#>RESOURCES</a></li>
<li><a href=#>ABOUT US</a></li>
</ul>";
}
You can add more functions for other common content as required.
Furthermore I would suggest moving your css into an external stylesheet; it will make it easier maintain and improve the readability of the markup pages.
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.