I have a working menu for my dynamic pdo based site. All the links work in regards to displaying data, but the issue is that all of the menu links are shown - seven in all. What I would like to do is have three menu links on the top, and the remaining four in the footer. I am using to database tables - topmenu and footermenu.

This is the current code for the index.page:

<?php require_once("db.php");
// top nav menu
    $data = array();
	$sql = $conn->query("SELECT `id`, `pageheader`, `pagetitle`, `pagecontent` FROM topmenu ORDER BY id ASC");

$data = $sql->fetchAll(PDO::FETCH_UNIQUE);

$id = (isset($_GET['id']) && array_key_exists($_GET['id'],$data) ? $_GET['id'] : 1);

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>test</title>
    <link href="css/pagestyles.css" rel="stylesheet">
</head>
<body>
<?php include("includes/top.php") ?>

<nav class="nav">
   <span class="nav__list">
   <?php

foreach($data as $rid => $row):
	echo '<a href="index.php?id='.$rid.'">'.$row['pageheader'].'</a>';
endforeach;

?>
   </span>
</nav>

<hr>

<h1 class="center"><?php echo $data[$id]['pagetitle']; ?></h1>

<hr>

<?php echo $data[$id]['pagecontent']; ?>


<div class="space"></div>

<!-- footer content -->
<?php include('includes/footer.php'); ?>
</body>
</html>

footer: <footer>Email: admin@######.com | ####### &copy; 

<?php echo date('Y'); ?>. All rights reserved. 

<small>
    <?php

foreach($data as $rid => $row):
	echo '<a href="index.php?pageheader='.$rid.'">'.$row['pageheader'].'</a>';
endforeach;
?>
</small>

</footer>

The last section is in a include: <?php include('includes/footer.php'); ?> is this helps.

I would like to know how to separate the menu items but don't know how.

Edited your post to use [code]...[/code] tags around your code block. (Unfortunately the markup that the </> button in the editor window uses is not really sufficient for anything more than a bit of in-line text. 🙁 )

    steveb63 I am using to database tables - topmenu and footermenu

    So it sounds to me like you need a separate DB query to populate the footer menu?

      Thanks for the info. I have created a new table in the database called footermenu with footerheader, footertitle, and footercontent containing test data.

      May I ask you to show me how to add a separate query. If you can, if will be appreciated.

        Should be able to do the same stuff you did for the top menu:

        // top nav menu
            $data = array();
        	$sql = $conn->query("SELECT `id`, `pageheader`, `pagetitle`, `pagecontent` FROM topmenu ORDER BY id ASC");
        
        $data = $sql->fetchAll(PDO::FETCH_UNIQUE);
        

        Just change the query to use the applicable table and column names for the different table, and maybe assign the results to a different variable such as $footer_data instead of $data (so that it's obvious what it's for), then use $footer_data for the loop when you do the footer menu.

        Hello. Unless I misread or mistyped something, the links from the second table, link to the first table so instead of having some test text appear when footer men link 1 is selected, the page title and content from topmenu appears.

        index.php:

        <?php require_once("db.php");
        // top nav
            $data = array();
        	$sql = $conn->query("SELECT `id`, `pageheader`, `pagetitle`, `pagecontent` FROM topmenu ORDER BY id ASC");
        
        $data = $sql->fetchAll(PDO::FETCH_UNIQUE);
        
        $id = (isset($_GET['id']) && array_key_exists($_GET['id'],$data) ? $_GET['id'] : 1);
        
        //footer nav
            $footerdata = array();
        	$footersql = $conn->query("SELECT `id`, `footheader`, `foottitle`, `footcontent` FROM footermenu ORDER BY id ASC");
        
        $footerdata = $footersql->fetchAll(PDO::FETCH_UNIQUE);
        
        $fid = (isset($_GET['id']) && array_key_exists($_GET['id'],$footerdata) ? $_GET['id'] : 1);
        ?>
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <title>test</title>
            <link href="css/pagestyles.css" rel="stylesheet">
        </head>
        <body>
        <?php include("includes/top.php") ?>
        
        <nav class="nav">
           <span class="nav__list">
           <?php
        
        foreach($data as $rid => $row):
        	echo '<a href="index.php?id='.$rid.'">'.$row['pageheader'].'</a>';
        endforeach;
        
        ?>
           </span>
        </nav>
        
        <hr>
        
        <h1 class="center"><?php echo $data[$id]['pagetitle']; ?></h1>
        
        <hr>
        
        <?php echo $data[$id]['pagecontent']; ?>
        
        
        <div class="space"></div>
        
        <!-- footer content -->
        <?php include('includes/footer.php'); ?>
        </body>
        </html>
        

        footer.php:

        <?php echo date('Y'); ?>. All rights reserved. 
        
        <small>
            <?php
        
        foreach($footerdata as $frid => $frow):
        	echo '<a href="index.php?id='.$frid.'">'.$frow['footheader'].'</a>';
        endforeach;
        ?>
        </small>
        

        What have I done wrong?

          	echo '<a href="index.php?id='.$frid.'">'.$frow['footheader'].'</a>';
          

          Shouldn't $frid be $frow['id']? And similarly for $rid in the other list? The array keys in the first just by coincidence match the id fields in the table rows.

          I wouldn't have gone to the bother of making a whole 'nother table and copy-pasted code for this. I would have just selected all the links, put them in an array, and then show the first three (elements 0,1,2) at the top and the other four (elements 3,4,5,6) at the bottom.

            Write a Reply...