如果产品名称不匹配,则隐藏项目';不存在


Hiding an item if product name doesn't exist

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

这里的问题是。当它为用户显示产品时,它实际上在//不显示的地方什么都不显示,而不是显示有产品名称的新产品。

所以有时它会展示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>';
    }
  }

正如我所评论的

尝试此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-相等

我建议简单地重写查询,即只返回具有productname的产品。

从产品名称不为空的产品中选择*ORDER BY RAND()LIMIT 12