Rendering another layout inside index.php - php

I am following this Link to understand the basic framework of PHP Web app but I am not able to understand how can I switch back and forth to the different layouts (Articles, Portfolio) through navigation bar?

All different layout must be like index.php.
Image you have articles.php and portfolio.php. There content would be like:
Articles.php:
<?php
// load up your config file
require_once("/path/to/resources/config.php");
require_once(TEMPLATES_PATH . "/header.php");
?>
<div id="container">
<div id="content">
<!-- Your article content here!!! -->
</div>
<?php
require_once(TEMPLATES_PATH . "/rightPanel.php");
?>
</div>
<?php
require_once(TEMPLATES_PATH . "/footer.php");
?>
and portfolio.php:
<?php
// load up your config file
require_once("/path/to/resources/config.php");
require_once(TEMPLATES_PATH . "/header.php");
?>
<div id="container">
<div id="content">
<!-- Your portfolio content here!!! -->
</div>
<?php
require_once(TEMPLATES_PATH . "/rightPanel.php");
?>
</div>
<?php
require_once(TEMPLATES_PATH . "/footer.php");
?>
But everytime you add a new page, you must update your header.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Simple Site</title>
</head>
<body>
<div id="header">
<h1>Simple Site</h1>
<ul class="nav global">
<li>Home</li>
<li>Articles</li>
<li>Portfolio</li>
<li>Any other page</li>
</ul>
</div>

This is what actually I wanted to achieve
<nav>
<ul>
<li>Home</li>
<li>Projects</li>
<li>Calculator</li>
<li>Blog</li>
<li>About</li>
</ul>
</nav>
I am sending the page variable to the index and then it is rendering the layout accordingly through this way.
if (isset($_GET["page"])){
$page=$_GET["page"];
}
else {
$page = "home";
}
renderLayoutWithContentFile($page .".php", $variables);

Related

Wordpress: CSS file not working?

Okay. I've been trying to link the css stylesheets to my webpages. I've been scratching my head trying to figure out what I am doing wrong. Any ideas?
functions.php
<?php
function roofers_wp_resources() {
wp_enqueue_style('normalize', get_stylesheet_uri());
wp_enqueue_style('style', get_stylesheet_uri());
}
add_action('wp_enqueue_scripts', 'roofers_wp_resources');
?>
header.php
<!DOCTYPE html>
<html>
<head>
<title>R.J Roofers</title>
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<?php wp_head(); ?>
</head>
<body>
front-page.php
<?php get_header(); ?>
<div class="main-container">
<header class="main-header">
<!-- <h1 class="logo-name"><li>R.J Roofer</li></h1>
--> <h1 class="logo-name">R.J Roofer</h1>
<nav class="main-nav">
<li class="nav-item-1">home</li>
<li>services</li>
<li>gallery</li>
<li>about us</li>
<li>contact</li>
</nav>
</header>
<div class="quote-box-1">
<div class="quote-box">
<p class="quote-box-title">HOME ROOFING BRISBANE NO.1 FOR ROOF RESTORATIONS AND ROOF REPLACE</p>
<button class="quote-btn-1">BOOK A FREE QUOTE</button>
</div>
</div>
More Code..
get_stylesheet_uri() gives full url to your style.css file, so in your case it's good only if you want to enqueue the style.css file.
For the second file, you need to pass a full url including the name of the file.
Assuming your normalize file called normalize.css:
function roofers_wp_resources(){
wp_enqueue_style('my-theme-style', get_stylesheet_uri());
wp_enqueue_style('normalize', get_template_directory_uri() . '/normalize.css');
}
add_action('wp_enqueue_scripts', 'roofers_wp_resources');
Notice that if you working on a child theme, you need to change get_template_directory_uri() to get_stylesheet_directory_uri().

Redirecting HomePage

