Search code examples
phpmysqlshow-hide

Hiding links with 0 query value results by php


I have a menu block that contains some links like: Some_link1: 5pcs, Some_link2: 13pcs, Some_link3: 0pcs, Some_link4: 0pcs. I want to hide link "Some_link" with 0pcs value. I write a code with MySQL query, but it's not working! "Some_link" with 0pcs not hiding but still show 0pcs value. What i'm doing wrong or what my mistake? I can't understand. Thank you for help.

<?
  $resultonline = mysql_query("SELECT count(customers_id) from tbl_customers WHERE active='Y' and saled='N'");
  $resultonshafasaled = mysql_query("SELECT count(customers_id) from tbl_customers WHERE shafa='Y' and saled='Y'");
  $resultonlinenonactive = mysql_query("SELECT count(customers_id) from tbl_customers WHERE active='N' and saled='N'");

  $topmenuNotOnShafa = mysql_result($resultonshafasaled, 0);
  $topmenuonline = mysql_result($resultonline, 0);
  $topmenuoffline = mysql_result($resultonlinenonactive, 0);

  $topmenuonlineText = "Some text : ";
  $topmenuOnShafaText = "Some text 2 : ";
 ?>

<?php if ($topmenuonline!=0): ?><?=$topmenuonlineText;?><?php endif; ?>
<?php if ($topmenuonline!=0): ?><a href="some_link" target="_self"><?=$topmenuonline;?></a>
<?php endif; ?>
<?php if ($topmenuoffline!=0): ?> / <a href="some_link" target="_self"><?=$topmenuoffline;?></a>
<br /><?php endif; ?>

<?php if ($topmenuNotOnShafa!=0): ?>
<span class="saled-warning"><a href="some_link" target="_self" ><?=$topmenuNotOnShafa;?></a></span>
<?php endif; ?>


Solution

  • You can check if the value of the item is 0 or not and print it only if it is not 0:

    Example:

     <?php
    
     $items='0';
    
     if(isset($items)){
         if($items != 0){
             echo "<a href='non_zero_item.php'>Item from menu (".$items.")";
         } else {
             echo "Oh sorry, there are no items!";
         }
     } else {
         echo "items variable is not declared!";
     }
    
     ?>
    

    In this example you will get the else condition, if you change the variable $items to 1 you will get printed the html code. This is a small test, the variable can be the mysql query result, a manual input like this, etc.

    If you dont want to print anything if the value is 0 or not declared, like I understand you want you can do only this:

     <?php
    
     $items='1';
    
     if(isset($items)){
         if($items != 0){
             echo "<a href='non_zero_item.php'>Item from menu (".$items.")";
         }
     }
    
     ?>
    

    For debuging I recommend you to use allways the else condition.