Jump to content

[SOLVED] If array is empty by pass results


GalaxyTramp

Recommended Posts

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
}
?>

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

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
         }

}
?>

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

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.