How to load CSS in specific page in php - php

I want to have dynamic header.php in all of my website pages, and there are many CSS files for loading. is there any way to load different CSS files for each page ? for example:
<?php if($title == "title") { ?>
<link href="mycss.css" >
<script src="test.js"></script>
<?php } ?>

You can create a function (or class) to echo your header and feed parameters as settings. Something like this may work:
function.get_header.php
// This function will render the header
function get_header($settings = false)
{
ob_start();
include("header.php");
$data = ob_get_contents();
ob_end_clean();
return $data;
}
function.get_page_css.php
// This function will act like a pseudo database return
// and will return a series of css links based on input
function get_page_css($var = false)
{
$css['title'][] = "/css/style1.css";
$css['other'][] = "/css/style2.css";
$css['title'][] = "/css/style3.css";
if(!empty($css[$var]))
return $css[$var];
}
header.php
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<title><?php echo (!empty($settings['title']))? $settings['title'] : "Untitled Page"; ?></title>
<head>
<?php if(!empty($settings['css']) && is_array($settings['css'])) {
foreach($settings['css'] as $link) {
?>
<link type="text/css" rel="stylesheet" href="<?php echo $link; ?>" />
<?php }
}
?>
</head>
index.php
<?php
// Include the header function
include("function.get_header.php");
// Include the css return function
include("function.get_page_css.php");
// Write the header to browser using the get_page_css() function
echo get_header(array("title"=>"This Great Page!","css"=>get_page_css('title')));
?>
<body>...etc.

In a simple way, you can used this method which is given below...
<?PHP
$page_name= $_SERVER['PHP_SELF'];
if($page_name=='about.php'){
echo '<link href="about_css.css" type="text/css">
<script src="about_test.js"></script>';
}
if($page_name=='contact.php'){
echo '<link href="contact_css.css" type="text/css">
<script src="contact_test.js"></script>';
}
?>

first check this url path
<?php
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
?>
or
<?php
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$url1 = "http://example.com/home";
$url2 = "http://example.com/about";
if (strpos($url1,'home') !== false) { ?>
<link href="mycss.css" >
<?php
} else if(strpos($url2,'about') !== false){ ?>
<link href="mycss2.css" >
<?php }else { ?>
// your defult css file
<?php } ?>
I hope you understood

Put this index.php file in your css folder
<?
$title = $_GET['title'];
header('Content-Type: text/css');
switch ( $title ) {
case 'title_1':
include('style_1.css');
case 'title_2':
include('style_2.css');
default:
include('default.css');
}
exit();
?>
Then whenever you want to call your css :
<?
echo '<link href="path/to/css/folder/?title='.$variable.'" rel="stylesheet">'
?>
The index.php in the css folder will be controller for css needs to be included

For simplicity, you can assign a page code for every page. Create the script with that name. So you can import it like this:
In the page header:
$pageCode = "INDEX";
in the footer:
<?php
$scriptFileName = "your/path/to/file/".$pageCode.".js";
if(file_exists($scriptFileName)){
echo "<script src='$scriptFileName'></script>";
}
apply the same for CSS if needed. But in header!

This worked for me just fine, I added it to the _header.php file which is included on on all my pages:
<?php
$page_name = $_SERVER['PHP_SELF'];
if($page_name =='/recepiepg/admin/index.php'){
echo '<link rel="stylesheet" href="../css/admin.css" type="text/css"/>';
}
?>

Related

html php title not work

I want to set title of this welcome page, but my code does not work. Where is the problem? Thanks all.
index.php:
$page = "";
include('layout/meta.php');
if(isset($_GET['page']) && $_GET['page'] == 'welcome') {
$page = 'welcome';
include('layout/welcome.php');
}
layout/meta.php
<html>
<head>
<?php
if($page == 'welcome') { ?>
<title>Welcome</title>
<?php
}
if($page == 'home') { ?>
<title>Home</title>
<?php
}
?>
<link rel="stylesheet" href="/css/style.css"/>
</head>
<body>
Try with this, $page is assigning after this, so you need to either assign first or user $_GET['page'].
<?php
if(isset($_GET['page']) && $_GET['page'] == 'welcome') { ?>
<title>Welcome</title>
<?php
}
if(isset($_GET['page']) && $_GET['page'] == 'home') { ?>
<title>Home</title>
<?php
}
?>
Why do you pass pagename in the GET parameter where you can get it using PHP global variables, this is how you get the page filename.
$filename=basename($_SERVER['PHP_SELF']); // Returns the current PHP File name
Now use it as below.
<?php if($filename == 'welcome.php') { ?>
<title>Welcome</title>
<?php } else if($filename == 'home.php') { ?>
<title>Home</title>
<?php } ?>
This will simplify your code with less efforts.
Why don't you use a switch control structure ? it can spare you resources, also, you code isn't very readable to me, I prefer to use heredoc in situations like yours, i.e.:
<?php
switch($page){
case "home":
$page = "<title>Home</title>";
break;
case "welcome":
$page = "<title>Home</title>";
break;
}
echo <<< EOF
<html>
<head>
{$title}
<link rel="stylesheet" href="/css/style.css"/>
</head>
<body>
EOF;
You can use like this
Index.php
include('layout/meta.php');
if(isset($_GET['page']) && $_GET['page'] == 'welcome') {
$data['page'] = 'welcome';
include('layout/welcome.php',$data);
}
I hope it help you

