Jump to content

Check array if there is nothing returned


ultraloveninja

Recommended Posts

I'm working on getting a result returned from MySQL to output an array of items to be rendered into HTML.

 

My current code functions like so:

 

$query = "select stores,item1,item2 from wp_dbt_nonnis_distro_data where state = '$_GET[state]' order by stores asc";
$result = mysql_query($query,$conn);
while ($row = mysql_fetch_array($result)){
$stores = $row['stores'];
$item1= $row['item1'];
$item2= $row['item2'];
$new1 = str_replace(',', '</li><li>', $item1);
$new2 = str_replace(',', '</li><li>', $item2);
echo <<<END
<div class="stores">
<h2>Store Name: $stores</h2>
<h3>Brands Carried:</h3>
<div class="items">
<p>Item1</p>
<ul><li>$new1</li></ul>
</div>
<div class="items">
<p>Item2</p>
<ul><li>$new2</li></ul>
</div>
</div>
END;
}

 

So far, this works fine for returning the items and then replacing the comma (since the items are separated by commas within the DB) and then creating the list items for the HTML.

 

Only thing is that if there are no items returned for a particular item, it just shows an empty bulleted list item.

 

I'm not sure how to get it to check the array and see if there is anything that comes back for each item and if not, then have it display some text like "Not Available".

Edited by ultraloveninja
Link to comment
Share on other sites

You can use empty() to check if $item1 and $item2 are empty as the following :

 

$query = "select stores,item1,item2 from wp_dbt_nonnis_distro_data where state = '$_GET[state]' order by stores asc";
$result = mysql_query($query,$conn);
while ($row = mysql_fetch_array($result)){
   $stores = $row['stores'];
   $item1= $row['item1'];
   $item2= $row['item2'];

   if ( !empty( $item1 ) )
       $new1 = str_replace(',', '</li><li>', $item1);
   else
       $new1 = 'Not Available';

   if ( !empty( $item2 ) )
       $new2 = str_replace(',', '</li><li>', $item2);
   else
       $new2 = 'Not Available';

   echo <<<END
   <div class="stores">
   <h2>Store Name: $stores</h2>
   <h3>Brands Carried:</h3>
   <div class="items">
   <p>Item1</p>
   <ul><li>$new1</li></ul>
   </div>
   <div class="items">
   <p>Item2</p>
   <ul><li>$new2</li></ul>
   </div>
   </div>
   END;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.