Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 982
Drew Avatar asked Jul 30 '11 16:07

Drew


People also ask

How many cases are in a switch statement?

Standard C specifies that a switch can have at least 257 case statements.

Can you have multiple cases in a switch statement?

As per the above syntax, switch statement contains an expression or literal value. An expression will return a value when evaluated. The switch can includes multiple cases where each case represents a particular value.

Can we check condition in switch case?

Switch Case In C In a switch statement, we pass a variable holding a value in the statement. If the condition is matching to the value, the code under the condition is executed. The condition is represented by a keyword case, followed by the value which can be a character or an integer. After this, there is a colon.


2 Answers

$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.

like image 107
KingCrunch Avatar answered Oct 22 '22 13:10

KingCrunch


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 />
like image 4
vstm Avatar answered Oct 22 '22 11:10

vstm