php templating header and footer

For each page on the site I have a header and footer, so I decided to create a template for both then just include them on each page.
<?php include_once("header.php")?>
contents of the page...
<?php include_once("footer.php")?>
header.php contains:
<!DOCTYPE html>
<head>
<title>Items Page</title>
<link rel="stylesheet" href="assets/css/item.css">
<script src="assets/css/item.js"></script>
</head>
<body>
<header>
contains a navigation bar
</header>
footer.php contains:
<footer>...</footer>
</body>
</html>
The above codes are for the Items page however I also have Users page they both have the same footer and header markup however different linked CSS and JS files. How can change <link> and <script> inside the <head> tag on the header.php include file if the user navigates to the user page, for example: mysite.com/user/blabla.
The correct link for the Users page should be:
<link rel="stylesheet" href="assets/css/user.css">
<script src="assets/css/user.js"></script>
Use this format in all pages
<?php
$page_title = "title_here";
$cssfiles = array("path/to/css1.css", "path/to/css2.css", ...);
$jsfiles = array("path/to/js1.js", "path/to/js2.js", ...);
?>
<?php include_once("header.php")?>
contents of the page...
<?php include_once("footer.php")?>
And in header.php
<head>
<title><?php echo $page_title;?></title>
<?php
foreach($cssfiles as $css){
?>
<link rel="stylesheet" href="<?php echo $css;?>">
<?php
}
foreach($jsfiles as $js){
?>
<script src="<?php echo $js;?>"></script>
<?php
}
?>
</head>
Try this way
User Type Page
<?php
$pageType = 2; // USER
?>
<?php include_once("header.php")?>
contents of the page...
<?php include_once("footer.php")?>
in other pages
<?php
$pageType = 1; // OTHER
?>
<?php include_once("header.php")?>
contents of the page...
<?php include_once("footer.php")?>
and in header
<!DOCTYPE html>
<head>
<?php if ( $pageType == 1 ) { ?>
<title>Items Page</title>
<link rel="stylesheet" href="assets/css/item.css">
<script src="assets/css/item.js"></script>
<?php } else if ( $pageType == 2 ) { ?>
<title>User Page</title>
<link rel="stylesheet" href="assets/css/user.css">
<script src="assets/css/user.js"></script>
<?php } ?>
</head>
Split up your nav and header include.
header.php
<!DOCTYPE html>
<head>
<title>Items Page</title>
<link rel="stylesheet" href="assets/css/item.css">
<script src="assets/css/item.js"></script>
nav.php
</head>
<body>
<header>
contains a navigation bar
</header>
whatever.php
<?php include_once("header.php")?>
<link rel="stylesheet" href="assets/css/user.css">
<script src="assets/css/user.js"></script>
<?php include_once("nav.php")?>
session_start();
if($_SESSION['user'])
{
?>
<link rel="stylesheet" href="assets/css/item_user.css">
<script src="assets/css/item_user.js"></script>
<?php
}
else
{
?>
<link rel="stylesheet" href="assets/css/item.css">
<script src="assets/css/item.js"></script>
<?php
}
?>
Try to set every page a variable for css and javascript like so:
$css = 'assets/css/user.css';
$javascript = 'assets/css/user.js';
<?php include_once("header.php")?>
contents of the page...
<?php include_once("footer.php")?>
on the header page you need to call those variable:
<!DOCTYPE html>
<head>
<title>Items Page</title>
<link rel="stylesheet" href="<?php echo $css; ?>">
<script src="<?php echo $javascript; ?>"></script>
</head>
<body>
<header>
contains a navigation bar
</header>
You can do like this
header.php
if(!isset($custom_css)) {
echo '<link rel="stylesheet" href="assets/css/item.css">';
}
if(!isset($custom_js)) {
echo '<script src="assets/css/item.js"></script>';
}
And in your users files, give values to $custom_css and $custom_js before including header
user.php
$custom_css = '<link rel="stylesheet" href="assets/css/user.css">';
$custom_js = '<script src="assets/css/item.js"></script>';
<?php include_once("header.php")?>

