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?
Standard C specifies that a switch can have at least 257 case statements.
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.
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.
$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 />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With