3

I was writing a Site in php the last few weeks and always had a question in my mind. On my index.php I route all the templates file like this

    if(isset($_GET['search'])){
        include_once 'template/template.search.php';
    }
    elseif(isset($_GET['newsletter'])){
        include_once 'template/template.newsletter.php';
    }
    elseif(isset($_GET['product'])){
        include_once 'template/template.product.php';
    }
    elseif(isset($_GET['categories'])){
        include_once 'template/template.categorie.php';
    }
    elseif(isset($_GET['about'])){
        include_once 'template/template.about.php';
    }
    elseif(isset($_GET['sitemap'])){
        include_once 'template/template.sitemap.php';
    }
    else
    {   
        include_once 'template/template.index.php';     
    }

But for me it dosen't look very clean. Is there a better possibility to handle a job like this ?

I already tried it like this, but didn't worked out for me

    $i = 0 ;
    switch($i){
    case(isset($_GET['search'])):
        include_once 'template/template.search.php';
        break;
    default:
        include_once 'template/template.index.php'; 
        break;
}

Edit: Better writing in the title was a bit misleading some of you, so I'm searching for the best performance for sure.

14
  • 2
    switch statement would solve this Commented Jan 11, 2013 at 18:46
  • 1
    you could use switch ca2.php.net/manual/en/control-structures.switch.php Commented Jan 11, 2013 at 18:47
  • 2
    Another option might be to look at routing with something like .htaccess Commented Jan 11, 2013 at 18:48
  • 1
    @ithcy you are spamming the crap out of this page with no answers or suggestions of your own. Are you trolling questions only to discredit those trying to help or are you actually planning on providing something constructive (like an actual answer) to the OP's Q. You seem to know the best solution but you choose not to contribute. Commented Jan 11, 2013 at 19:08
  • 1
    @ithcy I agree my comment is ridiculous, it wasn't well thought out and I jumped-the-gun on contributing. Once realized, I stopped and upvoted the answer I believe is best. I didn't spam all of the other comments/answers. I'm just tired of the common dev attitude of "piss on you I'm the best and I refuse to help such trivial questions, but I'll undoubtedly spend my time telling you how much you suck at what I do." Commented Jan 11, 2013 at 19:18

6 Answers 6

11

How about this?

$templates = array('search',
                   'newsletter',
                   'product',
                   'categories',
                   'about',
                   'sitemap',
                   'index');

foreach ($templates as $template)
{
    if (isset($_GET[$template]))
    {
        include_once "template/template.$template.php";
        break;
    }
}

You really should specify an array of valid templates—it's much safer.

I guess an alternative is to search the other way around:

$templates = array('search',
                   'newsletter',
                   'product',
                   'categories',
                   'about',
                   'sitemap',
                   'index');

foreach ($_GET as $key => $val)
{
    if (in_array($key, $templates))
    {
        include_once "template/template.$key.php";
        break;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Probably want to add a break after include_once.
@alfasin - I actually didn't see your answer as I wrote mine, but a +1 deserved and returned to you nonetheless.
4

Assuming $_GET holds only the includes you can do:

foreach ($_GET as $key => $val){
    if(isset($key)){
        include_once 'template/template.'.$val.'.php';
        break;
    }
}

15 Comments

what if $val is an array?
@RPM I don't understand your question: $_GET is indeed an array
I meant what if $val is an array?
Also, his values are not ALWAYS in the file name. See 'categories' for example.
more importantly, what if more than one of the $_GET keys exist? Then all of those files will be included.
|
3

It's not too much cleaner, but a switch is the best thing to use for this. Readability is about 15,000% better.

switch(true) {
    case isset($_GET['search']):
        include_once 'template/template.search.php';
        break;

    // do more

    default:
        include_once 'template/template.index.php';
        break;
}

4 Comments

I disagree. In this specific case, the switch reduces readability.
I agree with itchy, and if I were the OP I would choose to accept @acheong87's answer!
Most of the answers here a reasonable, however I believe using a loop in such a manner assumes that the key of the get variable will match a part of the file name religiously. Since we try not to localize answers here on SO, I chose a form that would match another curious user's needs.
@phpisuber01 I would discard the switch and enforce consistency between page selector and actual page names especially thinking of a curious beginner wandering on this page. Code readability and minimal data redundency are two principles that you can't learn too soon, in my opinion.
2

Just a few passing thoughts...

  • performance here is not an issue at all.
    Even a serie of ifs withouth elses would be executed in a few microseconds.

Code maintenability and robustness is an issue, though.

I find the slightly hackish use of switch somewhat unsatisfactory or even dangerous, because

  • the important information (the GET index and the actual page name) is still duplicated and burried in a glob of repetitive code,
  • the switch will behave oddly if two conditions happen to be true simultaneously (I suppose it will take the first satisfied condition, but still that's not very clean)
  • adding or removing a page still requires to duplicate/erase 3 or 4 lines of code, and inadvertently duplicating a block might go unnoticed and put the programmer in the annoying "I'm sure this matter was settled (but it actually wasn't)" situation.

As a matter of fact, I find the interface a bit strange. Passing a single "target" variable that would enumerate the possible pages seems more consistent to me. That would eliminate the odd cases where two page selection flags would be set simultaneously.

You can then have a simple list of targets and either compute the name of the target pages or have them stored in an assoc array (if you really can't name them in a consistent way, although I wonder what strange requirement would prevent you from doing that).

The key points I would consider for robustness are

  • no data duplication
  • no "lazy" default case (unless the default value comes from a functional requirement)

For all these reasons, I would change the way the page is selected, like so:

$pages = array (                  // single data source
    "search",
    "newsletter",
    // etc...
    );
@$page = $pages=[$_GET["page"]];  // single page selector
if (!$page) $page = "index";      // explicit default case

include "template/template.$page.php"; // no reason to include only once

// if someone else happens to include the template, better have the page
// break down immediately and correct the problem than letting a piece of
// buggy code live happily somewhere in your scripts

Comments

0

You can use an array: if you find the key inside, use it, otherwise just use default :

<?php
$tpl = array(
'search' => 'template/template.search.php',
'newsletter' => 'template/template.newsletter.php',
'product' => 'template/template.product.php'
#...
);

foreach($_GET as $get){
   if(array_key_exists($get, $tpl)) include_once($tpl[$get]); // assuming search is within $get
}

?>

Comments

0

You could use a variable (like t) in the query string to indicate the template you want to use, and then just include the template name dynamically based on that. So if your URL looks something like: mysite.com/page.php?t=newsletter&blah=1&..., then all you need to do is:

include_once('template/template.' . $_GET['t'] . '.php');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.