Php switch-case with functions doesn't work - php

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?

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 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

how make html cache from php script

i have this script on my site, and i need to cache it in HTML, and maybe set expire cache to 24hours. This is the script:
<?php
$dir='DDL/ANIME/';
$s = $_GET['s'];
$d = $_GET['d'];
$ref=$_SERVER['HTTP_REFERER'];
$file=$dir.$_GET["file"];
$file1=$_GET["file"];
$esiste = 0;
$desistenza= "error";
$domain0='www.example0.org';
$domain1='www.example1.org';
$domain2='www.example2.org';
$domain3='www.example3.org';
$domain4='www.example4.org';
$domain5='www.example5.org';
$domain6='www.example6.org';
$domain7='www.example7.org';
$domain8='www.example8.org';
$domain9='www.example19.org';
$domain10='www.example10.org';
$domain11='www.example11.org';
function checkFileOnDomain($file,$domain) {
$resourceUrl = "http://$domain/$file";
$ch = curl_init($resourceUrl);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.example0.org/');
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($statusCode == '200')
return true;
}
$domain_list=array("www.example1.org", "www.example2.org", "www.example3.org", "www.example4.org", "www.example5.org", "www.example6.org", "www.example7.org", "www.example8.org", "www.example9.org", "www.example10.org", "www.example11.org");
foreach ($domain_list as $domain) {
if (checkFileOnDomain($file,$domain)) { $esiste = 1; $desistenza=$domain;
} else {}
} unset($domain);
?>
<html>
<head>
<title>Download & Streaming </title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="playerhtml5.css">
<meta http-equiv="Content-Language" content="it" />
<link rel="stylesheet" type="text/css" href="0174/ds.css" />
<script type="text/javascript" src="0174/advertisement.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript"></script>
</head>
<body oncontextmenu="return false;">
<center>
<? if($esiste == 1 ){ ?>
<? if(empty($s)){ ?>
<?php
$d = $desistenza;
switch ($d) {
case $domain0: $d = "af"; break;
case $domain1: $d = "1"; break;
case $domain2: $d = "2"; break;
case $domain3: $d = "3"; break;
case $domain4: $d = "4"; break;
case $domain5: $d = "5"; break;
case $domain6: $d = "6"; break;
case $domain7: $d = "7"; break;
case $domain8: $d = "8"; break;
case $domain9: $d = "9"; break;
case $domain10: $d = "10"; break;
case $domain11: $d = "11"; break;
default: $s = "NO";
}
?>
<div id="wtf" class="button">
<a href="http://<?php echo $desistenza ?>/<?php echo $file ?>" target="_blank" >Download</a>
Streaming Alternative
</div>
<div id="wtf" class="html5video lightsoff" style="width:100%;">
<div id="wtf" class="main-container">
<div class="hero-unit">
<video id='video-player' preload='metadata' controls>
<source src="http://<?php echo $desistenza ?>/<?php echo $file ?>" type="video/mp4">
</video>
</div>
</div>
</div>
<? } ?>
<? if($s == 3){ ?>
<div id="wtf" class="button">
Download</p></b>
</div>
<? } ?>
<? } ?>
<? if($esiste == 0 ){ ?>
<b><p style="font-size:30px"><font color="#FF6600">File not found <br></font></b></p>
<? } ?>
</center>
</body>
</html>
the script is working fine and all is good, but my site is on shared hosting, so sometimes it causes high load to cpu, so i come up with the solution to cache this pages
You can save the content of the pages on your local files system. On the next request for the page, you check the timestamp of the saved page in your local file system. In case it's older than 24 hour, curl the page, otherwise serve the save file.

PHP Front Controller Flow Confusion - Switch statement only tested once

I am new to php and confused on the flow of this. It seems to me that switch($action) would need to be evaluated more than once when the $action variable gets a new value. But I don't see the switch($action) being evaluated but just once. I used admin_menu.php as an example. What if the user picks <p>Product Manager</p> from admin_menu.php? I don't see where index.php is called afterwards, so I don't see how the new value will be tested since switch($action) has already ran.
localhost ch21_ex2 # cat index.php
<?php
// Start session management and include necessary functions
session_start();
require_once('model/database.php');
require_once('model/admin_db.php');
// Get the action to perform
if (isset($_POST['action'])) {
$action = $_POST['action'];
} else if (isset($_GET['action'])) {
$action = $_GET['action'];
} else {
$action = 'show_admin_menu';
}
// If the user isn't logged in, force the user to login
if (!isset($_SESSION['is_valid_admin'])) {
$action = 'login';
}
// Perform the specified action
switch($action) {
case 'login':
$email = $_POST['email'];
$password = $_POST['password'];
if (is_valid_admin_login($email, $password)) {
$_SESSION['is_valid_admin'] = true;
include('view/admin_menu.php');
} else {
$login_message = 'You must login to view this page.';
include('view/login.php');
}
break;
case 'show_admin_menu':
include('view/admin_menu.php');
break;
case 'show_product_manager':
include('view/product_manager.php');
break;
case 'show_order_manager':
include('view/order_manager.php');
break;
case 'logout':
$_SESSION = array(); // Clear all session data from memory
session_destroy(); // Clean up the session ID
$login_message = 'You have been logged out.';
include('view/login.php');
break;
}
?>localhost ch21_ex2 # firefox 'view/admin_menu.php'
localhost ch21_ex2 # cat 'view/admin_menu.php'
<?php
require_once('util/secure_conn.php'); // require a secure connection
require_once('util/valid_admin.php'); // require a valid admin user
?>
<!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>
<title>My Guitar Shop</title>
<link rel="stylesheet" type="text/css" href="main.css"/>
</head>
<body>
<div id="page">
<div id="header">
<h1>My Guitar Shop</h1>
</div>
<div id="main">
<h1>Admin Menu</h1>
<p>Product Manager</p>
<p>Order Manager</p>
<p>Logout</p>
</div><!-- end main -->
</div><!-- end page -->
</body>
</html>
I just now see the answer....index.php is being called again with: index.php?action=show_order_manager in <p>Order Manager</p>

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