1

As the title says, I want to hide an item is the product name doesn't exist.

The problem here is.. When it show the products for the user, it actually show nothing where the //show nothing is placed instead of showing a new product that has a product name.

So sometime it shows 10 products, sometime it shows 5 products, which is not what i want.

I want it to show a new product if a product name is not there.

Any clues on what i change in my code?

my current code is:

Picks 12 random product from the database:

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

Shows the products for the user:

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>';
    }
  }
Jim Sundqvist
  • 160
  • 2
  • 17

2 Answers2

1

As I commented

try this SQL query

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

where the operator <> means "not equal" that is same as != So you look for all the products whose name is not empty and whose name is not NULL.

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

iiro
  • 3,132
  • 1
  • 19
  • 22
0

I would suggest to simply rewrite the query that only products with a productname are returned.

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

t7bdh3hdhb
  • 438
  • 3
  • 15