if statement switch - php

<?php
if ($_GET['user_id']) $content = $_GET['user_id'];
else $content = $_GET['content'];
switch($content) {
//The default page
default:
include_once('main.php');
break;
//The news related stuff
case 'news':
include_once('news.php');
break;
//Show the profile of a user
case 'user_id':
include_once('profile.php');
break;
}
?>
index.php?user_id=id won't work. Any ideas?

Maybe you intended this:
if (isset($_GET['user_id'])) $content = 'user_id';
Instead of:
if ($_GET['user_id']) $content = $_GET['user_id'];
else $content = $_GET['content'];
Then the switch would execute the user_id case and include profile.php.

the default case needs to be the last one.
switch($content) {
//The news related stuff
case 'news':
include_once('news.php');
break;
//Show the profile of a user
case 'user_id':
include_once('profile.php');
break;
//The default page
default:
include_once('main.php');
break;
}
Plus, you said that this doesn't work: index.php?user_id=id
The $content variable is being populated by the value of user_id in the _GET string, therefore if you wanted to see something other than the default page, you'd have to do one of these:
index.php?user_id=news
index.php?user_id=user_id
This probably isn't how you want it to behave, but given your code, this is what it's doing...

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 = "";
}

Dynamically output the page title with php

I am trying to output the page title dynamically. I am using induces and this script is withing the header.php the goal is to output the header dynamically using a case/switch statement. here is my code:
<?php $title ;
switch($_SERVER['PHP_SELF']) {
case '/index.php':
$title = 'Home';
break;
case '/about.php':
$title = 'About';
break;
case '/services.php':
$title = 'Services';
break;
case '/portfolio.php':
$title = 'Portfolio';
break;
case '/staff.php':
$title = 'Staff';
break;
case '/contact.php':
$title = 'Contact us';
break;
} ?> <title><?php echo $title ?></title>
I am getting a error telling me my variable $title is not defined?
What i am doing wrong?
In your first line, you have
<?php $title ;
This $title ; shouldn't be there.
And, as Kailash Ahirwar already mentioned, it's always a good idea to provide a default value for your $title:
switch($_SERVER['PHP_SELF']) {
[...]
default:
$title = "Default title goes here";
}
Try to define $title before switch:
$title = "";
in $_SERVER global array, $_SERVER[PHP_SELF] contains full path of file like
/project_name/index.php or /project_name/about.php or /project_name/services.php
Here project_name is name of your project.
replace
case '/index.php'
case '/about.php'
case '/services.php'
....
to
case '/project_name/index.php'
case '/project_name/about.php'
case '/project_name/services.php'
.....
& also initialize $title in start of php file.
<?php $title = "";
switch ($_SERVER['PHP_SELF']) {
case '/project_name/index.php':
$title = 'Home';
break;
case '/project_name/about.php':
$title = 'About';
break;
case '/project_name/services.php':
$title = 'Services';
break;
case '/project_name/portfolio.php':
$title = 'Portfolio';
break;
case '/project_name/staff.php':
$title = 'Staff';
break;
case '/project_name/contact.php':
$title = 'Contact us';
break;
}
?>
for testing purposes
print_r($_SERVER);
and check for $_SERVER(PHP_SELF) value.
The code looks basically fine though you are missing a "default" block to catch anything that is not caught by any of the "case" statements.
though a beginner in php, i sorted this out on my own, and it is very simple and logical, consider title as string and it is initially empty one write on top $title = "";
when including header in home page just after inclusion write the value of title(reassigning value of variable)

switch case isset triggered when case is -not- set

--Let me add this. This code works for me the way it is. I just do not know why it works.--
I can't figure this out.
switch ($_SERVER['QUERY_STRING']) {
case isset($_GET['test0']):
echo "test0<br>";
break;
case isset($_GET['test1']):
echo "test1<br>";
break;
case isset($_GET['test2']):
echo "test2<br>";
break;
case isset($_GET['test3']):
echo "test3<br>";
break;
case isset($_GET['test4']):
echo "test4<br>";
break;
default:
echo "no test<br>";
break;
}
When the url is index.php?test0, "test0" is shown.
When the url is index.php?test4, "test4" is shown.
When the url is index.php?test999, "no test" is shown.
When the url is index.php?tes, "no test" is shown.
When the url is index.php?, or index.php, "test0" is shown.
Why is this? The condition is not met, so should the default not be shown?
switch can't be used this way. isset() returns true or false, not something (a string, an int, etc) you can match against. What you are basically doing is:
switch ($_SERVER['QUERY_STRING']) {
case true:
echo "test0<br>";
break;
case true:
echo "test1<br>";
break;
case false:
echo "test2<br>";
break;
case false:
echo "test3<br>";
break;
case true:
echo "test4<br>";
break;
default:
echo "no test<br>";
break;
}
cases are considered from top to bottom. In this case, $_SERVER["QUERY_STRING"] is automatically type-converted to bool (which will return true in this case). The first case it sees would be test0, so it echos that. If you do that for test0-4, it will give you the false illusion that this code is working as intended, while it's not considering the edge cases.
The only way you can achieve what you want is by using multiple ifs, or by redesigning your application.
When the url is index.php?, or index.php, "test0" is shown.
Why is this? The condition is not met, so should the default not be shown?
Like a good question, your question as well contains the answer already.
You already have realized that the condition must be met even you think it is not met. Therefore you ask. So let's see which condition is met:
case isset($_GET['test0']):
echo "test0<br>";
break;
This is a test for isset($_GET['test0']) and we know with the request that this is FALSE. So this test tests for FALSE.
Now let's see against what this tests:
switch ($_SERVER['QUERY_STRING']) {
That is $_SERVER['QUERY_STRING']. So if $_SERVER['QUERY_STRING'] is FALSE the test0 will be output.
Because switch { case:} in PHP does loose comparison, the empty string $_SERVER['QUERY_STRING'] is FALSE. This is why you see the output.
Easy if you know why, right? And all so logical.
And what you wanted to test against was not $_SERVER['QUERY_STRING'] but just TRUE:
switch (TRUE)
{
case isset($_GET['test0']) :
...
}
This gets the job done, too.
<?php
$q = $_SERVER['QUERY_STRING'];
if(!empty($q) && isset($q) && strlen($q) >0 ){
$url = $q;
switch ($url){
case true;
echo $url;
break;
}
}
else {
echo "no test<br>";
}
what about
$found = false;
for($i=0;$i <=4; $i++){
if( isset($_GET['test'.$i]) ){
echo "test".$i;
$found = true;
}
}
if(!$found){
echo "no test";
}

Is it possible to create a PHP Switch which check the url for a specific string?

Is it possible to create a PHP switch, which checks a URL's for a specific string? I'm using $_GET to customize the URL'S of specific pages depending on various situations. I want to trim down the Url and check it against several cases. Here is the code I have so far.
Here is the an example of the URL:
www.example.com/page/resource/view.php?id=417&module=1&menuid=4&module=1&page=4
<?php
// Grabs the page URL
$url = curPageURL();
var_dump($url);
$trimURL = trim($url);
var_dump($trimmed);
$trimmed = trim($text, "resource/view.php?id=417&module=1& || &module=1&page=4");
var_dump($trimmed);
// The Switch
switch ($trimmed) {
case "menuid=1":
echo "Menu ID 1";
break;
case "menuid=2":
echo "Menu ID 2";
break;
case "menuid=2":
echo "Menu ID 3";
break;
default:
echo "No Menu Number Detected.";
}
?>
switch($_GET["menuid"]) {
case "1":
echo "Menu ID 1";
break;
case "2":
echo "Menu ID 2";
break;
}

Categories