ultraloveninja Posted February 19, 2013 Share Posted February 19, 2013 (edited) 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 February 19, 2013 by ultraloveninja Quote Link to comment https://forums.phpfreaks.com/topic/274698-check-array-if-there-is-nothing-returned/ Share on other sites More sharing options...
MaaSTaaR Posted February 19, 2013 Share Posted February 19, 2013 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; } Quote Link to comment https://forums.phpfreaks.com/topic/274698-check-array-if-there-is-nothing-returned/#findComment-1413486 Share on other sites More sharing options...
ultraloveninja Posted February 19, 2013 Author Share Posted February 19, 2013 (edited) Ahhh, cool! Yeah, I think that will work. I'll mess around with it and if I have any other questions, I'll let you know. Thanks! Edited February 19, 2013 by ultraloveninja Quote Link to comment https://forums.phpfreaks.com/topic/274698-check-array-if-there-is-nothing-returned/#findComment-1413491 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.