PHP - different header text on homepage and interior pages

I need to make the pagetitle in the header of the homepage different from that of the interior pages. It seems like a simple php fix but it is over my head. This seemed to be the solution but implementing it gave an error code : PHP if URL equals this then perform action
This is the code I have:
<?php
/**
* The Header for our theme.
*/
?><!DOCTYPE html>
<html>
<head>
<link href='http://fonts.googleapis.com/css?family=GFS+Didot'
rel='stylesheet' type='text/css'>
<title><?php wp_title('|', true, 'right'); ?></title>
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<div>
<header class="pagetitle">
<h1>
<a href="<?php echo esc_url(home_url('/')); ?>">
<?php bloginfo('name'); ?>
</a>
</h1>
<nav>
<?php wp_nav_menu(array('theme_location' => 'primary')); ?>
</nav>
</header>
Can someone tell me the if/else statement that will give me one on the homepage and a different on all other pages?
Try this:
<title>
<?php
if( is_home() ) {
echo 'Homepage Title';
}
else {
echo 'Other Pages Title';
}
?>
</title>
Reference: http://codex.wordpress.org/Function_Reference/is_home
Here's what I use on my own site to display just the site title on the homepage, but the article title on subpages:
<title>
<?php wp_title(''); ?><?php if(wp_title('', false)) { echo ' |'; } ?> <?php bloginfo('name'); ?>
</title>
Reference: http://codex.wordpress.org/Function_Reference/wp_title
if( is_home() || is_front_page() ) {
echo 'HOMEPAGE';
}
else {
echo 'INTERIOR PAGES';
}
The most recent edit on the question you referred to shows the correct if/else statement:
$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if($host == 'your.homepage.com/url/etc') {
echo "Homepage header text";
}
else {
echo "Other header text";
}
A wordpress-specific solution for checking if the user is on your home page is the function is_home() which returns a boolean if the main blog page is being displayed. More details about the is_home() function found here. There are also other "conditional tags" in wordpress that come with their own functions. A list of those can be found here.
So for example, you could also rewrite the above PHP code block with the is_home() function:
if (is_home()) {
echo "Homepage header text";
}
else {
echo "Other header text";
}

Having problems with function defined in other file

