Forum Moderators: coopster

Message Too Old, No Replies

Pass Variable in URL to Display Word/Phrase on the Page?

Can Display Word/Phrase w/ Variable but Want to Indicate a Default

         

clearpixel

7:12 pm on Jun 5, 2006 (gmt 0)

10+ Year Member



I am using the code below to have a variable in the URL render equivalent text on the actual page (e.g., index.php?domain=webmasterworld.com would make webmasterworld.com display on the page).

However, I am using this on a page that could be accessed without any variables defined in the URL, so how can I 1.) Modify the code below to indicate a default or 2.) Have no text display at all? I've tried adding a clause "or die("File not found!")" (no quotes in the code, just added here for readability), but it doesn't really work. Thanks!

<?php
$domain = $_GET['domain'];
echo "the domain is: $domain";
?>

PS: There is a parallel thread to this one located at [webmasterworld.com...]

dreamcatcher

8:37 pm on Jun 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?php
$domain = $_GET['domain'];
if ($domain)
{
echo "the domain is: $domain";
}
else
{
echo 'No domain found':
}
?>

Or was that not what you wanted? You could use a header redirect if nothing is found and direct to another page, or load an include file. Anything like that really.

dc

clearpixel

9:31 pm on Jun 5, 2006 (gmt 0)

10+ Year Member



Tried that, but am getting this error message:

Parse error: parse error, unexpected ':', expecting ',' or ';'

eelixduppy

9:32 pm on Jun 5, 2006 (gmt 0)



change the following:
echo 'No domain found':
to
echo 'No domain found';

It had a colon at the end; not a semi-colon(; )

dreamcatcher

9:55 pm on Jun 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oops. Thanks eelixduppy.

dc

clearpixel

12:52 am on Jun 6, 2006 (gmt 0)

10+ Year Member



So how could I modify the code below so that if the variable is one of two options the variable will or won't appear? For example, if I just want to create a scenario by which if the variable is "?domain=y" then the variable (a fixed value identified in the code) would appear, but if the variable is "?domain=n" it won't appear?

<?php
$domain = $_GET['domain'];
if ($domain)
{
echo "this is the text that would appear";
}
else
{
echo ' ';
}
?>

PS: Thank you both for you help; the code works perfectly as originally requested.

eelixduppy

12:58 am on Jun 6, 2006 (gmt 0)



Like this?

<?php
$domain = $_GET['domain'];
if ($domain == "y")
{
echo "this is the text that would appear";
}
?>

whoisgregg

1:36 pm on Jun 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Time for a switch [php.net]?

switch($_GET['domain']){
case 'xyz':
echo 'this domain is xyz';
break;
case 'abc':
echo 'this domain is abc';
break;
default:
echo 'this domain is default';
break;
}