如果产品名称不存在,则隐藏项目

时间:2013-06-27 09:21:26

标签: php mysql

正如标题所说,我想隐藏一个项目是产品名称不存在。

这里的问题是..当它为用户显示产品时,它实际上没有显示//放置任何内容而不显示具有产品名称的新产品。

所以有时它会显示10个产品,有时会显示5个产品,这不是我想要的产品。

如果产品名称不存在,我希望它显示新产品。

我在代码中更改内容的任何线索?

我目前的代码是:

从数据库中挑选12个随机产品:

$dynamicList = "";
$sql = mysql_query("SELECT * FROM products ORDER BY RAND() LIMIT 12");

显示用户的产品:

if($product_name == ''){

         //Show nothing

        }else{
    $dynamicList .= 
                '<table border="0" id="indexproducts" style="margin-left:22px; margin-bottom:20px;">
                  <tr>
                    <td id="indexproductfoto" align="center">
                        <a href="http://www.mysite.com/id/'.$id.'/'.$seo8.'/">
                            <img src="http://www.mysite.com/inventoryimages/'.$id.'.jpg" onerror="this.src=\'http://www.mysite.com/inventoryimages/x.jpg\';" />
                        </a>
                    </td>
                  </tr>
                  <tr>
                    <td id="indexproducttext2"><strong>'.$product_name.'</strong><br />
                    </td>
                  </tr>
                  <tr>
                    <td style="color:#F00" id="indexproducttext"><strong>Pris: '.round($price).':-</strong></td>
                    </tr>
                  <tr>
                    <td style="color:#F00" id="indexproducttext"><strong>(Exkl.moms: '.round($price1).':-)</strong></td>
                    </tr>
                    <tr>
                     <td id="indexproducttext"><a href="http://www.mysite.com/id/'.$id.'/'.$seo8.'/">Produktinformation</a></td>
                    </tr>
                </table>';
    }
  }

2 个答案:

答案 0 :(得分:1)

正如我评论的那样

尝试此SQL查询

SELECT * FROM products WHERE IFNULL(productname, "") <> "" ORDER BY RAND() LIMIT 12 

其中运算符<>表示“不等于”与!=相同所以您查找名称不为空且名称不为NULL的所有产品。

http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_not-equal

答案 1 :(得分:0)

我建议只重写查询,只返回带有productname的产品。

  

SELECT * FROM products WHERE productname NOT NULL ORDER BY RAND()LIMIT 12