Using three dropdown boxes to show data

I have a web page with a dropdown menu that when selected the information is shown on the page. However, the list is growing as I add more content. What I now need is three dropdown menus, for example, header, sub header, and sub sub header. Then have the selected data shown on one page.

I am able to copy and paste the same dropdown menu, but this is not what I need. I have tried three tables and changing the variables to match the tables but this means adding the <?php ?> code twice e.g. <?php echo header ?><?php echo sub header ?> which displays both sets of data.

How do I have three dropdown menus, using data from three tables displayed on one page?

  1. Navigation code:

     <div class="container" id="contents">
     		
     		<div class="row">
     			
     			<div class="col-md-4 box">
     				
     				<div>dropdown box 1</div>
     				
     			</div><!-- col end: -->
     			
     			<div class="col-md-4 box">
     				
     				<div class="dropdown">
     					
     					<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
     					Select Option
     					</button>
     				
     					<div class="dropdown-menu text-center" aria-labelledby="dropdownMenuButton">
     					
     					<?php 
     					
     					$q = "SELECT * FROM pages";
     					$r = mysqli_query($dbc, $q);
     					
     					while($nav = mysqli_fetch_assoc($r)) {
     							
     					echo '<a class="dropdown-item box" href="?page='.$nav['id'].'">'.$nav['title'].'</a>';
     					
     					}
     					
     					?>
     					</div><!-- dropdown menu end: -->
     					
     				</div><!-- dropdown end: -->
     				
     			</div><!-- col end: -->
     			
     			<div class="col-md-4 box">
     				
     				<div>Dropdown box 3</div>
     				
     			</div><!-- col end: -->
     			
     		</div><!-- row end: -->
     	
     	</div><!-- contents container end: -->
    
  2. The set up code:

    function links

    include(‘functions/data.php’); // for page set up

     # page setup
     if(isset($_GET['page'])) {
     	
     	$pageid = $_GET['page']; // set $pageid to equal the value to the url
     	
     } else {
     	
     	$pageid = 1; // set $pageid equal to 1 or the home page
     	
     }
    
     $page = data_page($dbc, $pageid); // see data.php: $pageid = $id
    
     # page set up end
    
  3. The function code:

     <?php
    
     # function to display all pages from database
     function data_page($dbc, $id) {
     	
     	$q = "SELECT * FROM pages WHERE id = $id"; // id was $Pageid
     	$r = mysqli_query($dbc, $q);
     	
     	$data = mysqli_fetch_assoc($r);
     	
     	return $data;
     	
     }
     # function end
    
     ?>
    
  4. The body code:

       <div class="col-md-2">
            <h4><strong><?php echo $page['slug']; ?></strong></h4>
        </div><!-- col end: -->
        
        <div class="col-md-4"> 
  		<h4 class="text-justify"><?php echo $page['body']; ?></h4>
        </div><!-- col middle end: -->
        
        <div class="col-md-3">
            <h4>Facts</h4>
            <h5 class="text-justify"><?php echo $page['facts']; ?></h5>
        </div><!-- col end: -->
        
 		<div class="col-md-3" id="sidebar">
            <h5 class="text-justify"><?php echo $page['sidebar']; ?></h5>
        </div><!-- col right end: -->
        
   </div><!-- row end: -->

I am new to php so need any advice to be ‘an idiots’ guide type.

Thanks in advance for any help.

What do you mean with “tables”? I only see one mysql table you are getting data from, and i do not see where the data is actually devided into header, subheader and subsubheader (and you may have to think about the naming).

Sponsor our Newsletter | Privacy Policy | Terms of Service