My page won't load at all, browser says its redirecting in a way that is not loading. As the title suggests I think the issue is with one of the php files I include with require_once(). Let me just show you:
thumbnail.php:
<?php
if(!function_exists("makethumbnail"))
{
//die("right before function definition");
function makethumbnail($src,$new_name,$new_width,$new_height)
{
die("inside makethumbnail");
$si = imagecreatefromjpeg($src);
$w = imagesx($si);
$h = imagesy($si);
$vi = imagecreatetruecolor($new_width,$new_height);
imagecopyresampled($vi,$si,0,0,0,0,$new_width,$new_height,$w,$h);
imagejpeg($vi,$new_name);
}
}
?>
checksession.php:
<?php
session_start();
$session_name = "forces";
$com=0;
if(!function_exists("logout"))
{
function logout()
{
$_SESSION = array();
session_destroy();
header('Location:http://cs4.sunyocc.edu/~j.d.dancks/index.php');
}
}
if(!isset($_SESSION['time']) || !isset($_SESSION['nick']))
{
$com=2;
logout();
}
else if($_SESSION['time'] < time())
{
$com=3;
logout();
}
//redirect back to main whatever ignore this line
?>
index.php:
<?php
require_once("shopsite/thumbnail.php");
require_once("shopsite/checksession.php");
die("made it past the require_once");
$con = mysql_connect('localhost','jddancks','csc255');
mysql_select_db('test',$con);
$q = mysql_query("select prod_name,image_name,type1,type2 from Product",$con) or die("its the mysql");
$row = mysql_fetch_assoc($q);
$totalRows_Recordset1 = mysql_num_rows($q);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Welcome to the shopsite</title>
<script type="text/javascript" src="shopsite/lib/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="shopsite/lib/jquery.jcarousel.min.js"></script>
<link rel="stylesheet" type="text/css" href="shopsite/skins/ie7/skin.css" />
<script type="text/javascript">
JQuery(document).ready(function() {
JQuery('#mycarousel').jcarousel({
itemLoadCallback:itemLoadCallbackFunction,
start:1,
auto:3,
animation:"slow",
wrap:"both"
});
});
</script>
</head>
<body>
<?php
session_start();
if(isset($_SESSION['nick']))
{
echo "<p>Welcome, ".$_SESSION['nick']."</p>";
}
die("Made it past the first php");
?>
<h1>Welcome to the one-stop shop for your every need!</h1>
<div class="jcarousel-ie7">
<p>Browse Items:</p>
<div class="jcarousel-container">
<div class="jcarousel-clip">
<ul id="mycarousel" class="jcarousel-skin-ie7">
<?php
$cnt=1;
do
{
$i=$row['image_name'];
$name=preg_split("/.jpg/",$i);
$name = "shopsite/thumb/".$name[0]."-index.jpg";
if(!file_exists($name))
{
makethumbnail("shopsite/images/".$row['image_name'],$name,50,50);
}
echo " <li class=\"jcarousel-item-".$cnt."\"><img src=\"".$name."\" /></li>\n";
$cnt=$cnt+1;
if($cnt>12) die("cnt larger than 12");
}
while($row = mysql_fetch_assoc($q));
?>
</ul>
</div>
<div disabled="disabled" class="jcarousel-prev jcarousel-prev-disabled"></div>
<div class="jcarousel-next"></div>
</div>
</div>
</body>
</html>
<?php mysql_free_result($row);
mysql_close($con); ?>
I want to insert images into a jquery carousel so a visitor can browse items they may want to purchase. I've never used jcarousel so I don't know if what I have works, I'm pretty sure thats not the problem. I guess its just one of those things you need a second pair of eyes for. The die statements in thumbnail.php make me believe that is the culprit, but it won't make it to the first line of the function, which is really confusing. I don't know how the php preporcessor works, other than its client-side.

creating a master template in CodeIgniter

I am stuck with a very basic problem. Ook the problem is this that I want a master template in which I can call the header, body and footer. I am unable to send title and css in header and also how can I send multiple css files. I am doing something like this:
This is the code in controller
$data['title'] = 'Login To WePOS';
$data['css'] = base_url().'style/login-box.css';
$this->load->view('templates/default',$data);
This is the code in header
<head>
<title><?php echo $title ?> - CodeIgniter 2 Tutorial</title>
<link href=" <?php echo $css;?>" rel="stylesheet" type="text/css" />
</head>
This is the code in template name as default
<html>
<?php
$this->load->view('templates/header', $data);
?>
<body>
<?php
$this->load->view('login/index', $data);
?>
</body>
<?php
$this->load->view('templates/footer', $data);
?>
</html>
hi there is different method to use template in codeigniter .
1- you can use this procedure
In controller
$data['main_content'] = 'login_view';
$data['title'] = 'Login To WePOS';
$data['css'] = 'login-box.css';
$this->load->view('templates/default',$data);
In template.php View
$this->load->view('header_view');
$this->load->view($main_content);
$this->load->view('footer_view');
in your main content variable you can pass the view file
if you want to add multiple css or multiple js files you can use MY_MARK idea as
$data['cssFiles'] = array(
'login-box.css',
'other.css'
);
and in your header file
if(is_array($cssFiles)){
foreach($cssFiles as $cssFile) {
<link href="<?php echo base_url() . 'style/' . $css; ?>" rel="stylesheet" type="text/css" />
}
}
Hope it helps.
You don't have to pass $data again in your default template.
<html>
<?php $this->load->view('templates/header'); ?>
<body>
<?php $this->load->view('login/index'); ?>
</body>
<?php $this->load->view('templates/footer'); ?>
</html>
This should allow you to pick up the $title and $css variables in your header as you have got currently.
With regards to sending multiple css files, create an array of files, like:
$data['cssFiles'] = array(
'login-box.css',
'other.css'
);
And modify the code in your header to be:
foreach($cssFiles as $cssFile) {
<link href="<?php echo base_url() . 'style/' . $css; ?>" rel="stylesheet" type="text/css" />
}
Hope that helps...

Categories