0

I am trying to make a nav bar for a website. I have to use a foreach loop to display the different pages. However, whenever I try to display them they do not link the pages and they show the wrong text.. Here is the code:

<?php
//Main Navigation Function
function main_nav(){
$nav_sections = array("home"=>"","products"=>"products/","services"=>"services/",
                     "about"=>"about/", "contact"=>"contact/");
$output = "<UL>";
$i=0;

foreach($nav_sections as $nav){
    $output .= "<LI><A href=" . URL_ROOT . $nav . "></A>" . $nav . "</LI>";


}
 echo $output;
}
?>

URL_ROOT is a constant defined that gives the root URL, however it is not linking the pages through the text. This is what the site currently displays: http://tomcat.cit.iupui.edu/gaddough/cit21500/assignment4/

1
  • 1
    The a tag is empty... Commented Oct 20, 2013 at 1:45

1 Answer 1

1

You've (correctly) set up your array as key-value pairs, meaning you'll have to loop through it as such:

foreach($nav_sections as $title => $link) {
    $output .= "<li><a href=" . URL_ROOT . $link . ">" . ucfirst($title) . "</a></li>" . PHP_EOL;
}

ucfirst() makes the first letter of the word or sentence uppercase.

Furthermore, in your current loop you open and close the anchor (link) without assigning any text to it (i.e. there's nothing between <a href...> and </a>.

Last points: you'll want to echo the $output either between <ul> or <ol> tags, and your counter $i is superfluous in this particular snippet.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! the $i variable was just there from me messing around trying to figure it out, haha. I got that part working fine. Now when I click the link to go to the specific pages it gives me an error: Warning: require(config.php): failed to open stream: No such file or directory in /home/gaddough/cit21500/assignment4/products/index.php on line 4 Warning: require(config.php): failed to open stream: No such file or directory in /home/gaddough/cit21500/assignment4/products/index.php on line 4
It seems its looking inside the sub folder for the config.php file, which is out in my main root folder. Any idea how to fix that? code: <?php //products index file require('config.php'); $page_title = "Products"; $output = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. .... include('/home/gaddough/cit21500/assignment4/views/template.php'); ?>
Well, it does depend a bit on how and where you are including this config.php; the most straightforward manner is to simply alter the include path, e.g. from require('config.php') to require('../config.php'), but depending on your project structure this can easily become a bit unmaintainable. A "better" approach would probably be to have one page handling the inclusion of both this config and your child pages, e.g. index.php has a menu and a switch($page) that includes 'pages/some_page.php'.

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.