Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    Regular Coder
    Join Date
    Aug 2012
    Posts
    142
    Thanks
    39
    Thanked 3 Times in 3 Posts

    Simplifying a query for a menu list

    I've successfully created a working menu from my page database. I'm using categories, groups, and menu position to sort out the menu. All is working fine but as you can see from the code below, there's a ton of duplicate code below. The query runs again and again and if statements sort out the data.

    Working Code:
    PHP Code:
    <h2>Main Pages</h2>
        <ul>
          <?php 
    // =============== query content page records ============
    $result mysql_query("select * FROM siteContent ORDER BY id DESC");
    if (
    mysql_num_rows($result) > 0) {
        while (
    $row mysql_fetch_assoc($result)) {
            
    $pageCategory $row['pageCategory'];
            
    $menuPosition $row['menuPos'];
            
    $pageGroup $row['menuGroup'];
            if (
    $row['approval'] == 'y'
            
    && $menuPosition == 'Main'
            
    && $pageCategory == 'Visitors') {
            
    // ======== create a link  ===============
            
    $link '<a href="'.$row['linkName'].'?recordID='$row['id']. '">'$row['linkName']. '</a>';
             echo 
    '<li>'$link'</li>' ;
                }
            }
        }
        
    ?>
    </ul>

    <!--Visitor Accomodations Pages -->
    <h2>Accomodations</h2>
    <ul> 
    <?php 
    // =============== query content page records ============
    $result mysql_query("select * FROM siteContent ORDER BY linkName");
    if (
    mysql_num_rows($result) > 0) {
        while (
    $row mysql_fetch_assoc($result)) {
            
    $pageCategory $row['pageCategory'];
            
    $menuPosition $row['menuPos'];
            
    $pageGroup $row['menuGroup'];
            if (
    $row['approval'] == 'y' 
            
    && $pageCategory == 'Visitors' 
            
    && $pageGroup == 'Accommodations'
            
    && $menuPosition == 'Sub') {
            
    // ======== create a link  ===============
            
    $link '<a href="'.$row['linkName'].'?recordID='$row['id']. '">'$row['linkName']. '</a>';
             echo 
    '<li />'$link;
            }
        }
    }
        
    ?>
    <ul> 
    <?php 
    // =============== query content page records ============
    $result mysql_query("select * FROM siteContent ORDER BY linkName");
    if (
    mysql_num_rows($result) > 0) {
        while (
    $row mysql_fetch_assoc($result)) {
            
    $pageCategory $row['pageCategory'];
            
    $menuPosition $row['menuPos'];
            
    $pageGroup $row['menuGroup'];
            if (
    $row['approval'] == 'y' 
            
    && $pageCategory == 'Visitors' 
            
    && $pageGroup == 'Accommodations'
            
    && $menuPosition == 'Secondary') {
            
    // ======== create a link  ===============
            
    $link '<a href="'.$row['linkName'].'?recordID='$row['id']. '">'$row['linkName']. '</a>';
             echo 
    '<li />'$link;
            }
        }
    }
        
    ?>
    </ul>
    </ul>

    <!--Visitor Entertainment Pages -->
    <h2>Entertainment</h2>
    <ul> 
    <?php 
    // =============== query content page records ============
    $result mysql_query("select * FROM siteContent ORDER BY linkName");
    if (
    mysql_num_rows($result) > 0) {
        while (
    $row mysql_fetch_assoc($result)) {
            
    $pageCategory $row['pageCategory'];
            
    $menuPosition $row['menuPos'];
            
    $pageGroup $row['menuGroup'];
            if (
    $row['approval'] == 'y' 
            
    && $pageCategory == 'Visitors' 
            
    && $pageGroup == 'Entertainment'
            
    && $menuPosition == 'Sub') {
            
    // ======== create a link  ===============
            
    $link '<a href="'.$row['linkName'].'?recordID='$row['id']. '">'$row['linkName']. '</a>';
             echo 
    '<li />'$link;
                }
            }
    }
        
    ?>
    </ul>
    I'd like to run the query once then use for each statements to call up the repeating groups. Every time I try the loop stops after the first for each or first while method.

    I was hoping that something like this would work after the query so that I would only have to run the query once:
    PHP Code:
    if ($row['approval'] == 'y' 
            
    && $pageCategory == 'Visitors' 
            
    && $pageGroup == 'Accommodations'
            
    && $menuPosition == 'Sub') {
            
    // ======== create a link  ===============
            
    $link '<a href="'.$row['linkName'].'?recordID='$row['id']. '">'$row['linkName']. '</a>';
             foreach (
    $link as $v) {
                    echo 
    "\$link[$i] => $v.\n";
                    
    $i++;
                            }
            }
                    
    // ============ Next Group ===============
    if ($row['approval'] == 'y' 
            
    && $pageCategory == 'Visitors' 
            
    && $pageGroup == 'Entertainment'
            
    && $menuPosition == 'Sub') {
            
    // ======== create a link  ===============
            
    $link '<a href="'.$row['linkName'].'?recordID='$row['id']. '">'$row['linkName']. '</a>';
             foreach (
    $link as $v) {
                    echo 
    "\$link[$i] => $v.\n";
                    
    $i++;
                            }
            } 
    Any ideas on how I could simplify this code would be appreciated. There are 40 groups and 5 classes in the menu and it's growing....
    Last edited by rgEffects; 01-01-2013 at 06:09 PM.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •