Web Hosting Talk







View Full Version : PHP Help


ePlanetDesign
09-10-2007, 03:48 PM
Hi,

Can someone please show me how to condense, or do this a better way? It works but there has to be a better way.

I'm adding an image/content to certain pages only.

Thanks



<? if( basename( $_SERVER['PHP_SELF'] ) == 'welcome.php'
or basename( $_SERVER['PHP_SELF'] ) == 'support.php'
or basename( $_SERVER['PHP_SELF'] ) == 'cart.php'
or basename( $_SERVER['PHP_SELF'] ) == 'sellhome.php'
or basename( $_SERVER['PHP_SELF'] ) == 'editprofile.php'
or basename( $_SERVER['PHP_SELF'] ) == 'subacctmgmt.php'
or basename( $_SERVER['PHP_SELF'] ) == 'profilemgmt.php'
or basename( $_SERVER['PHP_SELF'] ) == 'affiliate.php'
or basename( $_SERVER['PHP_SELF'] ) == 'acctrefill.php'
or basename( $_SERVER['PHP_SELF'] ) == 'news.php'
or basename( $_SERVER['PHP_SELF'] ) == 'kb.php'
or basename( $_SERVER['PHP_SELF'] ) == 'imanage.php'
or basename( $_SERVER['PHP_SELF'] ) == 'manage.php'
or basename( $_SERVER['PHP_SELF'] ) == 'omanage.php'
or basename( $_SERVER['PHP_SELF'] ) == 'imanage.php'
or basename( $_SERVER['PHP_SELF'] ) == 'smanage.php'
or basename( $_SERVER['PHP_SELF'] ) == 'hmanage.php'
or basename( $_SERVER['PHP_SELF'] ) == 'download.php'
or basename( $_SERVER['PHP_SELF'] ) == 'downloada.php'
or basename( $_SERVER['PHP_SELF'] ) == 'journal.php'
or basename( $_SERVER['PHP_SELF'] ) == 'history.php'
or basename( $_SERVER['PHP_SELF'] ) == 'emailarchive.php'
or basename( $_SERVER['PHP_SELF'] ) == 'sellhome.php'){ ?>

MountedWeb
09-10-2007, 03:52 PM
I think that is very nice , compared to If Statement.

RBBOT
09-10-2007, 04:27 PM
You could use the in_array function:

http://www.php.net/manual/en/function.in-array.php

Annex
09-10-2007, 05:52 PM
<?
$pages = array('welcome.php','support.php','cart.php','sellhome.php' ,'editprofile.php','subacctmgmt.php','profilemgmt.php' ,'affiliate.php','acctrefill.php','news.php','kb.php','imanage.php','manage.php','omanage.php' ,'imanage.php','smanage.php','hmanage.php' ,'download.php','downloada.php','journal.php' ,'history.php','emailarchive.php','sellhome.php');

if (in_array(basename($_SERVER['PHP_SELF']), $pages)){
//code goes here
}
?>

I haven't tested it, however it should work.

ADS | Ryan
09-10-2007, 06:12 PM
An array would be overhead. Just use switch and case:


$base = basename($_SERVER['PHP_SELF']);
switch ($base){
case 'welcome.php':
echo 'Welcome Page';
break;
case 'support.php':
echo 'Support Page';
break;

//continue to the end
}



http://www.tizag.com/phpT/switch.php

ePlanetDesign
09-10-2007, 08:14 PM
An array would be overhead. Just use switch and case:


$base = basename($_SERVER['PHP_SELF']);
switch ($base){
case 'welcome.php':
echo 'Welcome Page';
break;
case 'support.php':
echo 'Support Page';
break;

//continue to the end
}





Each page will have the exact same content added, wouldn't that make this a bit redundant?

Bangalore Job Mob
09-10-2007, 09:04 PM
What exactly is it you're trying to accomplish and why can't each page produce its own content?

Annex
09-10-2007, 09:42 PM
An array would be overhead. Just use switch and case:


$base = basename($_SERVER['PHP_SELF']);
switch ($base){
case 'welcome.php':
echo 'Welcome Page';
break;
case 'support.php':
echo 'Support Page';
break;

//continue to the end
}



http://www.tizag.com/phpT/switch.php

He wanted to compress the code, not expand.

ePlanetDesign
09-10-2007, 10:10 PM
This is hard to explain.

These pages are template pages for a billing software and could get overwritten by updates to the software. I have many, many of these templates out there (and many more to come) so adding content to each page on every site is not an option. So I'm using an include to add an absolute positioned div to the header file which appears like it's on these pages. (The header file never gets changed/overwritten by updates). The div only shows up if the client is logged in and if he is on one of these pages. The content at the moment is just a "Client Home" link but will eventually be a full menu for the client area only. I did't design these templates so I'm just working with what I have to work with.

sorry about any confusion, I'll just use what I had to start with.

And yes, compress, not expand. The array looks good, I'll try that.

Thank You to all.

isurus
09-11-2007, 02:10 PM
FWIW, the in_array() method given by Annex was the first thing that jumped to mind for me too, but it is slightly slower than using the page name as the key in an associative array.

The code snippet below takes approx 1.5s to execute on my workstation, whilst the in_array version takes approx 6.8s (keeping everything else constant).

On the flip side, this approach will use slightly more memory.

For example: (I've used additional white space for clarity, but it could all be shoved on to the same line if you like)
<?
$div_pages = array( 'welcome.php' => 1,
'support.php' => 1,
'cart.php' => 1,
'sellhome.php' => 1,
'editprofile.php' => 1,
'subacctmgmt.php' => 1,
'profilemgmt.php' => 1,
'affiliate.php' => 1,
'acctrefill.php' => 1,
'news.php' => 1,
'kb.php' => 1,
'imanage.php' => 1,
'manage.php' => 1,
'omanage.php' => 1,
'imanage.php' => 1,
'smanage.php' => 1,
'hmanage.php' => 1,
'download.php' => 1,
'downloada.php' => 1,
'journal.php' => 1,
'history.php' => 1,
'emailarchive.php' => 1,
'sellhome.php' => 1 );

$script_name = strtolower( basename( $_SERVER['PHP_SELF'] ) );

for( $i=0; $i<1000000; $i++ )
{
if( isset( $div_pages[$script_name] ) )
echo "y\n";
else
echo "n\n";
}
?>This will only make much difference if you have some very popular sites.

ePlanetDesign
09-11-2007, 03:57 PM
Thanks, I'll try that.