简体   繁体   中英

Get list of cases in switch statement

I use a PHP Switch statement to determine the pages of my website. So here is an example:

switch($page) {
    case "about":
        $title_name = "About Us";
        $page_content = "includes/about-us.php";
        include("inner.php");
    break;
    case "services":
        $title_name = "Services";
        $page_content = "includes/services.php";
        include("inner.php");
    break;
}

And my file structure is index.php?page=about which converts to /about/ using htaccess.

What I want to do is take all of my pages in that switch statement and automatically grab it and put it in a list so I can automatically have it write to my footer page where I will have all of the links.

So instead of manually typing in all of the links in the footer like: Home | About Us | Services | FAQ , it will pull it automatically based on the pages I provided in the Switch statement.

Is there a way to do this? It would also be good to automatically be able to add new pages and it will add a new case for the new page, and automatically create the page in the includes folder.

If anyone can point me in the right direction I would really appreciate it. From my understanding, I don't believe you can do this with a switch statement, I would have to re-work the way I call the pages, right?

$pages = array('about'=> 'About Us', 'services' => 'Services');

if (array_key_exists($page, $pages)) {
   $title_name = $pages[$page];
   $page_content = "includes/$page.php";
   include('inner.php');
}

For your footer you can just iterate over the list of pages. To add a new page, just add it to the array and create the corresponding file.

But to answer your question: No, you can't analyze code-statements during runtime.

Nope, it's not possible using switch - but you could store those informations in an array:

$page_list = array(
    'about' => array(
        'title' => 'About Us',
        'content' => 'includes/about-us.php',
    ),
    'services' => array(
        'title' => 'Services',
        'content' => 'includes/services.php',
    ),
);

if(isset($page_list[$page])) {
    $page_info = $page_list[$page];

    $title_name = $page_info['title'];
    $page_content = $page_info['content'];

    include("inner.php");
} else {
    // 404 - file not found
}

// create links
foreach($page_list as $link_name => $page_ent) {
    echo "<a href=\"/{$link_name}/\">{$page_ent['title']}</a><br />"
}

// output
// <a href="/about/">About Us</a><br />
// <a href="/services/">Services</a><br />

You have this backwards. What you want is to have an array of the pages, and then loop over it once for the switch statement and once for the footer. Or, better yet, get rid of the switch statement altogether and instead use an associative array mapping pages to the info you might need to construct that particular page (there's a lot of common behavior between the switches, so knowing what your page is, you can likely just construct the proper URLs/etc from that).

You definitely can't get all values from a switch statement, you will have to redesign your code. Of course there are many ways to achieve this, but I normally do something like the below, which is both shorter and easier to extend than your method.

<?php

$pages = array('home', 'about', 'services', 'faq');
$titles = array('Home', 'About us', 'Services', 'FAQ');

$index = array_search($_POST['p'], $pages);
if ($index !== false) {
   $page_content = 'includes/' . $pages[$index] . '.php';
   $title_name = $titles[$index];
} else {
   print 'Page not found';
}

?>

Hope this helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM