Switch inside a switch loop ... what is wrong with my code? - php

Ok guys, i´m trying to switch inside a case but it does not work it allways falls back to the default. What am i missing?
When i try /index.php?siteSettings it displays the default, same thing when i try index.php?siteSettings&mode=adminSettings
switch($_SERVER['QUERY_STRING']){
case 'logout':
include("logout.php");
break;
case 'siteSettings':
switch($_GET['mode']){
case 'generalSettings':
$mainPageTitle = "General settings";
$caseFile = "".$admin_templates_path."/generalSettings.php";
break;
case 'adminSettings':
$mainPageTitle = "Admin settings";
$caseFile = "".$admin_templates_path."/adminSettings.php";
break;
case 'userSettings':
$mainPageTitle = "User settings";
$caseFile = "".$admin_templates_path."/userSettings.php";
break;
case 'advertising':
$mainPageTitle = "Advertising";
$caseFile = "".$admin_templates_path."/advertising.php";
break;
default:
$mainPageTitle = "General settings";
$caseFile = "".$admin_templates_path."/generalSettings.php";
break;
}
break;
default:
$mainPageTitle = "General settings";
$caseFile = "".$admin_templates_path."/generalSettings.php";
break;
}

After #aynber made me aware of my mistake this is what i did and it does work now.
Maybe someone will need it.
Thnx guys.
$switchPage = explode("~",str_replace(["?","&","="], "~",$_SERVER['QUERY_STRING']));
switch($switchPage[0]){
.....

Related

PHP . $_GET and switch case to navigate url from one page to another

I am working on a php base online forum Sincerely speaking i bought the script from codecanyon am still a newbie in php the index page contain $_GET nd switch case which will help in navigating to the other pages but its working keep showing page not find. I have tried all i can pls i need your help thanx.....
`<?php
include("includes/db_config.php");
include("includes/google_config.php");
include("includes/functions.php");
include("includes/loaders.php");
//get web settings
$web = mysql_fetch_array(mysql_query("SELECT * FROM settings ORDER BY id
DESC LIMIT 1"));
//update user online time
if($_SESSION['usern']) {
$user_id = userinfo($_SESSION['usern'],"id");
$online_time = time();
$update = mysql_query("UPDATE users SET online_time='$online_time' WHERE
id='$user_id'");
}
//update forum visits
update_visits();
load_header();
$page = protect($_GET['page']);
}
switch($page) {
case "set_password": include("pages/set_password.php"); break;
case "chat_content": include("pages/chat_content.php"); break;
case "chat": include("pages/chat.php"); break;
case "tag": include("pages/tag.php"); break;
case "forum_sign_in": include("pages/sign_in.php"); break;
case "forum_sign_up": include("pages/sign_up.php"); break;
case "forum_lostpassword": include("pages/lostpassword.php"); break;
case "forum_profile": include("pages/profile.php"); break;
case "forum_messages": include("pages/messages.php"); break;
case "forum_online_users": include("pages/online_users.php"); break;
case "forum_adpanel": include("pages/adpanel.php"); break;
case "view_forum": include("pages/view_forum.php"); break;
case "view_thread": include("pages/view_thread.php"); break;
case "post_thread": include("pages/post_thread.php"); break;
case "post_replie": include("pages/post_replie.php"); break;
case "post_edit": include("pages/post_edit.php"); break;
case "post_delete": include("pages/post_delete.php"); break;
case "post_quote": include("pages/post_quote.php"); break;
case "post_report": include("pages/post_report.php"); break;
case "userinfo": include("pages/userinfo.php"); break;
case "search": include("pages/search.php"); break;
case "read_message": include("pages/read_message.php"); break;
case "send_message": include("pages/send_message.php"); break;
case "reply_message": include("pages/reply_message.php"); break;
case "delete_message": include("pages/delete_message.php"); break;
case "panel": include("pages/panel.php"); break;
case "adpanel_func": include("pages/adpanel_func.php"); break;
case "forum_logout":
unset($_SESSION['usern']);
session_destroy();
session_unset();
$redir = $web['forum_url']."sign_in/";
header("Location: $redir");
break;
default: include("pages/home.php");
}
load_footer();
?>
First you have to debug whats inside the $page variable with:
var_dump($page);
and look whats is the value when you click that link.
NOTE:
I see a brace "`" before the php tag opener, delete it

issues with friendly urls using htaccess and php

i have a problem, but may be easier if i put my code. I have this:
<?php
$view = "";
if(isset($_REQUEST["view"]) != "" && $_REQUEST["view"]) {
$view = $_REQUEST["view"];
} else {
$view = "";
}
#View Handler
$factory = new \API\Factory();
$factory->Template('header');
switch($view) {
#Home
case '':
$factory->View("home/index");
break;
case 'about-us':
$factory->View("home/about-us");
break;
case 'contact':
$factory->View("home/contact");
break;
#Products
case 'products':
$factory->View("products/index");
break;
case 'details':
$factory->View("products/details");
break;
#How To
case 'how-to':
$factory->View("how-to/index");
break;
#Tech Documents
case 'tech-docs':
$factory->View("tech-docs/index");
break;
#Virtual Home
case 'virtual-home':
$factory->View("virtual-home/index");
break;
#Shopping Cart
case 'shopping-cart':
$factory->View("cart/shopping-cart");
break;
case 'checkout':
$factory->View("cart/checkout");
break;
#Client
case 'client-dashboard':
$factory->View("client/client-dashboard");
break;
case 'client-profile':
$factory->View("client/client-profile");
break;
case 'logout':
ClientLogin::doLogout();
header("Location: index.php");
break;
}
$factory->Template('footer');
Don´t ask why, lol. So my problem is this, when user navigates through website he see url's like this: www.mysite.com?view=products, but i want that they can see something like this: www.mysite.com/products/ but i want to manage it using .htaccess how can i implement that .htaccess to rewrite those ugly url's into nice url's. And asuming that all view requests are handled through that index.php
Also want to see it in my localhost, http://localhost:8080/index.php?view=products to http://localhost:8080/products/
Thanks in advance.
you can do this (replaced $_REQUEST["view"] by $_SERVER['PATH_INFO']):
if(isset($_SERVER['PATH_INFO']) != "" && $_SERVER['PATH_INFO']) {
$view = $_SERVER['PATH_INFO'];
} else {
$view = "";
}

How can I pass this variable and check if it matches?

How can I correct/simplify this and put it in an array?
A link is passing: somelink.php?w=a (or b,c,d)
I want the page (somelink.php) to determine if "w" is set, and if set and the var matches, include the specified page.
<?php
if(isset($_GET['w'])&&($GET['w'] == "a")){include("1.htm");}
if(isset($_GET['w'])&&($GET['w'] == "b")){include("2.htm");}
if(isset($_GET['w'])&&($GET['w'] == "c")){include("3.htm");}
if(isset($_GET['w'])&&($GET['w'] == "d")){include("4.htm");}
else{include("1.htm");}
?>
try using:
$w = $_GET['w'];
if(isset($w)) {
switch(strtolower($w)) {
case "a":
include("1.htm");
break;
case "b":
include("2.htm");
break;
case "c":
include("3.htm");
break;
case "d":
include("4.htm");
break;
default:
include("not-found.htm");
break;
}
}
Use a switch statement:
if(isset($_GET['w']))
{
switch($_GET['w'])
{
case 'a': include("1.html"); break;
case 'b': include("2.html"); break;
case 'c': include("3.html"); break;
case 'd': include("4.html"); break;
default: include("1.html"); break;
}
} else {
include("1.html");
}
how about a simple array
$x=array('a'=>'1.html','b'=>'2.html');
then
include $x[$GET['w']];
Like this:
if(isset($_GET['w'])){
switch($_GET['w']){
case "a":
include("1.htm");
break;
case "b":
include("2.htm");
break;
case "c":
include("3.htm");
break;
case "d":
include("4.htm");
break;
}
}
But I wouldn't do it that way. I'd make it so that the name of the page corresponds to the value being retrieved from the $_GET variable. That way you could do something like this.
if(!empty($_GET['w'])){
include($_GET['w'] . ".htm");
}
Of course, you'd want a little filtering of the $_GET var too to make sure it doesn't get something you don't want there. Maybe like this.
$acceptable_values = array("some","acceptable","values");
if(!empty($_GET['w']) && in_array($_GET['w'],$acceptable_values) ){
include($_GET['w'] . ".htm");
}
As I'm sure you are aware, passing variables directly into include statements or database queries is a TERRIBLE idea. See here for why in this case.
http://websec.wordpress.com/2010/02/22/exploiting-php-file-inclusion-overview/
You could do a few things, lets take a look at some of them.
<?php
$webpage = '';
if(isset($_GET['w']))
$webpage = strtolower($_GET['w']);
switch($webpage)
{
case 'b':
include '2.html';
break;
case 'c':
include '3.html';
break;
case 'd':
include '4.html';
break;
default:
include '1.html';
break;
}
Or we could use arrays
<?php
$webpage = '';
if(isset($_GET['w']))
$webpage = strtolower($_GET['w']);
$included_pages = array(
'a' => '1.htm',
'b' => '2.htm',
'c' => '3.htm',
'd' => '4.htm',
);
// Check inside our array
if(array_key_exists($webpage, $includes))
{
include $included_pages[$webpage];
}
else
{
// Couldn't find the site
include '1.htm';
}

Check several field to execute switch case

I have a code to check through several fields to execute certain code as below:
switch($tag1 || $tag2 || $tag3 || $tag4 ||$tag5){
case "satay":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/satay/$img_name";
break;
case "digitalmarketing":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/interactive_marketing/$img_name";
break;
case "chillicrab":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/chilli_crab/$img_name";
break;
case "chickenrice":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/chicken_rice/$img_name";
break;
case "chendol":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/chendol/$img_name";
break;
}
But it does not work.
Anyone can help?
Switch support one value only. Only IF condition can have OR and AND condition.
$tags = get the tag value.
switch($tags){
case "satay":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/satay/$img_name";
break;
case "digitalmarketing":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/interactive_marketing/$img_name";
break;
case "chillicrab":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/chilli_crab/$img_name";
break;
case "chickenrice":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/chicken_rice/$img_name";
break;
case "chendol":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/chendol/$img_name";
break;
}
The following should do the trick for you:
<?php
$tag1='satay';
$tag2='test2';
$tag3='digitalmarketing';
function isItThere($myTag)
{
switch($myTag)
{
case "satay":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/satay/$img_name";
echo $imgput;
break;
case "digitalmarketing":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/interactive_marketing/$img_name";
echo $imgput;
break;
// etc etc
}
}
for($i=1;$i<4;$i++)
{
isItThere(${'tag'.$i});
}
?>
I have basically set up a small function that contains the switch statement and written a simple loop to test the variables.
As I said in my comment, you can't use more than one variable in the switch statement, but this will provide you a nice clean workaround to do the same thing without the need to write many long statements.

Jpgraph : database data can't insert to the bar chart using php

here is my code :
<?php
// content="text/plain; charset=utf-8"
require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_bar.php');
include("mysql _connect .php");
$code="CSC1113";
$ac_yr="2010/2011";
$sql = "SELECT results, COUNT(*) 'No_Of_grades' FROM std_results WHERE code='$code' && ac_year='$ac_yr' GROUP BY results ORDER BY results ASC";
$result = mysql_query($sql) or die(mysql_error());
while($ors = mysql_fetch_array($result)) {
$grd=$ors['results'];
switch ($grd)
{
case "A":
$datay[1]=$ors['No_Of_grades'];
break;
case "A+":
$datay[0]=$ors['No_Of_grades'];
break;
case "A-":
$datay=$ors['No_Of_grades'];
break;
case "B":
$datay[4]=$ors['No_Of_grades'];
break;
case "B+":
$datay[3]=$ors['No_Of_grades'];
break;
case "B-":
$datay[5]=$ors['No_Of_grades'];
break;
case "C":
$datay[7]=$ors['No_Of_grades'];
break;
case "C+":
$datay[6]=$ors['No_Of_grades'];
break;
case "C-":
$datay[8]=$ors['No_Of_grades'];
break;
case "D":
$datay[10]=$ors['No_Of_grades'];
break;
case "D+":
$datay[9]=$ors['No_Of_grades'];
break;
case "E":
$datay[11]=$ors['No_Of_grades'];
break;
case "AB":
$datay[12]=$ors['No_Of_grades'];
break;
case "NE":
$datay[13]=$ors['No_Of_grades'];
break;
default:
$datay[14]=$ors['No_Of_grades'];
}
}
//set vlaue zero for othe grades..
for($i=0;$i<15;$i++){
if(!isset($datay[$i])){
$datay[$i]=0;
}
}
// Create the graph. These two calls are always required
$graph = new Graph(550,320,'auto');
$graph->SetScale("textlin");
//$theme_class="DefaultTheme";
//$graph->SetTheme(new $theme_class());
// set major and minor tick positions manually
$graph->yaxis->SetTickPositions(array(0,4,8,12,16,20), array(2,6,10,14,18));
$graph->SetBox(false);
//$graph->ygrid->SetColor('gray');
$graph->ygrid->SetFill(false);
$graph->xaxis->SetTickLabels(array('A+','A','A-','B+','B','B-','C+','C','C-','D+','D','E','AB','NE','MC'));
$graph->yaxis->HideLine(false);
$graph->yaxis->HideTicks(false,false);
// Create the bar plots
$b1plot = new BarPlot($datay);
// ...and add it to the graPH
$graph->Add($b1plot);
$b1plot->SetColor("white");
$b1plot->SetFillGradient("#4B0082","white",GRAD_LEFT_REFLECTION);
$b1plot->SetWidth(25);
$graph->title->Set("Bar Gradient(Left reflection)");
// Display the graph
$graph->Stroke();
?>
here jpgrpah doesn't display..error shows : JpGraph Error: 25067 Your manually specified scale and ticks is not correct. The scale seems to be too small to hold any of the specified tick marks.
but I gave direct data for above $datay array.this code is working perfectly..like this
$datay[0]=2;
$datay[1]=5;
$datay[2]=1;
$datay[3]=2;
$datay[4]=0;
$datay[5]=0;
$datay[6]=3;
$datay[7]=0;
$datay[8]=3;
$datay[9]=0;
$datay[10]=1;
$datay[11]=1;
$datay[12]=0;
$datay[13]=1;
$datay[14]=1;
wht's the wrong with my code ....can't understand....help me...thanxx in advanced...
I think now that i understand you :
case "A-":
$datay=$ors['No_Of_grades'];
break;
should be:
case "A-":
$datay['2']=$ors['No_Of_grades'];
break;

Categories