I have a bit of a problem with my PHP code, I am assigning values to variables in different states of the flow depending on what I receive, but for some reason it keeps getting stuck at one point, here is the code.
if (isset($session)) {
//if the user is in the database
if ($row == 1) {
$from = $_GET['from'];
if (isset($from)) {
$page = $_GET['page'];
switch ($page) {
case "game":
$page = "game";
sendVars($page);//send the variable
break;
case "gallery":
$page = "gallery";
sendVars($page);//send the variable
break;
case "leaderboard":
$page = "leaderboard";
sendVars($page);//send the Variable
break;
}
}else {
$page = "game";
sendVars($page);//send the variable
}
//if the user is not in the database
}else {
//do this
}
} else {
//register
}
Now for some odd reason, it keeps setting the value of $page to game, even though I set the page variable to gallery like so http://www.mydomai.com/?from=set&page=gallery . the only reason for this that I can think of is that my switch is not working as it should? or it is bypassing the switch somehow?
Thanx in advance!
I just ran your code after removing a few of the unessersary variable assignments:
<?php
// I added this function just for testing
function sendVars($page) {
echo $page;
}
if (isset($_GET['from'])) {
$page = $_GET['page'];
switch ($page) {
case "game":
sendVars($page); //send the variable
break;
case "gallery":
sendVars($page); //send the variable
break;
case "leaderboard":
sendVars($page); //send the Variable
break;
}
} else {
$page = "game";
sendVars($page); //send the variable
}
And it all seems fine, xxx.php?from=1&page=gallery echos out "gallery", try doing a print_r($_GET) at the top of your script and see what it prints out and let us know.
On a side note I think the below may be shorter for you and still do the same thing:
if (isset($_GET['from'])) {
// Check if $_GET['page'] exsists and is either game, gallery or leaderboard
if (isset($_GET['page']) && in_array($_GET['page'], array('game', 'gallery', 'leaderboard')))
sendVars($_GET['page']);
}
else
sendVars('game');
I hope this helps
Cheers
Luke
Try doing a var_dump($page);exit; before the switch and see what it spits out.
Also you can do a var_dump($from) and see what that is spitting out - it may be that it goes to the else, so it may not even be getting to the switch.
If this is inside a function, I personally prefer guard-style clauses than constantly increasing the levels of indentation. The idea is you pick out the bad conditions (ie if something is going wrong) to "protect" the larger block of logic.
In your case that's the switch statement.
if (!isset($session))
return ...; // register
if ($row != 1)
return ...; // do this
$from = $_GET['from'];
$page = $_GET['page'];
if (isset($from)) switch ($page) {
case "game":
$page = "game";
break;
case "gallery":
$page = "gallery";
break;
case "leaderboard":
$page = "leaderboard";
break;
}
else $page = "game";
return sendVars($page);// don't need to repeat this if all cases do it!
It's just a style of code and it's not going to fix all (if any) of your problems. You actually don't need the switch block in there for this code. I can't see that it's doing anything.
You don't necessarily need the switch statement. I can't see an obvious problem with your logic but I think this will do the same thing:
if (isset($session))
{
//if the user is in the database
if ($row == 1)
{
$page = (in_array($_GET['page'],array('game','gallery','leaderboard'))) ? $_GET['page'] : "game";
sendVars($page); //send the variable
}
else //if the user is not in the database
{
//do this
}
}
else
{
//register
}
Just saw Luke used the same in_array method but 25 mins before me. Serves me right for being slow!
Related
<?php if (isset($_GET['p'])) { $variable = $_GET['p']; ?>
<?php if ($variable == '1'): ?>
<?php include('pages/home.php');?>
<?php endif;?>
<?php };?>
<?php if (isset($_GET['p'])) { $variable = $_GET['p']; ?>
<?php if ($variable == '2'):
include('pages/about.php');
else: include('pages/home.php'); endif; };?>
The above is what I have used to try and fix it, but if I don't put it, errors show up. For example if I used "ifelse", and it tells me to change it to else, or endif. But when I use endif, it tells me to use ifelse. I'm trying to make it so http://localhost/?p=PAGE_ID just simply shows the page using an include(FILE_PATH) statement.
Does anyone know where I went wrong? Or how I can fix it :)
You seem to have thoroughly tied yourself in knots. To my mind, this is a much cleanrer and more flexible and maintainable way to approach the whole thing:
$pages = [
"1" => "pages/home.php",
"2" => "pages/about.php"
];
if (isset($_GET['p'])) {
if (isset($pages[$_GET['p']]))
{
$page = $pages[$_GET['p']];
}
else
{
$page = $pages["1"];
}
}
else
{
$page = $pages["1"];
}
include($page);
This way, you have a list of pages, and the system simply looks up the required page in the array by its index number (as passed in via the GET parameter). If it can't find that index, it just uses the default page.
You can now easily add pages by just adding items to the array, without needing to write more if/else logic. You could even store the array data in a database instead of it being hard-coded.
You dont need or want an elseif in this flow, simple do
<?php
if (isset($_GET['p'])) {
if ($_GET['p'] == '1') {
include('pages/home.php');
}
if ($_GET['p'] == '2') {
include('pages/about.php');
}
}
If the value of $_GET['p'] can only be 1 or 2 you could do
<?php
if (isset($_GET['p'])) {
if ($_GET['p'] == '1') {
include('pages/home.php');
} else {
include('pages/about.php');
}
}
Or if $_GET['p'] could be multiple values a switch may be more useful
<?php
if (isset($_GET['p'])) {
switch($_GET['p']) {
case 1:
include('pages/home.php');
break;
case 2:
include('pages/about.php');
break;
default:
include('pages/somethingelse.php');
}
}
Note I also left out the intermediate variable, as its not needed. The $_GET and $_POST arrays are, once filled by PHP all yours to use as you want. There is no need to waste CPU cycles and memory creating unnecessary variables
I have this code being used on my website
if( isset($_GET['gw'] )){
//this is some content
}
So whenever my url is something like this
http://mywebsite.com/?gw=hello
the content inside that div will appear. It works very well, but now what I want to do is have different content for a specific string. Something like this
if (gw=hello) {
// content here
} else if (gw=goodbye) {
// content here
} else {
//content here
}
I know that is not the code, but does anyone know a way I could do this? Thanks!
Your pretty much there!
if(isset($_GET['gw'])) {
if($_GET['gw'] == "hello") {
}
else if ($_GET['gw'] == "goodbye") {
}
else {
}
}
It's probably best to use a switch statement, so you can easily expand on the conditions in the future.
switch (isset($_GET['gw']) ? $_GET['gw'] : '')
{
case "hello":
// do something here
break;
case "goodbye":
// do something here
break;
default:
// default action if nothing above matches
break;
}
$door = $_POST["doorType"];
$doorWidth;
if ($door=="Single")
{
$doorWidth = $width;
}
else if ($door=="Double")
{
$doorWidth = $dOneWidth;
}
When I run the page it doesn't recognize the variable $doorWidth?
$doorWidth;
doesn't assign anything. It only returns the variable ... to anything. Doing this PHP is accessing the variable, causing a notice. Write for example:
$doorWidth = NULL; // assigns something (some default value if $door isn't "Signle" nor "Double")
I guess that $door has a value far from Single|Double. This may be caused by another error in your application. You should learn, that you should in any case set a proper default value for a variable if you are about to assign to it from into a conditional statement (like if):
$doorWidth = 'not set!';
if ($door=="Single")
{
$doorWidth = $width;
}
else if ($door=="Double")
{
$doorWidth = $dOneWidth;
}
Further note about the switch statement which has a default: branch:
switch($door) {
case 'Single' :
// do something
break;
case 'Double' :
// do something else
break;
default:
die('$door has a value far from 'Single|Double'. Currently: ' . $door);
}
I have some hardcoded if/else statements to set $page variable - (for later use in header: ".page") - to a given website based on the $_POST["username"] input.
CODE:
if ($_POST["username"] == "username1#domain1.com") {
$page = "http://www.google.com";
}
else if ($_POST["username"] == "username2#domain1.com"){
$page = "http://www.yahoo.com";
}
else if ($_POST["username"] == "username1#domain2.com"){
$page = "http://www.stackoverflow.com";
}
else if ($_POST["username"] == "username2#domain2.com"){
$page = "http://www.serverfault.com";
}
else if ($_POST["username"] == "username#domain3.com"){
$page = "http://www.superuser.com";
}
else if (!preg_match($domain2.com, $_POST["username"])) { //THIS IS VERY WRONG
$page = "http://www.backblaze.com";
}
else{
$page = "DefaultBackupPage.php";
}
I am trying to say if your username has "#domain.com" at the end of it, set the $page to, in this case backblaze.com, but could be anything.
I am aware it is messy, I don't actually like this implementation. It just has to fit in this schema and I need a quick fix!
The current error I am receiving is that the regular expression is empty. I am hoping this is a no brainer for someone who knows PHP - which I have been hastily trying to learn!
if (preg_match('/#domain2\.com$/i',$_POST['username']))
will catch username ending with domain2.com. Please note the escaping backward slash for the dot. If you want to test against the opposite (aka. does NOT end with domain2.com) then use an exclamation mark before the preg_match() function.
Is this what you were asking?
EDIT 1: I added the i flag to the pattern to make it look for a case-insensitive match.
EDIT 2: For the sake of readability and control I would wrap this in a function instead, but that's my own preference so it's not the suggested approach or anything. In case your code is long and complicated…
function get_page($username) {
$username = strtolower($username);
switch ($username) {
case "username1#domain1.com" : return "http://www.google.com";
case "username2#domain1.com" : return "http://www.yahoo.com";
case "username1#domain2.com" : return "http://www.stackoverflow.com";
case "username2#domain2.com" : return "http://www.serverfault.com";
case "username#domain3.com" : return "http://www.superuser.com";
}
return preg_match('/#domain2\.com$/',$username) ?
"http://www.backblaze.com" : "DefaultBackupPage.php";
}
$page = get_page($_POST['username']);
The line:
else if(!preg_match($domain2.com, $_POST["username"]))
has to be:
else if(!preg_match("/domain2\.com/", $_POST["username"]))
I need to find the value of a variable and use it to add a class to a div, based on a switch statement.
For example, my variable is $link and if $link has google.com IN IT at all, I need $class to equal 'google', if $link as yahoo.com IN IT at all, $class then needs to equal 'yahoo'
So, I need something like this, but I'm not sure how/or if to use preg_match or something to check and see if the $link variable has the value we are looking for in it - see 'case' text below:
switch ($link) {
case 'IF link has Google.com in it':
$class = 'google';
break;
case 'IF link has Yahoo.com in it':
$class = 'yahoo';
break;
default:
# code...
break;
}
OR if there is a better way to do this, please let me know :D
Also, I'm good with using an IF ELSE statement as well..
Thanks
You want an IF-statement, not a switch statement
I think preg_matchis not necessary here.stripos is enough for it.
$url = $link->hits;
$pos_google = stripos($url,'google.com');
$pos_yahoo = stripos($url,'yahoo.com');
if($pos_google !== false)
{
$class = 'google';
}
elseif($pos_yahoo !== false)
{
$class = 'yahoo';
}
else
{
#code
}
Seems it could be simpler:
if(ereg("google", $link)){
$class = "google";
}else if(ereg("yahoo", $link)){
$class = "yahoo";
}else{
$class = "";
}