GalaxyTramp Posted August 13, 2008 Share Posted August 13, 2008 Hi I have an array that may or may not contain data. All works well when there is but a fatal error occurs if the array contains no data. Any suggestions as to how I can overcome this as simply as possible please. <?php $n=1; //loop thru arrays and output data if(is_array($result["retrieveFeaturedRentalsResult"]["rental"][0])){ $arr_properties = $result["retrieveFeaturedRentalsResult"]["rental"]; } else { $arr_properties = $result["retrieveFeaturedRentalsResult"]; } foreach($arr_properties as $key => $value) { $arr_property = $arr_properties[$key]; ?> <?php if ($n>$numbercols) { echo "<tr><td>"; } elseif ($n==1){ echo "<tr><td>"; } else { echo "<td>"; } ?> <table cellpadding="0" cellspacing="0" class="panel"><tr><td class="title" colspan="2"> <a id="Location" href="<?php echo $property_details_page ?>?propertyid=<?php echo $arr_property["!propertyId"]; ?>"> <?php echo translateWord($arr_property["type"])." ".translateWord("in"); ?> <?php echo $arr_property["city"]; ?></a> </td> </tr> <?php if ($n>=$numbercols) { echo "</td></tr>"; $n=1; } else { echo "</td>"; $n++; } ?> <?php } ?> Link to comment https://forums.phpfreaks.com/topic/119487-solved-if-array-is-empty-by-pass-results/ Share on other sites More sharing options...
trq Posted August 13, 2008 Share Posted August 13, 2008 Check the the size of the array using count(). Link to comment https://forums.phpfreaks.com/topic/119487-solved-if-array-is-empty-by-pass-results/#findComment-615529 Share on other sites More sharing options...
akitchin Posted August 13, 2008 Share Posted August 13, 2008 empty() also works on arrays. Link to comment https://forums.phpfreaks.com/topic/119487-solved-if-array-is-empty-by-pass-results/#findComment-615531 Share on other sites More sharing options...
GalaxyTramp Posted August 13, 2008 Author Share Posted August 13, 2008 Check the the size of the array using count(). basically what I need to be doing here is: I need to check whether the array is empty using (count) If array = empty do not output else output data Just wish my coding was up to the task C Link to comment https://forums.phpfreaks.com/topic/119487-solved-if-array-is-empty-by-pass-results/#findComment-615566 Share on other sites More sharing options...
DarkWater Posted August 13, 2008 Share Posted August 13, 2008 It's a simple if statement. if (count($array) > 0) { //it's an array //output stuff. } Link to comment https://forums.phpfreaks.com/topic/119487-solved-if-array-is-empty-by-pass-results/#findComment-615575 Share on other sites More sharing options...
GalaxyTramp Posted August 13, 2008 Author Share Posted August 13, 2008 It's a simple if statement. if (count($array) > 0) { //it's an array //output stuff. } OK works a treat thanks for the inspiration Link to comment https://forums.phpfreaks.com/topic/119487-solved-if-array-is-empty-by-pass-results/#findComment-615616 Share on other sites More sharing options...
GalaxyTramp Posted August 13, 2008 Author Share Posted August 13, 2008 Whoops guess i got a bit carried away it displays nothing whether the array has content or not. Must have got the code wrong. I have: <?php // check if array contains data if (count($array) > 0) { ?> <?php $n=1; //loop thru arrays and output data if(is_array($result["retrieveFeaturedRentalsResult"]["rental"][0])){ $arr_properties = $result["retrieveFeaturedRentalsResult"]["rental"]; } else { $arr_properties = $result["retrieveFeaturedRentalsResult"]; } foreach($arr_properties as $key => $value) { $arr_property = $arr_properties[$key]; ?> <?php if ($n>$numbercols) { echo "<tr><td>"; } elseif ($n==1){ echo "<tr><td>"; } else { echo "<td>"; } ?> <table cellpadding="0" cellspacing="0" class="panel"><tr><td class="title" colspan="2"> <a id="Location" href="<?php echo $property_details_page ?>?propertyid=<?php echo $arr_property["!propertyId"]; ?>"> <?php echo translateWord($arr_property["type"])." ".translateWord("in"); ?> <?php echo $arr_property["city"]; ?></a> </td> </tr> <?php if ($n>=$numbercols) { echo "</td></tr>"; $n=1; } else { echo "</td>"; $n++; } ?> <?php } } ?> Link to comment https://forums.phpfreaks.com/topic/119487-solved-if-array-is-empty-by-pass-results/#findComment-615672 Share on other sites More sharing options...
akitchin Posted August 13, 2008 Share Posted August 13, 2008 that's probably because $array doesn't exist. you'll need to change that to whichever array you wanted to check against. Link to comment https://forums.phpfreaks.com/topic/119487-solved-if-array-is-empty-by-pass-results/#findComment-615718 Share on other sites More sharing options...
GalaxyTramp Posted August 13, 2008 Author Share Posted August 13, 2008 Ok I have <?php $a = array(1, 2, 3, array("retrieveFeaturedRentalsResult", "rental", "0")); var_dump($a); ?> This gives as a result:array(4) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> array(3) { [0]=> string(29) "retrieveFeaturedRentalsResult" [1]=> string(6) "rental" [2]=> string(1) "0" } } I also changed "array" to "result" <?php // check if array contains data if (count($result) > 0) { ?> <?php $n=1; //loop thru arrays and output and format data if(is_array($result["retrieveFeaturedRentalsResult"]["rental"][0])){ $arr_properties = $result["retrieveFeaturedRentalsResult"]["rental"]; } else { $arr_properties = $result["retrieveFeaturedRentalsResult"]; } foreach($arr_properties as $key => $value) { $arr_property = $arr_properties[$key]; ?> This gives me Fatal error: Cannot use string offset as an array in /home/***/public_html/***/page.php This I think is because there is no data to output C Link to comment https://forums.phpfreaks.com/topic/119487-solved-if-array-is-empty-by-pass-results/#findComment-615743 Share on other sites More sharing options...
jordanwb Posted August 13, 2008 Share Posted August 13, 2008 Nevermind I'm an idiot. :-\ Link to comment https://forums.phpfreaks.com/topic/119487-solved-if-array-is-empty-by-pass-results/#findComment-615747 Share on other sites More sharing options...
akitchin Posted August 13, 2008 Share Posted August 13, 2008 try changin count() to this: if (!empty($result)) and what does the var dump on result look like? Link to comment https://forums.phpfreaks.com/topic/119487-solved-if-array-is-empty-by-pass-results/#findComment-615750 Share on other sites More sharing options...
GalaxyTramp Posted August 13, 2008 Author Share Posted August 13, 2008 try changin count() to this: if (!empty($result)) and what does the var dump on result look like? Ok I changed code to this: <?php // check if array contains data if (count($rental) > 0) { ?> <?php $n=1; //loop thru arrays and output and format data if(is_array($result["retrieveFeaturedRentalsResult"]["rental"][0])){ $arr_properties = $result["retrieveFeaturedRentalsResult"]["rental"]; } else { $arr_properties = $result["retrieveFeaturedRentalsResult"]; } foreach($arr_properties as $key => $value) { $arr_property = $arr_properties[$key]; ?> All now appears to be OK (Didn't I say that before ) VAR DUMP gives this by the way: array(4) { * => int(1) [1]=> int(2) [2]=> int(3) [3]=> array(3) { * => string(29) "retrieveFeaturedRentalsResult" [1]=> string(6) "rental" [2]=> string(1) "0" } } C Link to comment https://forums.phpfreaks.com/topic/119487-solved-if-array-is-empty-by-pass-results/#findComment-615754 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.