html php title not work - php

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

Related

Simple PHP IF/ELSE code not working properly with html markup [duplicate]

This question already has answers here:
The 3 different equals
(5 answers)
Closed 4 years ago.
I have started to learn some basic PHP. So far everything worked perfectly until I tried to run some IF/ELSE statements embedded into the markup as you can see in the snipet bellow:
<?php
$msg = 0;
echo $msg;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<?php if ($msg = 5){ ?>
<h1>5</h1>
<?php } elseif ($msg = 0) { ?>
<h1> 0 </h1>
<?php } else { ?>
<h1>Whatever</h1>
<?php } ?>
</body>
</html>
The result is always the first if statement, even if it doesn't meet the condition. In this case the result is:
0
5
Tried everything I knew and searched a lot without any results. What am I doing wrong here?
This is a simple syntax error, it needs to be:
<?php
$msg = 0;
echo $msg;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<?php if ($msg == 5){ ?>
<h1>5</h1>
<?php } elseif ($msg == 0) { ?>
<h1> 0 </h1>
<?php } else { ?>
<h1>Whatever</h1>
<?php } ?>
</body>
</html>
Notice the == which compares things vs. = which sets things.
The = sign is for assignment; in order to do a comparison you need to use ==:
<?php if ($msg == 5){ ?>
The problem is in the condition -> ($msg = 5)
It should be ==
($msg == 5) instead of ($msg = 5).
When you are comparing two values you need to use double equal sign not single.
Single means you are assigning value to the variable.
Don'g give up!!
Good luck!
<?php
$msg = 0;
echo $msg;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<?php if ($msg == 5){ ?>
<h1>5</h1>
<?php } elseif ($msg == 0) { ?>
<h1> 0 </h1>
<?php } else { ?>
<h1>Whatever</h1>
<?php } ?>
</body>
</html>

HTML doesn't load after PHP opening tag

Right now I'm coding this really simple user page but getting one problem that's bugging me and can't find out why it's happening.
When ever I would put <?php HTML will just stop loading from there.
<?php
session_start();
if (!isset($_SESSION["user"])) {
$logged_in = "no";
} else {
$username = $_SESSION["user"];
$logged_in = "yes";
}
include ('global.php');
?>
<head>
<title><?php echo $sitename; ?></title>
<link rel="stylesheet" type="text/css" href="css.css">
<link href="https://fonts.googleapis.com/css?family=Raleway" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div class="header">
<div class="header_content">
<div class="middle_logo">
<p><?php echo $sitename; ?></p>
</div>
</div>
</div>
<div class="under_header">
<?php
$id = htmlentites($_GET["id"]);
$get_user_data = mysqli_query ($dconnect, "SELECT * FROM user_data WHERE id='$id'");
if (mysqli_num_rows($get_user_data) < 2) {
header ("Location: index.php");
} else {
while ($row = mysqli_fetch_array($get_user_data)) {
echo $row["username"];
}
}
?>
<h1><?php echo $users_name; ?></h1>
</div>
When I look at the source code it ends at <div class="under_header">
The answer has already been found in the comment :
The line :
$id = htmlentites($_GET["id"]);
Contain an error and should be :
$id = htmlentities($_GET["id"]);
Thanks to #RiggsFolly and #JustOnUnderMillions.
You can now accept an answer and close the question.

How to load CSS in specific page in 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"/>';
}
?>

Php switch-case with functions doesn't work

Why does the first logic work
<!DOCTYPE html>
<html>
<head>
<title>Installation Script</title>
</head>
<?php
$step = (isset($_GET['step']) && $_GET['step'] != '') ? $_GET['step'] : '';
switch($step){
case '1':
step_1();
break;
case '2':
step_2();
break;
case '3':
step_3();
break;
default:
step_1();
}
?>
<body>
<?php
function step_1(){
some php code for the below form
?>
form (html code)
<?php
}
function step_2(){
some php code for the below form
?>
form (html code)
<?php
}
function step_3(){
some php code for the below form
?>
form (html code)
<?php
}
?></body>
</html>
but the second doesn't?
<?php
$result='';
$step = (isset($_GET['step']) && $_GET['step'] != '') ? $_GET['step'] : '';
switch($step){
case '1':
step_1();
break;
case '2':
step_2();
break;
case '3':
step_3();
break;
default:
step_1();
}
function step_1(){
$result='';
some php code for the $result form in this function
$result.='html form';
}
function step_2(){
$result='';
some php code for the $result form in this function
$result.='html form';
}
function step_3(){
$result='';
some php code for the $result form in this function
$result.='html form';
}
?><!DOCTYPE html>
<html>
<head>
<meta charset="utf8" />
<title>Install</title>
</head>
<body>
<div id="wrapper">
<header id="header">
<h1>Installer</h1>
</header>
<section>
<?php print $result; ?>
</section>
<footer id="footer">
</footer>
</div>
</body>
</html>
It's ok,that i shouldn't use an installer like this, i won't, but i only have this code to show you my problem. In my opinion,the second example's structure is more logical than the first one, but it doesn't work. Can you help me? Or where to find some information about this?

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.

Categories