Log in

View Full Version : PHP Variables



Techykid3
08-15-2011, 12:32 PM
I have been searching the internet about this... Still nothing, but I know its possible:

I need to display content based on if they have a certain variable...

I need it to be like this: http://mywebsite.com/downloads/download.php?id=1 would do this: include('files/wallpaper/chat.php');

So therefore http://mywebsite.com/downloads/download.php?id=2 would do this: include('files/screensaver/download.php');

and all other requests that don't have a variable in the url would include "nodownload.php"

Thanks guys :)

bluewalrus
08-15-2011, 01:06 PM
Something like this should do it. Not sure if the switch default includes empty so the first two lines maybe removable.


<?php
if (empty($_GET['id']))
header("Location : nodownload.php");
switch ($_GET['id'])) {
case 1:
include('files/wallpaper/chat.php');
break;
case 2:
include('files/screensaver/download.php');
break;
default:
header("Location : nodownload.php");
break;
}
?>

djr33
08-15-2011, 01:07 PM
Slow down and start with the answer you already have here:
http://dynamicdrive.com/forums/showthread.php?t=64003

The rest is just some basic if statements. If that is too complicated, you need to start with some introductory PHP tutorials to understand how PHP works. Or you could hire someone to make his for you.


Edit: looks like bluewalrus posted as I did. That's a good answer. But still, you should start with the basics of PHP.

traq
08-15-2011, 03:18 PM
Not sure if the switch default includes empty so the first two lines maybe removable.

the default case does apply to empty/unset variables. However, you'd still get a warning from php, so it's best to leave the empty() check in place.

Techykid3
08-15-2011, 05:34 PM
Well, I sort of got code from a tutorial, but the problem is, is that it would be: http://website.com/downloads/download.php?1 or http://website.com/downloads/download.php?2

And thats the bad thing, I want to make it http://website.com/downloads/download.php?id=1



<?php
//If is defined URL variable 'aboutme'
if(isset($_GET['spoutclient'])){
// include page about me
include('dl/files/spout/spout.php');
//else if is defined URL variable 'interests'
}else if(isset($_GET['chatclient'])){
// include page interests
include('dl/files/chatclient/chatclient.php');
// in all other cases include the home page
} else {
include('nodownload.php');
}
?>

That code works for http://website.com/downloads/download.php?2, but I want to add the ?id=2 instead.

Techykid3
08-16-2011, 05:07 PM
Bump.

bluewalrus
08-16-2011, 07:56 PM
What tutorial did you use? Are you using a framework, htaccess? The case you presented in your first post should work with the solution presented, please provide more details if it doesn't work.

JShor
08-16-2011, 10:03 PM
I don't understand what you're trying to do. Your code doesn't add up to anything that you're trying to convey to us.

If you want to get the value of a query string, use _GET with the variable of the name set in the query.

For example:


<?php echo $_GET['id']; ?>


If you type in the URL page.php?id=2, the page should print 2.

Techykid3
08-17-2011, 12:40 PM
Okay, there are things like $name='ChatClient' on the includes page, it does not show any text, it just defines the variables, now on the page with the php scripts (the one in my last post) tells it which page to include depending on the url variable. And the includes define the variables.

And the problem is, I can't find out how to make it http://mysite.com/downloads/download.php?id=chat, instead of ?chat...

JShor
08-17-2011, 01:29 PM
I just explained how--Nevermind. This thread is clearly hopeless.