Iam Learning PHP, so i have started an website..in that i have created index.php and index_1.php and login.php in which when user successful login the user has to be redirect to index.php but iam not getting it so i have created an another index_1.php in this page only three navigation bars will be there Home,projects and contactus.
This is my index.php Page
<!DOCTYPE HTML>
<html>
<head>
<title>Karthik</title>
<meta name="description" content="website description" />
<meta name="keywords" content="website keywords, website keywords" />
<meta http-equiv="content-type" content="text/html; charset=windows-1252" />
<link href="style/style.css" rel="stylesheet" type="text/css">
<style>
.mySlides {display:none;}
</style>
</head>
<body>
<div id="main">
<div id="header">
<div id="logo">
<div id="logo_text">
<!-- class="logo_colour", allows you to change the colour of the text -->
<h1>KarthikAenugula</h1>
</div>
</div>
<div id="menubar">
<ul id="menu">
<!-- put class="selected" in the li tag for the selected page - to highlight which page you're on -->
<li class="selected">Home</li>
<li>Projects</li>
<li>AboutMe</li>
<li>Login</li>
<li>Register</li>
<li>Contact</li>
</ul>
</div>
</div>
This is my index_1.php
<?
session_start();
if(!isset($_SESSION['user_email']))
{
echo '<p>Please Login to continue Log In</p>';
exit();
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Karthik</title>
<meta name="description" content="website description" />
<meta name="keywords" content="website keywords, website keywords" />
<meta http-equiv="content-type" content="text/html; charset=windows-1252" />
<link href="style/style.css" rel="stylesheet" type="text/css">
<style>
.mySlides {display:none;}
</style>
</head>
<body>
<div id="main">
<div id="header">
<div id="logo">
<div id="logo_text">
<!-- class="logo_colour", allows you to change the colour of the text -->
<h1>KarthikAenugula</h1>
</div>
</div>
<div id="menubar">
<ul id="menu">
<!-- put class="selected" in the li tag for the selected page - to highlight which page you're on -->
<li class="selected">Home</li>
<li>Projects</li>
<li>AboutMe</li>
<li>Contact</li>
<?php
echo '<p align="right">';
session_start();
echo "Welcome";
echo '<br>';
echo ($_SESSION ['user_email']);
echo '<br> Logout?ClickHere</p>';
?>
This is my login.php
<?php
ob_start();
session_start();
if(isset($_POST["Submit"]))
{
$user_email=$_POST['user_email'];
$user_password=md5($_POST['user_password']);
$con=#mysql_connect('localhost','xxxxx','xxxx') or die(mysql_error());
mysql_select_db('suryapra_aenugula_karthik');
$query=mysql_query("SELECT * FROM user_registration where user_email='".$user_email."' AND user_password='".$user_password."'") or die("error in selection");
$numrows=mysql_num_rows($query);
if($numrows!=0)
{
while($row=mysql_fetch_assoc($query))
{
$dbusername=$row['user_email'];
$dbpassword=$row['user_password'];
}
if($user_email==$dbusername && $user_password==$dbpassword)
{
if(isset($_POST['remember']))
{
setcookie('user_email',$user_email,time()+60*60*7);
setcookie('user_password',$user_email,time()+60*60*7);
}
session_start();
$_SESSION['user_email']=$user_email;
header("Location: index_1.php");
ob_end_flush();
}
}
else
{
header("Location: login_2.php");
ob_end_flush();
}
}
else
{
header("Location: login.php");
}
?>
what my problem is if user logins he is redirecting to index.php and again login and register links are also getting in index.php
i want solution in which when user logins he should be redirect to index.php and login,register tabs should not be visible to him unless he presses logout button
The basic flow of User Authentication:
User submits login form to login.php this, if login is correct, will set a $_SESSION variable to signal user is logged in.
User is then redirected back to site, where
In the templates, anything that is optional for loggedin/logged out users are wrapped in IF blocks
Something like so:
<?php
$logged_in = $_SESSION['logged_in'];
?>
<nav>
<?php if (!$logged_in):?>
Login
<?php endif;?>
<?php if ($logged_in):?>
Logout
<?php endif;?>
</nav>
First of all after a successful login on the login page redirect user on the index.php page.
you dont need index_2.php page.
After that edit your index page with following code where first we will check if [user_email] session is been created or not when we logged in if it is created we are not echoing anything if not then we will echo the login and register link. bellow is your new index.php page
<?php session_start();
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Karthik</title>
<meta name="description" content="website description" />
<meta name="keywords" content="website keywords, website keywords" />
<meta http-equiv="content-type" content="text/html; charset=windows-1252" />
<link href="style/style.css" rel="stylesheet" type="text/css">
<style>
.mySlides {display:none;}
</style>
</head>
<body>
<div id="main">
<div id="header">
<div id="logo">
<div id="logo_text">
<!-- class="logo_colour", allows you to change the colour of the text -->
<h1>KarthikAenugula</h1>
</div>
</div>
<div id="menubar">
<ul id="menu">
<!-- put class="selected" in the li tag for the selected page - to highlight which page you're on -->
<li class="selected">Home</li>
<li>Projects</li>
<li>AboutMe</li>
<?php if(isset($_SESSION[user_email]))
{
}
else
{
echo "<li><a href='login.php'>Login</a></li>";
echo "<li><a href='register.php'>Register</a></li>";
}
?>
<li>Contact</li>
</ul>
</div>
</div>

Redirect button in php [duplicate]

This question already has answers here:
What does "href" stand for? [duplicate]
(5 answers)
Closed 7 years ago.
Hello guys I want to redirect a user to another page when clicking on a button on my webpage, but I dont know how to do it. My buttons are "home", "coffee", "shop" and "about us". When clicking on any of these, I want to get to another page. Can anyone help please?
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><?php echo $title; ?></title>
<link rel="stylesheet" type="text/css" href="Styles/stylesheet.css" />
</head>
<body>
<div id="wrapper">
<div id="banner">
</div>
<nav id="navigation">
<ul id="nav">
<li>Home</li>
<li>Coffee</li>
<li>Shop</li>
<li>About us</li>
</ul>
</nav>
<div id="content_area">
<?php echo $content; ?>
</div>
<div id="sidebar">
</div>
<footer>
<p>All rights reserved</p>
</footer>
</div>
</body>
</html>
and this is my index code:
<?php
$title = "Coffee Webpage";
$content = '<img src="https://encrypted-tbn2.gstatic.com/images? q=tbn:ANd9GcSmRPsVdzF4BYRWbF3yBpbkW-1ZjyURYWX1JQm0e6RAbJPR0H76" class="imgLeft" />
<h3>Title 1</h3>
<p>
</p>
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTdJv4yTpue170rTnOn9unKMgYWeOSPxBP9LoBwvUvhcDJNFcPG6w" class="imgRight"/>
<h3>Title 2</h3>
<p>
</p>
<img src="http://seattle.eat24hours.com/files/cuisines/v4/coffee.jpg?e24v=225" class="imgLeft" />
<h3>Title 3</h3>
<p>
</p>';
include 'Template.php';
?>
add the url to that page in to your link so for example if the page coffes is coffe.php then put coffe.php in there.
alternately put your full url which I would recommend.
you could also add this before your link to get the site url using php
<?php echo "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; ?>
Typically, you'd want to do this by adding a front controller; or, in your case, turning your index.php into a front controller.
<?php
$page = $_GET['page'];
switch($page) {
case 'coffee':
// set variables
break;
case 'another':
// set variables to something else
break;
}
require 'template.php';
Put a link to each page in their corrosponding a href attributes.
Example:
<li>Coffee</li>
<li>Shop</li>
<li>About us</li>

Where/How do I include the navigation bar in a yii view

I am new to PHP frameworks and yii and I am trying to understand how views/layouts work. This is how I currently understand how to layout a page template:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="page_wrp">
<?php include 'includes/top_right_nav.php' ?>
<div id="content_wrp">
<div id="content">
</div>
</div><!--#content_wrp-->
<?php include 'includes/main_nav.php' ?>
</div><!--#page_wrp-->
</body>
I understand that basic yii way is more like this:
<?php /* #var $this Controller */ ?>
<!DOCTYPE>
<html>
<head>
<title><?php echo CHtml::encode($this->pageTitle); ?></title>
</head>
<body>
<div class="container" id="page">
<div id="header">
<div id="logo"><?php echo CHtml::encode(Yii::app()->name); ?></div>
</div><!-- header -->
<div id="mainmenu">
<!--
DON'T WANT TO USE ZII.WIDGETS FOR MENU
WANT TO KNOW BEST WAY TO INCLUDE MY ONW NAVIAGTION MENU-
->
<?php $this->widget('zii.widgets.CMenu',array('...')); ?>
</div><!-- mainmenu -->
<?php echo $content; ?>
<div id="footer"></div><!-- footer -->
What I don't understand is what is the best practice/analogous way to include my own navigation bar in the context of yii/MVC. Can I still use includes as above or do I have to use zii.widgets? If it's ok to use includes where do I put the files?
Any help would be much appreciated.
You don't have to use Yii widgets however it's recommended to use them, they are quite flexible.
Make a view file for your menu markup in the layouts folder, something like _myMenu.php
_menu.php
<ul>
<li>menu item 1</li>
<li>menu item 2</li>
</ul>
then in your layout use renderPartial() to render your _menu.php partial view inside your layout.
main.php
<div id="mainmenu">
<?php $this->renderPartial("//layouts/_menu"); ?>
</div>
This is a really simple solution, if you want to make the menu more dynamic you can add a method to your controller to handle this.

Can I reference an external JavaScript file in a PHP file?

I cannot seem to get this to work. I have a small JS file that switches banners depending on the time of day, but it seems that doing an external reference in my PHP file does not work. It works fine in an HTML page.
This is the code in the JavaScript file.
function getStylesheet() {
var currentTime = new Date().getHours();
if (7 <= currentTime && currentTime < 19) {
document.write("<img src='images/banner_day.jpg'>");
} else {
document.write("<img src='images/banner_night.jpg'>");
}
}
getStylesheet();
And here is the reference code I used to call the JavaScript file. Its in a PHP file.
<script src="http://beta.website.com/wp-content/themes/theme/scripts/banner.js"></script>
Everything on the PHP page shows up in the browser, except for the banner that I tried to call with the script.
Here is the entire PHP file code.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<div id="main">
<div class="container">
<div id="header">
<div id="logo">
<img src="http://beta.dfdfdf.com/wp-content/themes/asdafd/images/logo.png" />
</div>
<div id="shadow2"></div>
<div id="shadow1"></div>
<ul id="menu">
<li>df</li>
<li>df</li>
<li>fd df df</li>
<li>The asfdssd asdfds</li>
<li>sf</li>
<li>df</li>
<li>dfd</li>
</ul>
<div id="slogan"><big>"fasfdsads2005."</big></div>
<div id="loginDiv">Login Panel Here</div>
</div>
<div id="banner">
<script src="http://beta.adsfasfasfd.com/wp-content/themes/adfadsf/scripts/banner.js"></script></div>
<div class="sidebar">
Sidebar Content<br />
<br /><br />
blah
blah
blah
</div>
<div class="content">
Main Content
<br />
<br />
<br />
<br />
<br />
<br />
</div>
<div id="footer">
<div class="container">
<div class="footer_column long">
<h3>Cadsfadsfafdfsd.com All Rights Reserved</h3>
<p>dsafasdfdffadffadsdafsdfsadafsdfsadfas</p>
</div>
<div class="footer_column2">
<h3>More Links</h3>
<ul>
<li><a href="http://aadfsdfsdfa.
com">asfsdfa</a></li>
<li><a href="http://ItsNotch.
com">ItsNotch</a></li>
<li><a href="http://adfasfsfaf.
com">adsfasf</a></li>
<li><a href="http://twitter.
com/safs">Twitter</a></li>
<li><a href="https://www.facebook.com/group.php?gid=10215yy20340498">Facebook Fan Page
</a></li>
</ul>
</div>
<div class="footer_column2">
<h3>RSS</h3>
<ul>
<li>RSS Feed</li>
<li>What is RSS?</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
Try changing your Javascript to this:
window.onload = function () {
// Uncomment this line to make sure the script is loading/running, then delete it
// alert('Hello, your Javascript is running');
// Declare variables
var currentTime, bannerDiv, newImg;
// Get currentTime
currentTime = new Date().getHours();
// Get 'banner' div
bannerDiv = document.getElementById('banner');
// Get create a new <img>
newImg = document.createElement('img');
// Assign a src="" attribute depending on the time
newImg.src = (currentTime > 7 && currentTime < 19) ? 'images/banner_day.jpg' : 'images/banner_night.jpg';
// add the new image to the document
bannerDiv.appendChild(newImg);
};
And put it in the <head> of the document, so change
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<!-- ....... -->
<div id="banner">
<script src="http://beta.adsfasfasfd.com/wp-content/themes/adfadsf/scripts/banner.js"></script>
</div>
<!-- ....... -->
...to:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="http://beta.adsfasfasfd.com/wp-content/themes/adfadsf/scripts/banner.js"></script>
</head>
<body>
<!-- ....... -->
<div id="banner"></div>
<!-- ....... -->
If it still doesn't work, one of two things is happening:
Your javascript file is not actually at http://beta.adsfasfasfd.com/wp-content/themes/adfadsf/scripts/banner.js so the script cannot be loaded/run. Uncomment the first line to confirm that the script is running.
Your banner images cannot be found at images/banner_night.jpg - make sure your relative paths are correct.
Have you made sure that you've actually got the <script></script> in your head? I certainly can't see it there (unless it is in <?php wp_head();>?).
Having attempted to navigate to http://beta.website.com/wp-content/themes/theme/scripts/banner.js it returns a 404, you'll need to fix this issue first.
Remember that javascript is client side, and all you want to do with php is spit out the tags somewhere for the browser to deal with.
It might be path problem.
What is you images (full) path?
What is you page (full) path?
As it is now it looks for images directory beneath your current page. Is this correct?

Categories