/    Sign up×
Community /Pin to ProfileBookmark

how to build an ELSEIF based on 2 URL variables

I need to create an ELSEIF function based on 2 URL Variables, for example

exampleurl.com/script.php?csl=ps2&id=1234

and the code is the part i’m stuck on. i need between 35 and 40 csl variables and inside them upto 10,000 id options.

Each id would need an include output, i think i can do this as below:

[code=php]if($id == “1234” {
include “1234.php”;
}
[/code]

Does anyone know how i can construct the rest of the code?

to post a comment
PHP

17 Comments(s)

Copy linkTweet thisAlerts:
@SheldonJan 04.2008 — I am not sure if this is exactly what you are after, I don't fully understand your question.

[code=php]
<?php
// RETREIVE BOTH $_GET ELEMENTS
$csl = $_GET['csl'];
$id = $_GET['id'];

// MAKE SURE THE $csl VAR IS NOT EMPTY.
if(!empty($csl)){
// MAKE SURE THERE IS AN $id SET.
if(!empty($id)){
// SPECIFIY TEH FILE TO INCLUDE/
$path = "includes/{$csl}/{$id}.inc.php";
// MAKE SURE THERE REALLY IS A FILE.
if(file_exists($path)){
// INCLUDE THE FILE
include($path);
}
}
}
?>[/code]
Copy linkTweet thisAlerts:
@danpoultonauthorJan 04.2008 — thanks, thats pretty much what i want!

the only thing i need to do it specify is an include for no variables at all and if only a csl is specified.

eg. url.com/script.php (and no variables)

and

eg. url.com/script.php?csl=ps2 includes ps2/ and xbx includes xbx/.
Copy linkTweet thisAlerts:
@knowjJan 05.2008 — you should look into turning registered globals off.

rather than using $id you would use $_GET['id']

GET = a variable from the URL
Copy linkTweet thisAlerts:
@danpoultonauthorJan 05.2008 — you should look into turning registered globals off.[/QUOTE]

What does that mean? (am new to PHP)
Copy linkTweet thisAlerts:
@blue-eye-labsJan 05.2008 — why don't you use this:

[code=php]
$id = $_GET['id'];
$csl = $_GET['csl'];

if(!$id || !$cls) { //checks whether or not the vars are empty
include("default.php"); //if either is emtpy (or both) then include the default file
} else {
include($id . ".php"); //otherwise include id.php so if id=1234 then 1234.php
}

[/code]
Copy linkTweet thisAlerts:
@danpoultonauthorJan 26.2008 — thanks for your code, had been using it but did not reply.

i've changed it to

[code=php]<?php

$game = $_GET['game'];
$page = $_GET['page'];
$letter = $_GET['letter'];
$console = $_GET['console'];

if(!$game || !$page || !$letter || !$console) { //checks whether or not the vars are empty
include "http://www.gameslayer.co.uk/cheatserve/serve/default.php?type=title"; //if either is emtpy (or both) then include the default file
} else {
include "http://www.gameslayer.co.uk/cheatserve/serve/" . $console . $letter . $game . $page . ".php?type=title"; //otherwise include id.php so if id=1234 then 1234.php
}

?>[/code]


The problem i have is that the default page is included if all four variables aren't provided, if one variable is provided a page will be included so how would i change the code?

i am aware its this part
[code=php]if(!$game || !$page || !$letter || !$console)[/code]

how can i change that so it is if variable $game exists include a file and if $page exists include that, also what if more than one variable is included?

can that be done with changing the second part of code?
Copy linkTweet thisAlerts:
@blue-eye-labsJan 27.2008 — Unfortunately, fx just crashed for no good reason and I lost my reply.

I think I understand, but the best way to do this would be using a mysql database, since it would take covering various permutations. You could do this with about 21 if/elseifs but that is long.

Alternatively, if you're trying to do something that I think you're trying, this might work.

[code=php]
#set the variables
$game = $_GET['game'];
$page = $_GET['page'] or "1";
$letter = $_GET['letter'] or "A";
$console = $_GET['console'];

if(!$game && !$page && !$letter && !$console) { //if all are emtpy
include("default.php");
elseif($game) { //if $game is set
//basically this includes a file with the name of the game, and if the console is set, it tells it what console it's on, if a page is specified it goes to that page (if unspecified, the page is 1).
include($game . ".php?page=" . $page . "&console=" . $console);
} elseif(!$game && $console) {
//if no game is specified, display the page for that console.
//if no letter is specified, the default is a so the console page should list all games beginning with either the specified letter or 'A'
include($console . ".php?letter=" . $letter);
} elseif($letter && !$console) {
//if only a letter, but not a console, then display the letter page
include($letter . ".php");
} else {
//if none of these combos are specified then include the default again
include("default.php");
}
[/code]

I hope that that was what you were looking for.
Copy linkTweet thisAlerts:
@danpoultonauthorFeb 01.2008 — This is the code i am using now.

[code=php]<?php

$game = $_GET['game'];
$page = $_GET['page'];
$letter = $_GET['letter'];
$console = $_GET['console'];

if(!$console && !$letter && !$game && !$page) { //checks whether or not the vars are empty
include "http://www.gameslayer.co.uk/cheatserve/serve/default.php?type=title"; //if either is emtpy (or both) then include the default file
} else {
include "http://www.gameslayer.co.uk/cheatserve/serve/" . $console . $letter . $game . $page . ".php?type=title"; //otherwise include id.php so if id=1234 then 1234.php
}

?>[/code]


That works and does what i want it to do.

However i would like to have that code in the <head> of the page

and have something like the below code in the <body> where i want content to appear.

[code=php]<? include "$title"; ?>[/code]

and also $content where in the first code is [code=php] . $console . $letter . $game . $page . ".php?type=title[/code] will include ?type=content.

How can i change the code to allow that.
Copy linkTweet thisAlerts:
@blue-eye-labsFeb 01.2008 — just integrate it into your HTML page, so:
[code=html]
<!-- ... top bit of the page w/ doctype etc ... -->
<head>
<title>A page</title>
<?php //some PHP code ?>
</head>

<body>
<?php //some more PHP code ?>
<div>
<?php //even more PHP code ?>
</div>
</body>
</html>
[/code]


I hope that that makes sense.
Copy linkTweet thisAlerts:
@danpoultonauthorFeb 02.2008 — ok thanks that makes sense

thanks for all your help
Copy linkTweet thisAlerts:
@danpoultonauthorFeb 12.2008 — right, all the code works.

the only addition i would like to make is a customised title.

So currently "Console > Letter > Game" is all you can have. it would be better the page which is included stored the title in parts as variables, for example:

[code=php]
$console = "PS2";
$letter = "F";
$game = "FIFA 08";
[/code]


is in the include, then on the page including the variables above

[code=php]
<title>Website Name > $console etc</title>
[/code]


The only problem i can think of is the seperators (>), if one of the variables doesn't exist, as on index pages with only console and letter the output would be Console > Letter >

How would i code that?

thanks
Copy linkTweet thisAlerts:
@blue-eye-labsFeb 12.2008 — Firstly, an important point to note: use "&lt;" for less-than signs (<) and "&gt;" for greater-than signs (>). <> are HTML entities so must be coded for as I have shown.

Anyway... you could do something like this:
[code=php]
<?php
$console = $_GET['console'];
$game = $_GET['game'];
$letter = $_GET['letter'];
$title = "MyGameWorld.com";

(!empty($console)) ? $title .= " &gt; $console" : $title .= "";
(!empty($game)) ? $title .= " &gt; $game" : $title .= " &gt; $letter";
?>
<!-- top of page etc -->
<title><?php echo $title; ?></title>
<!-- rest of page -->
[/code]


What this does is first checks whether or not the $console var is empty, if it isn't, then it displays "MyGameWorld.com > console", if it is then "MyGameWorld.com". It does similar for the $game variable, showing either a game or a letter (if the $game var is empty).

I hope that that solves your problem.
Copy linkTweet thisAlerts:
@danpoultonauthorFeb 13.2008 — thanks that does solve my problem, i just have a few questions, currently the below code includes the title

[code=php]
<?php

$game = $_GET['game'];
$page = $_GET['page'];
$letter = $_GET['letter'];
$console = $_GET['console'];

if(!$console && !$letter && !$game && !$page) { //checks whether or not the vars are empty
include "http://www.gameslayer.co.uk/cheatserve/serve/default.php?type=title"; //if either is empty (or both) then include the default file
} else {
include "http://www.gameslayer.co.uk/cheatserve/serve/" . $console . $letter . $game . $page . ".php?type=title > "; //otherwise include id.php so if id=1234 then 1234.php
}

?>
[/code]


and on the page included

[code=php]
elseif($type == "title"){
$titlegame = "Choose a Console";
}

(obviously choose a console is not a game, it was just to test the default page).
[/code]


so i put the codes together (not sure if thats how to make it work)

[code=php]
<?php

$game = $_GET['game'];
$page = $_GET['page'];
$letter = $_GET['letter'];
$console = $_GET['console'];

if(!$console && !$letter && !$game && !$page) { //checks whether or not the vars are empty
include "http://www.gameslayer.co.uk/cheatserve/serve/default.php?type=head"; //if either is empty (or both) then include the default file
} else {
include "http://www.gameslayer.co.uk/cheatserve/serve/" . $console . $letter . $game . $page . ".php?type=head > "; //otherwise include id.php so if id=1234 then 1234.php
}

$titleconsole = $_GET['console'];
$titlegame = $_GET['game'];
$titleletter = $_GET['letter'];
$titleprefix = "Prefix";

(!empty($titleconsole)) ? $pagetitle .= " &gt; $titleconsole" : $titleprefix .= "";
(!empty($titlegame)) ? $pagetitle .= " &gt; $titlegame" : $titleprefix .= " &gt; $titleletter";
?>
[/code]


As you can see i changed variable names (and would like to add some more like titlesuffix and just pagetitle for default.php and other non-game pages)

thanks for you help, how can i add the code.
Copy linkTweet thisAlerts:
@danpoultonauthorMar 03.2008 — i know it's been a while, i have changed the code to the below.

the $lines part gets text from a file based on its line ... and works.

i am having difficulty with the second part, i now have the title in three parts, title_console, title_letter and title_game (in the variable, $type).

the if statement is where i am stuck, i think what i have coded is:

IF = all variables empty, file_get_contents default.php

ELSEIF = console is set, only file_get_contents the page with the title_console type variable ?type=title_console

and so on the second variable being if console and letter, than include the console, get the separator (>) from the text file and include title_letter.

However, this does not work, anyone see where i have gone wrong?

[code=php]
<?php

$lines = file('config.txt');
$l_count = count($lines);
for($x = 0; $x< $l_count; $x++)

{
}

$game = $_GET['game'];
$letter = $_GET['letter'];
$console = $_GET['console'];

echo '<title>' . $lines[3];

if(!empty($console) && !empty($letter) && !empty($game)) {
echo file_get_contents("http://www.gameslayer.co.uk/cheatserve/serve/default.php?type=title_console");
}
elseif(!isset($console)) {
echo file_get_contents("http://www.gameslayer.co.uk/cheatserve/serve/" . $console . $letter . $game . ".php?type=title_console");
}
elseif(!isset($console) && !isset($letter)) {
echo file_get_contents("http://www.gameslayer.co.uk/cheatserve/serve/" . $console . $letter . $game . ".php?type=title_console");
echo $lines[9];
echo file_get_contents("http://www.gameslayer.co.uk/cheatserve/serve/" . $console . $letter . $game . ".php?type=title_letter");
}
elseif(!isset($console) && !isset($letter) && isset($game)) {
echo file_get_contents("http://www.gameslayer.co.uk/cheatserve/serve/" . $console . $letter . $game . ".php?type=title_console");
echo $lines[9];
echo file_get_contents("http://www.gameslayer.co.uk/cheatserve/serve/" . $console . $letter . $game . ".php?type=title_letter");
echo $lines[9];
echo file_get_contents("http://www.gameslayer.co.uk/cheatserve/serve/" . $console . $letter . $game . ".php?type=title_game");
}
else {
}

echo $lines[6] . '</title>';

?>
[/code]
Copy linkTweet thisAlerts:
@blue-eye-labsMar 04.2008 — So, if I understand correctly your if ... elseif statement should be structured as follows

if all three are empty, get default file

otherwise, if just the console is set, get the file for that console

otherwise, if just the console AND the letter is set, then get appropriate &c.

Okay. It looks as if you're getting confused with using the "!" operator. This acts as a logic NOT. That basically means that your "!isset($var)" is checking if something is not set, which is where it's going wrong, it would seem. Try this:

[code=php]
if(empty($var1) && empty($var2) && empty($var3)) {
//if all variables (1, 2 and 3) are empty, then incl default
include("default.php");
} elseif(!empty($var1) && empty($var2) && empty($var3)) {
//if all are empty except var1 (console)
include("page.php?console=$var1");
} elseif(!empty($var1) && !empty($var2) && empty($var3)) {
//if 1 and two are set, but 3 isn't
include
...
[/code]


I'm sure you get the point. For complete control you'll need to create an elseif for each scenario. There are 3 + 3!/2! + 1 different scenarios which is 7 different scenarios, I believe (don't bother correcting me if I'm wrong ?), so that's not too many.

I hope that that helps.
Copy linkTweet thisAlerts:
@danpoultonauthorMar 07.2008 — thanks that's exactly what i wanted. ?
Copy linkTweet thisAlerts:
@blue-eye-labsMar 07.2008 — excellent.
×

Success!

Help @danpoulton spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...