I would like to ask if it's possible to have a switch statement inside a php file where it is in mpdf.
this is my switch statement
switch($cts){
case 1:
echo "Not at all";
break;
Case 2:
echo "Ok";
break;
default;
echo "none";
}
but this could not be done in a mpdf.
Help.
You could store the respective messages in a variable and print it out outside the switch.
$msg = '';
switch($cts){
case 1:
$msg = "Not at all";
break;
case 2:
$msg = "Ok";
break;
default: $msg = "none";
}
echo $msg;
Related
I created a simple sample php file for display some function outputs. here is the code..
<?php
// $printName = hello('samitha');
// $printHeader = pageHeader('main','on');
switch (key($_GET)){
case 'red':
$printHeader = pageHeader('red','on');
$printName = hello('Joel');
break;
case 'blue':
$printHeader = pageHeader('blue','off');
$printName = hello('Duck');
break;
case 'yellow':
//$printHeader = pageHeader('yellow','on');
break;
}
function hello($name){
return $name;
}
function pageHeader($header,$onoff){
if ($onoff == 'on') {
return $header."page header<br>";
}
else {return null;}
}
echo $printHeader;
echo $printName;
?>
This code is working fine without any problems.
When I call 'example.com/php/tipo34.php?red', it shows on the screen:
redpage header
Joel
And when I call 'example.com/php/tipo34.php?blue' it shows on the screen:
Duck
I tried to put the below functions inside of another php file called tipo34-req.php and received the following error:
Fatal error: Call to undefined function pageHeader() in C:\wamp\www\php\tipo34.php on line 8
The code I tried:
<?php
// $printName = hello('samitha');
// $printHeader = pageHeader('main','on');
switch (key($_GET)){
case 'red':
$printHeader = pageHeader('red','on');
$printName = hello('samitha');
break;
case 'blue':
$printHeader = pageHeader('blue','off');
$printName = hello('kalum');
break;
case 'yellow':
//$printHeader = pageHeader('yellow','on');
break;
}
include 'tipo34-req.php';
echo $printHeader;
echo $printName;
?>
tipo34-req.php code:
<?php
function hello($name){
global $name;
return $name;
}
function pageHeader($header,$onoff){ global $header, $onoff
if ($onoff == 'on') {
return $header."page header<br>";
}
else {return null;}
}
?>
How do I solve this problem? Using the function directly on the file works, but when I put the functions in another php file, it throws the error.
Thanks.
Include your file above its contents usage. PHP is unaware of the functions since they are included later in the code.
include 'tipo34-req.php';
switch (key($_GET)){
case 'red':
$printHeader = pageHeader('red','on');
$printName = hello('samitha');
break;
case 'blue':
$printHeader = pageHeader('blue','off');
$printName = hello('kalum');
break;
case 'yellow':
//$printHeader = pageHeader('yellow','on');
break;
}
echo $printHeader;
echo $printName;
?>
Have you tried including the file at the top before calling any of the functions?
So I have this commands handling:
$message = the entered message.
public function handleCommands($message, $username)
{
//Variables we're going to use
$space = strpos($message, ' '); # The first space.
$command = trim(substr($message, 1, $space)); # The command after the slash
$name = substr($message, $space + 1); # The name after the command.
switch ($command)
{
case 'ban':
$this->ban($name, $username);
break;
case 'prune':
$this->prune($username);
break;
case '':
echo 'Please use a command!';
break;
case 'test':
try
{
$this->test($name);
}
catch (exception $r)
{
echo $r->getMessage();
}
break;
}
}
This basically will check for the command.
$command = the entered word after the slash ( " / " ).
Can you see
case '':
This basically checks if there is no command after the slash.
Question: I want the system to check aswell, if the command exists in the cases.
For example:
user wrote:
/hello
But that command doesn't exists, considering we only have case 'ban', case 'prune', case 'test' and case ''.
there is no case 'hello', so it will throw an error.
Is there a function that does this sort of thing? How can I do this?
I believe what you're looking for is a default: case.
Example:
<?php
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
default:
echo "i is not equal to 0, 1 or 2";
}
?>
EDIT:
Fixed version of problem that was chatted about: http://privatepaste.com/bd34e7e63b
Use the case default:
switch ($command)
{
case 'ban':
$this->ban($name, $username);
break;
case 'prune':
$this->prune($username);
break;
case '':
echo 'Please use a command!';
break;
case 'test':
try
{
$this->test($name);
}
catch (exception $r)
{
echo $r->getMessage();
}
break;
default:
echo "That command does not exist.";
}
I'm try to send a value to a PHP file, but when I check, this value became null.
I send the value by: user_login.php?p_action=New_User
The code of user_login.php is:
require("include/session_inc.php");
require("include/user_handling_inc.php");
require("include/db_inc.php");
start_Session(false, false);
switch ($p_action) {
case 'Login': {
$l_flag = verify_User($p_in_username, $p_in_password);
if ($l_flag == "Not_Found") {
$l_flag = "New_User";
}
break;
}
case 'Save_Profile': {
$l_flag = "Save_Profile";
break;
}
case 'New_User':
$l_flag = "New_User";
break;
case 'Create_New_User':
$l_flag = "Create_New_User";
}
switch ($l_flag) {
case 'New_User': {
include "include/user_new_inc.php";
break;
}
case 'Save_Profile': {
load_User_Data(" username = '$p_in_username' ", false);
include "include/user_profile_save_inc.php";
break;
}
case 'Wrong_Password':
echo "Wrong Pass";
break;
case 'OK':
load_User_Data(" username = '$p_in_username' ", true);
store_User_Cookie($g_userdata->user_id);
include "include/user_profile_inc.php";
break;
case 'Create_New_User':
$l_user_id = create_New_User ($p_in_username, $p_in_email, 'Y');
if ($l_user_id != -1) {
store_User_Cookie($l_user_id);
echo "Success !! <br><br> \n";
echo "<a href\"/index.php\"> Back to Main </a>";
}
break;
}
First your code isn't correct please read more about using Switch here
second to access to any variable came from url you can use Global variable $_GET or $_REQUEST
and you can read more about them from here and here
and this is your code after fixing it please try to run it
<?php
require("include/session_inc.php");
require("include/user_handling_inc.php");
require("include/db_inc.php");
start_Session(false, false);
$p_action=$_GET["p_action"];
switch ($p_action) {
case 'Login':
$l_flag = verify_User($p_in_username, $p_in_password);
if ($l_flag == "Not_Found") {
$l_flag = "New_User";
}
break;
case 'Save_Profile':
$l_flag = "Save_Profile";
break;
case 'New_User':
$l_flag = "New_User";
break;
case 'Create_New_User':
$l_flag = "Create_New_User";
break;
}
switch ($l_flag) {
case 'New_User':
include "include/user_new_inc.php";
break;
case 'Save_Profile':
load_User_Data(" username = '$p_in_username' ", false);
include "include/user_profile_save_inc.php";
break;
case 'Wrong_Password':
echo "Wrong Pass";
break;
case 'OK':
load_User_Data(" username = '$p_in_username' ", true);
store_User_Cookie($g_userdata->user_id);
include "include/user_profile_inc.php";
break;
case 'Create_New_User':
$l_user_id = create_New_User ($p_in_username, $p_in_email, 'Y');
if ($l_user_id != -1) {
store_User_Cookie($l_user_id);
echo "Success !! <br><br> \n";
echo "<a href\"/index.php\"> Back to Main </a>";
}
break;
}
?>
you need to make the code like this friend
switch ($_GET["p_action"]) {
case 'Login': {
$l_flag = verify_User($p_in_username, $p_in_password);
if ($l_flag == "Not_Found") {
$l_flag = "New_User";
}
that well give you the value of the get!!!
Use $_GET to get your parameter.
Sometimes $_REQUEST is preferable since it access both get & post data.
2nd thing never trust the user input so you must use addslashes(); or real_escape_string() function to prevent attacks on the system.
So Code would be like this :
$var = addslashes($_GET['p_action']);
switch($p) {
case 'Login':
$l_flag = verify_User($p_in_username, $p_in_password);
if ($l_flag == "Not_Found") {
$l_flag = "New_User";
}
break;
"OTHER CASES HERE"
}
Notice that : Don't add { } for CASE. Read syntax for switch
here.
<html>
<head>
<title> GRADE DETAILS </title>
</head>
<body>
<?php
$myname=$_GET ['myname'];
$mygrade=$_GET ['mygrade'];
if(($myname=='#' or $myname=='!'))
{
echo "Special character not allowed.";
}
else if($myname==" ")
{
echo "Please enter your name";
}
else
{
switch($mygrade)
{
case "";
echo "you did not enter grade";
break;
case A;
echo "Increment 30% of basic salary";
break;
case B;
echo "Increment 10% of basic salary";
break;
default;
echo"Wrong Grade";
break;
}
}
?>
</body>
</html>
i want to set multiple conditions at my if else statement..i try many way, but still didn't work...i hope someone can help me ..nothing to do with my Switch Case, but the problem only at IF ELSE..PLS HELP.Thanks.
It's basically OK, but you need to replace the semicolons after the "CASE" statements with colons, and second put the text strings (A, B etc.) into quotes:
switch($mygrade) {
case "":
echo "you did not enter grade";
break;
case "A":
echo "Increment 30% of basic salary";
break;
case "B":
echo "Increment 10% of basic salary";
break;
default:
echo "Wrong Grade";
break;
}
You where missing some quotes, A and B are interpreted as constants
<?php
$myname= trim($_GET ['myname']);
$mygrade= trim($_GET ['mygrade']);
if(strstr('#',$myname) or strstr('!',$myname) )
{
echo "Special character not allowed.";
}
else if(empty($myname))
{
echo "Please enter your name";
}
else
{
switch($mygrade)
{
case "":
echo "you did not enter grade";
break;
case 'A':
echo "Increment 30% of basic salary";
break;
case 'B':
echo "Increment 10% of basic salary";
break;
default:
echo"Wrong Grade";
break;
}
}
Is this a plain PHP? I have my answers below. But, I am not recommending that you continue with this. You can search for other class or framework that can help you on the validation. This might be prone on injection as well.
To answer your question: (Basically)
$invalid = array("#", "!");
if (in_array($myname, $invalid)) {
echo 'Special character not allowed.';
}
if (empty($myname)) {
echo "Please enter your name";
}
if (!empty($mygrade)) {
switch($mygrade) {
case "":
echo "you did not enter grade";
break;
case "A":
echo "Increment 30% of basic salary";
break;
case "B":
echo "Increment 10% of basic salary";
break;
default:
echo "Wrong Grade";
break;
}
}
else {
echo 'Grade was empty';
}
I am creating a report with the data which calls the stored procedure and that procedure returns
various sections (1,2,3,4,5,6) with the data in each section.Now the sections may contain or may
not contain the data.This is how i have wriiten my logic
foreach($this->$dbresults as $row){
$var1 ='';
If($var1!=$row['section']){
switch($row['section']){
case '1':echo "some thing data";
break;
case '2':echo "some thing data";
break;
case '3':echo "some thing data";
break;
case '4':echo "some thing data";
break;
case '5':echo "some thing data";
break;
case '6':echo "some thing data";
break;
}
}
$var1=$row['section']
}
So here My problem if any one of the section is not present then that section case cannot be executed
.I mean How do i execute the section even if the section is not returned from the database
I guess you're already ordering your results by section. If your sections are really 1-n, you could put your switch() code into some runsections function and do this:
$var1=0; $lastsection=16;
foreach($this->dbresults as $row) {
If($var1!=$row['section']){
for($num=$var1+1; $num<$row['section']; $num++) runsections($num);
runsections($row['section']);
}
$var1=$row['section'];
}
for($num=$var1+1;$num<=$lastsection;$num++) runsections($num);
if your sections aren't sequential numbers you could create an array and check if they've all been executed
$sections=array('a'=>0,'b'=>0,'c'=>0,'d'=>0,'e'=>0);
If($var1!=$row['section']){
unset($sections[$row['section']]);
runsection($row['section']);
}
...
}
foreach($sections as $num) {
runsection($num);
}
edit: so the runsections() function would look like this:
function runsections($section) {
switch($section){
case '1':echo "some thing data";
break;
case '2':echo "some thing data";
break;
case '3':echo "some thing data";
break;
case '4':echo "some thing data";
break;
case '5':echo "some thing data";
break;
case '6':echo "some thing data";
break;
}
}
switch($x){
case '1':
echo "some thing 1";
break;
case '2':
echo "some thing 2";
break;
case 'N':
echo "some thing N";
break;
default:
echo "some thing else";
}
After your last case, insert:
default: echo "some error";
The break is optional since it's the last case in the switch statement. Also, The single quotes are also optional if you're looking for numeric options.
case 1: echo "something";
break;
Maybe I am not quite understanding exactly what you want. The following code should work in the following conditions:
You always want to display the 6
sections with either DB data or
non-DB data.
You do not need to display the same
section multiple times.
$sections = range(1, 6);
foreach($sections as $sectionNum) {
$sectionNum = (string) $sectionNum;
$foundSection = false;
foreach($this->dbresults as $row) {
if ($row['section'] == $sectionNum) {
echo "section #$sectionNum has DB data: " . $row['data'];
$foundSection = true;
break;
}
}
if (!$foundSection) {
echo "section #$sectionNum does not have DB data.";
}
}
Here's what I've got:
foreach($this->dbresults as $row){
if(isset($row['section'])){
switch($row['section']){
case '1':echo "some thing data";
break;
case '2':echo "some thing data";
break;
case '3':echo "some thing data";
break;
case '4':echo "some thing data";
break;
case '5':echo "some thing data";
break;
case '6':echo "some thing data";
break;
default:echo "some thing data";
break;
}
} else {
//Do something since no section data was stored
}
}
I added a default case, fixed the small php errors ($this->$dbresults is changed, using isset instead of !='') and added an else to your if to do something if the section isn't found.