简体   繁体   中英

how to skip one item to display from database while loop in php

<ul class="sub-menu">
  <?php 
    $sql = mysql_query("SELECT * FROM tbl_main_menu ORDER BY fld_main_menu_id ASC");
    while($menu_list = mysql_fetch_array($sql)){
  ?>  
  <li>
    <a href="content_editor.php?main_menu_id=<?php echo $menu_list['fld_main_menu_id']; ?>&menu_name=<?php echo $menu_list['fld_main_menu_name'];?>">
      <?php echo $menu_list['fld_main_menu_name']; ?><span class="arrow"></span>
    </a>
  </li>     
  <?php } ?>    
</ul>

Here display menu from database:

  1. Home
  2. About
  3. Board of directors
  4. Management
  5. Products
  6. Claim
  7. Career

But I don't want to display Board of directors from database and I want put a static page link here . Now what I have to do?

You can do like this from html

    <ul class="sub-menu">
       <?php 
          $sql = mysql_query("select * from tbl_main_menu order by fld_main_menu_id asc");
          while($menu_list = mysql_fetch_array($sql)){
       ?>  

          <?php if ($menu_list['fld_main_menu_name']!='Board of directors') { // check if name is Board of directors ?>
          <li>
          <a href="content_editor.php?main_menu_id=<?php echo $menu_list['fld_main_menu_id']; ?>&menu_name=<?php echo $menu_list['fld_main_menu_name'];?>">
             <?php echo $menu_list['fld_main_menu_name']; ?><span class="arrow"></span>
          </a>
          </li>           
        <?php } //end of if 
          } //end of while ?>      
    </ul>

And you can do it like this from database:

$sql = mysql_query("select * from tbl_main_menu where fild_main_menu_name != 'Board of directors' order by fld_main_menu_id asc");

You can do it with some if statements in your PHP, but while your MySQL is right there why not modify that like this:

<ul class="sub-menu">
    <!-- This is a static page -->
    <li><a href="static_page.html">Static Page</a></li>    
    <?php 
        $sql = mysql_query("SELECT * FROM tbl_main_menu WERE fld_main_menu_name != 'Board of directors' ORDER BY fld_main_menu_id asc");
        while($menu_list = mysql_fetch_array($sql)){
    ?>  
    <li>
        <a href="content_editor.php?main_menu_id=<?php echo $menu_list['fld_main_menu_id']; ?>&menu_name=<?php echo $menu_list['fld_main_menu_name'];?>">
         <?php echo $menu_list['fld_main_menu_name']; ?><span class="arrow"></span>
         </a>
    </li>           


    <?php } ?>

    <!-- This is a static page -->
    <li><a href="static_page.html">Static Page</a></li>    
</ul>

In either method you'll need to make sure the spelling and case of the link name is exact.

Also note I included static links at the beginning and end for you as an example.

Edit: Here's how I would do it based on the comments below:

<ul class="sub-menu">   
    <?php 
        $sql = mysql_query("SELECT * FROM tbl_main_menu ORDER BY fld_main_menu_id asc");
        while($menu_list = mysql_fetch_array($sql)){
    ?>
    <?php if('fld_main_menu_name' != 'board of directors'){?>
    <li>
        <a href="content_editor.php?main_menu_id=<?php echo $menu_list['fld_main_menu_id']; ?>&menu_name=<?php echo $menu_list['fld_main_menu_name'];?>">
         <?php echo $menu_list['fld_main_menu_name']; ?><span class="arrow"></span>
         </a>
    </li>
    <?php } else { ?>
    <li>
        <a href="custom_page_path.php">Custom page name<span class="arrow"></span></a>
    </li>

    <?php } //end for if...else ?> 
    <?php } //end for while loop ?> 
</ul>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM