Loading include using if else

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aussiewebgirl
Forum Newbie
Posts: 6
Joined: Wed Mar 03, 2010 8:38 pm

Loading include using if else

Post by aussiewebgirl »

Hi,

I have a web page which loads an include file

Code: Select all

<?php include('includes/hosts-a.html'); ?>
.

On that page I also have links A - Z and when each one is clicked I want to display the other include files hosts-b.html to hosts-z.html

I think I need to use an if statement but how do I use the link to load the relevant include file - ie they click on the B link and hosts-b.html is displayed in place of where the include hosts-a.html was? The web page is here if needed http://www.hgtsa.com.au/hostemployers/index.php

Would it go something like

Code: Select all

if ( $link_variable == b ) {
	include('includes/hosts-b.html'); 
} 
I don't think I'm any where near what I need to be doing. Confusion reigns supreme... Any help much appreciated.

Thanks...
phu
Forum Commoner
Posts: 61
Joined: Tue Mar 30, 2010 6:18 pm

Re: Loading include using if else

Post by phu »

I wouldn't do it manually. Something with a regex match would be the most efficient (from a programming and maintenance standpoint, anyway) way to accomplish this...

Code: Select all

$regex = "/^[a-z]{1}$/";
$matches = array();
preg_match($regex, $link_variable, $matches);

if  (count($matches))
{
    $filename = "includes/hosts-{$matches[0]}.html";
}

if (isset($filename) && is_file($filename))
{
    include($filename);
}
else
{
    // error handling here
}
This has only had cursory testing, but should match hosts-[a-z].html and include the proper file, if it exists, given $link_variable.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Loading include using if else

Post by requinix »

That's a... complicated solution.

Code: Select all

$link_variable = substr($link_variable . "0", 0, 1);
$path = dirname(__FILE__) . "/include/hosts-{$link_variable}.html";
if ($link_variable >= "a" && $link_variable <= "z" && is_file($path)) {
    include $path;
}
aussiewebgirl
Forum Newbie
Posts: 6
Joined: Wed Mar 03, 2010 8:38 pm

Re: Loading include using if else

Post by aussiewebgirl »

Thank you for the reply. No wonder I couldn't get it working - I wasn't even at the right station let alone being on the right track.

As a complete newbie with just basic experience this code is a tad over my head.

Do I just add this in to my page where I currently have <?php include('includes/hosts-a.html'); ?>?

Is there anything else I need to do with my page elsewhere eg what do I do with the navigation links which are currently pointing to 26 separate pages <a href="../hostemployers/hostemployers-b.php">B</a> each with its own include.

Thanks again :)
aussiewebgirl
Forum Newbie
Posts: 6
Joined: Wed Mar 03, 2010 8:38 pm

Re: Loading include using if else

Post by aussiewebgirl »

tasairas - thank you as well for your help with this. I thought the code would be easier than this and both replies are way out of my depth. Is there any chance you could elaborate for me on your code on what to put in my links - currently <a href="../hostemployers/hostemployers-b.php">B</a> but I want these to load the includes instead of going to a separate page?

Thanks again.
phu
Forum Commoner
Posts: 61
Joined: Tue Mar 30, 2010 6:18 pm

Re: Loading include using if else

Post by phu »

tasairis wrote:That's a... complicated solution.

Code: Select all

$link_variable = substr($link_variable . "0", 0, 1);
$path = dirname(__FILE__) . "/include/hosts-{$link_variable}.html";
if ($link_variable >= "a" && $link_variable <= "z" && is_file($path)) {
    include $path;
}
That's fine if you want to make assumptions about the input and abandon error handling, which are common problems in the PHP community... personally, I would rather write something more "complicated" that handles input without modifying it and provides accurate error handling so it can be debugged and monitored correctly.

I would not call anything using integer operations to compare strings arbitrarily concatenated to ignore the actual input good practice or even suitable for production. This is inaccurate and basically an incorrect result by design.
aussiewebgirl
Forum Newbie
Posts: 6
Joined: Wed Mar 03, 2010 8:38 pm

Re: Loading include using if else

Post by aussiewebgirl »

Oh please don't argue on my post... (everyone in my office has been arguing all morning - must be the full moon last night) :(

Both codes are way over my head anyway.

phu I'm quite happy to use yours if you can help me with implementation a bit more....
phu
Forum Commoner
Posts: 61
Joined: Tue Mar 30, 2010 6:18 pm

Re: Loading include using if else

Post by phu »

Heh, sorry... my code should work as it is. I have to hit the hay; too many responsibilities to wake up late. ;)

If you can use the forum PM function it should alert me via email tomorrow. Otherwise I'll be handling client work.
Post Reply