Page or URL function?

Page or URL function?

am 29.07.2009 23:06:45 von tmiller

I've been searching php.net for a function to do this:

if page_url('browse.php') {

$default =3D "A";

}

$letter =3D isset($_GET['letter'])? $_GET['letter'] :"$default" ;

else
{
=20
$letter =3D isset($_GET['letter'])? $_GET['letter'] :"" ;

}

I want to say if the page is browse, default to the A listings, if the page
is not browse show no default listings (because there is other data on the
page and I don't want this to output)

I thought there was url functions....am I calling it something different?
Terion


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Page or URL function?

am 29.07.2009 23:28:32 von Ben Dunlap

> I've been searching php.net for a function to do this:
>
> if page_url('browse.php') {

The $_SERVER global array has this sort of information. The 'PHP_SELF' key
might be what you want:

http://us.php.net/manual/en/reserved.variables.server.php

But where is the code that needs to know? I'm guessing it's in a common library
file that's going to be included in browse.php as well as other scripts, but am
I guessing rightly?

BTW, you could simplify your code slightly by defining $default as an empty
string before the IF block. Then you only have to read from $_GET once.

$default = "";
if () {
$default = "A";
}
/*
* No need for an "else" any more, now you can just
* set the value of $letter
*/
?>

I'd also suggest using filter_input() rather than reading directly from $_GET:

$letter = filter_input(INPUT_GET, 'letter', , []);
if (empty($letter)) {
$letter = $default;
}
?>

Presumably you'd want to use FILTER_VALIDATE_REGEXP in place of "" and,
for "", pass a regex that ensures that 'letter' is a single
alphabetical character.

You can read about the filter functions here (sort of, the documentation is a
little sparse):

http://us.php.net/manual/en/ref.filter.php

A VALIDATE_REGEXP example is available here:

http://www.w3schools.com/php/filter_validate_regexp.asp

Ben

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Page or URL function?

am 29.07.2009 23:34:28 von Ben Dunlap

Ben Dunlap wrote [TWICE]:
> The $_SERVER global array has this sort of information. The 'PHP_SELF' key
[8<]
> Ben

Very sorry for the double-post. Reply-all in Thunderbird News seems a little
overzealous by default.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Page or URL function? (RESOLVED)

am 30.07.2009 20:31:27 von tmiller

I Figured it out using this:

if ($_SERVER['SCRIPT_FILENAME'] =3D "browse.php" ) { =
$default =3D "A"; =
} =
=
else { $default =3D=
""; } =
=
$letter =3D isset($_GET['letter'])? $_GET['letter'] :"$defaul=
t" ;


On 7/29/09 4:34 PM, "Ben Dunlap" wrote:

Ben Dunlap wrote [TWICE]:
> The $_SERVER global array has this sort of information. The 'PHP_SELF' ke=
y
[8<]
> Ben

Very sorry for the double-post. Reply-all in Thunderbird News seems a littl=
e
overzealous by default.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Page or URL function? (RESOLVED)

am 30.07.2009 21:00:58 von List Manager

Miller, Terion wrote:
> I Figured it out using this:
>
> if ($_SERVER['SCRIPT_FILENAME'] = "browse.php" ) { $default = "A"; } else { $default = ""; } $letter = isset($_GET['letter'])? $_GET['letter'] :"$default" ;
>

unless you are doing more then what you are showing above.

I would do it like this:

if ( $_SERVER['SCRIPT_FILENAME'] = 'browse.php' ) {
if ( isset($_GET['letter']) ) {
$letter = $_GET['letter'];
} else {
$letter = 'A';
}
} else {
$letter = '';
}

Basically, it is the same thing. But it doesn't execute the additional
IF statement when it doesn't need to.

Jim

>
> On 7/29/09 4:34 PM, "Ben Dunlap" wrote:
>
> Ben Dunlap wrote [TWICE]:
>> The $_SERVER global array has this sort of information. The 'PHP_SELF' key
> [8<]
>> Ben
>
> Very sorry for the double-post. Reply-all in Thunderbird News seems a little
> overzealous by default.
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
>
>



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Page or URL function? (RESOLVED)

am 30.07.2009 21:20:08 von Ben Dunlap

Jim Lucas wrote:
> Miller, Terion wrote:
>> I Figured it out using this:
>>
>> if ($_SERVER['SCRIPT_FILENAME'] = "browse.php" ) {
>> $default = "A";
>> } else {
>> $default = "";
>> }
>>
>> $letter = isset($_GET['letter'])? $_GET['letter'] :"$default" ;
>
> unless you are doing more then what you are showing above.
>
> I would do it like this:
>
> if ( $_SERVER['SCRIPT_FILENAME'] = 'browse.php' ) {
> if ( isset($_GET['letter']) ) {
> $letter = $_GET['letter'];
> } else {
> $letter = 'A';
> }
> } else {
> $letter = '';
> }
>
> Basically, it is the same thing. But it doesn't execute the additional
> IF statement when it doesn't need to.

They end up slightly different. In your version, Jim, only the page
'browse.php' will examine the GET-parameter called 'letter'.

In Terion's version, any page with this code in it will examine the 'letter'
parameter.

Either one might be appropriate, depending on the context, but they don't have
quite the same effect.

Ben

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php