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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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