...

View Full Version : Using $_GET with no variable in the URI



SmArTaSs
11-28-2003, 09:27 PM
First off, I've only been working with PHP for 3 or 4 weeks.
So this might be an easy problem to solve, maybe not.

Here's my problem: EVERY page on my site has ?p= (eg. ?p=games) except for the index page, so that func.php knows that file to include. The problem comes about when ?p= does not appoear in the URL (on the index).

When people are looking at /index.php or / I want to include news/show_news.php. I have tried all the diff combinations of if, elseif, else, etc. and nothing works.

Right now when you look at index.php, with no ?p=, it shows no content. I want it to show the news when ?p= does not appear in the URL.

http://smartass.buildtolearn.com/

Any suggestions?

the code w/ the problem in index.php:


if($_GET['p']){
include('func.php');
content();
}
else{
include("news/show_news.php");
}

func.php:


<p align="center">
<?php
function content() {
if($_GET["p"] == "") { require("links/config.php");}
if($_GET["p"] == "join") { include("links/join.php");}
if($_GET["p"] == "lostcode") { include("links/lostcode.php");}
if($_GET["p"] == "lostid") { include("links/lostid.php");}

.......

}?>
</p>

raf
11-28-2003, 09:48 PM
Do you want to include the files are redirect to them?


Anyway,


if($_GET['p']){
include('func.php');
content();
}
else{
include("./news/show_news.php");
}


(note the ./ in front of the adress)
Would probably work (if that file existsn that is ... )

Instead of


if($_GET["p"] == "") { require("links/config.php");}
if($_GET["p"] == "join") { include("links/join.php");}
if($_GET["p"] == "lostcode") { include("links/lostcode.php");}
if($_GET["p"] == "lostid") { include("links/lostid.php");}

You'd probably better use a switch


switch ($_GET['p'] ) {
case 'join':
include('./links/join.php');
break;
case 'lostcode':
include('./links/join.php');
break;
....
default:
require('./links/config.php');
}


which will be quicker because in your code, the querystring will be compared to each comparison,, where in the switch, the comparionons will be stopped after a match (because of the break). So if you order the cases and put the most occuring on top, you'll get some performance gain.

or you could use


if($_GET['p'] == '') {
require('./links/config.php');
} else {
include('./links/' . $_GET['p'] . '.php');
}


will be fastest.

SmArTaSs
11-28-2003, 10:13 PM
ok, i took your advice and the content works fine, but the news still wont show :confused:

raf
11-28-2003, 10:49 PM
If you don't get an error, then the files content is included inside your PHP file.

But include just inserts the show_news.php code inside the page where you call it from. If the code from show_news.php doesn't generate any output, then you wount see anything.

so add something like

echo 'OK';

at the first line of your show_news.php (after the php-tag of course)

nicklim
12-02-2003, 04:40 AM
i would do this

$page= ((isset($_GET['p'])) && ($_GET['p']!=""))? $_GET['p'] : "config";
// (if thre is a ?p= and it contains something... ) then we get p.. else page = config...

if ($page=="config") require ......
else if ($page==.... ) all the includes

this will check for the presence of ?p and act accordingly.. (hope this helps)

mcoelho123
05-16-2006, 05:56 PM
Hi guys i tried this:

<?php
switch (isset($_GET['login'])) {

case 'error':
print ('Login Error');
break;
case 'failed':
print ('Invalid Login');
break;
default:
print ('<br>');
}
?>

But it returns always the first case the 'Login Error' even the login is failed

Anyone can help me with that,
Thanks in advance

GJay
05-16-2006, 06:16 PM
isset() returns either true or false, so using switch isn't really appropriate...

mcoelho123
05-16-2006, 06:19 PM
Ohh OK, so i cant use it here

Thanks

goughy000
05-16-2006, 06:55 PM
// If not set or p= nothing..
if(!isset($_GET['p'] || $_GET['p'] == ""){
// include news page
include_once("path/to/news");
}else{
content();
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum