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 } ?> Quote Link to comment 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(). Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 13, 2008 Share Posted August 13, 2008 empty() also works on arrays. Quote Link to comment 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 Quote Link to comment 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. } Quote Link to comment 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 Quote Link to comment 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 } } ?> Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
jordanwb Posted August 13, 2008 Share Posted August 13, 2008 Nevermind I'm an idiot. :-\ Quote Link to comment 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? Quote Link to comment 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 Quote Link to comment 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.