Im trying to take a url like this:
www.clan-n2o.com/index.php?view=Console&action=StatsChecker&user=Me
using a switch statement i was able to parse the data correctly but then i came to and question:
How would i check to see whether user has a value?
Here is my code (so far):
switch ($view) {
case "Console":
switch($action) {
case "StatsChecker":
switch($user) {
case ""; // ?? user has a value? <-- problem here
break;
default:
StatsCheck();
break;
}
break;
}
}
Ideas:
switch ($view) {
case "Console":
switch($action) {
case "StatsChecker":
switch($user) {
case isset($user); // would this work?
break;
default:
StatsCheck();
break;
}
break;
}
}
Your first code snippet should work, I'd use the die() function though in that case though assuming you want the script to stop if $user doesn't have a value.
I would use a check like:
if(isset($user) or $user == "") {
die("Error: No user specified.");
} else {
StatsCheck();
}
But is there any way to use a switch statement?
like this:
case "Console":
switch($action) {
case "StatsChecker":
switch($user) {
case isset($user):
switch ($game) {
case isset($game):
getstats($user, $game);
break;
default:
StatsCheck-Enter();
break;
}
break;
default:
StatsCheck-Enter();
break;
}
break;
// Next Case goes here
}
break;
Reason is im writing a complete Clan Script off of 1 file and i wanted to fully parse the url.
Rather than having intensively-loaded switch conditions within other switch conditions, you should subdivide into smaller functions, for instance:
case "Console":
testConsole($action);
break;
case "Something":
testSomething($action);
break;
// later
function testConsole($action) {
switch ($action) {
// blah
}
}
That makes debugging easier and code maintenance MUCH easier.
Quote from: AC_Drkan on December 02, 2004, 05:08 PM
Im trying to take a url like this:
www.clan-n2o.com/index.php?view=Console&action=StatsChecker&user=Me
using a switch statement i was able to parse the data correctly but then i came to and question:
How would i check to see whether user has a value?
Here is my code (so far):
switch ($view) {
case "Console":
switch($action) {
case "StatsChecker":
switch($user) {
case ""; // ?? user has a value? <-- problem here
break;
default:
StatsCheck();
break;
}
break;
}
}
Ideas:
switch ($view) {
case "Console":
switch($action) {
case "StatsChecker":
switch($user) {
case isset($user); // would this work?
break;
default:
StatsCheck();
break;
}
break;
}
}
isset will not accomplish what you want because even if user===''; isset($user) will still return true. isset returns false only if user=== NULL; || unset($user);
what you might try to do is this, if you can use if's:
switch($_GET['user'])
case isset($_GET['user']):
if($_GET['user']==='')
echo 'You didn\'t enter a username.';
else{
echo $_GET['user'].' was entered as a username.'
}
default:
echo 'Either you are trying to access this page directly, or your login was not correctly parsed. Please attempt to re-loggin. Thankyou.';
Ok thy on that subject.
Now i got another:
i cannot set a cookie in php,
i know the code, and my clan scripts are running fine. but for some odd reason
this:
if (isset($_COOKIE['visit'])) {
$lastvisit = $visit;
echo("<p align=\"center\"><FONT FACE=\"Tahoma\" SIZE=\"1\" COLOR=\"#FFFFFF\">Hello Again there your last visit to this site was on $lastvisit.</p>");
} else {
setcookie("visit", date( "D M d, Y H:i:s \G\M\T\."), time() + 3600);
echo("<p align=\"center\"><FONT FACE=\"Tahoma\" SIZE=\"1\" COLOR=\"#FFFFFF\">Hello and Welcome To This Clan, This is your first visit to thes site today!</p>");
}
does absolutely nothing,
I have looked at every php manual that i can think of and it says that this code would be correct but for some weird reason, it will not output:
Hello Again there your last visit to this site was on $lastvisit.
line 2
$lastvisit = $_COOKIE['visit'];
cookies can not be set after anything has been outputted to the screen. This includes echo commands and even the <html> tag. Be sure to set your cookies at the top of every page.