0

I have this variable for $ipCheck = $home_model->checkIP($id); and after print_r of $ipCheck I am getting bellow results.

[0] => stdClass Object
        (
            [ID] => 330
            [image_ID] => 44
            [ip] => 192.168.1.197
            [like_count] => 1
            [favorite_count] => 0
        )

    [1] => stdClass Object
        (
            [ID] => 332
            [image_ID] => 44
            [ip] => 192.168.1.197
            [like_count] => 0
            [favorite_count] => 1
        )

 )

Now I have to set some condition if favorite_count = 0 echo favorite count 0 else favorite count 1.

I have tried with this condition but not working.

<?php if(isset($ipCheck[0]->favorite_count) == 0 or empty($ipCheck[0]->favorite_count)) { ?>
    favorite count 0
    <br />
<?php }else{ ?>
    favorite count 1
    <br />
<?php }?>

I am getting every time favorite count 0. Can you help me to fix this condition. I am new and learning php. I will thank full If you help me.

Thanks.

3
  • 1
    try to make it all $ipCheck[0] to $ipCheck[1] and in the if argument and see the results. you must learn how to deal with arrays =) Commented Apr 8, 2014 at 5:06
  • have you posted you full output? Commented Apr 8, 2014 at 5:10
  • You have to loop through each element of $ipCheck and check it's favorite count Commented Apr 8, 2014 at 5:15

2 Answers 2

2

if i understood correctly you want favorite count 1 if any of the array objects had favorite count 1 not just the first . in that case how about :

$favorite=0;
foreach($ipCheck as $obj){
    if(isset($obj->favorite_count) && $obj->favorite_count!=0)
         {$favorite++;}
}
echo "favorite count ".$favorite."<br />";
Sign up to request clarification or add additional context in comments.

Comments

1

isset and empty return, oft opposing, boolean values. If your variable is empty empty returns true, if your variable isn't set at all isset returns false.

Additionally your == 0 isn't checking variable type so false will == 0. Using === instead would require it to be an integer(number)

If your isset returns false your isset == 0 will return true. If your isset returns true but is 0 your if will return true, if your variable is empty your if will return true.

Empty will accept 0 as an empty value if it's an integer, double, or string (basically a boolean 0 is not empty and everything else is). In your case that's what's forcing it to be always true.

Also, avoid <?= instead use <?php echo. <?= is environment specific and requires specific php settings to be on which can generally break your code when moved to a new environment.

You can see a few options in the other two comment's code snippets. A foreach won't iterate through an empty value in the case of this check so an empty check is redundant(and also still always true) but other than that the code used there isn't bad.

In your case you could probably just get away with an

if(isset($ipCheck[0]->favorite_count)){
   echo "favorite count ". $ipCheck[0]->favorite_count;
}

1 Comment

That is, assuming of course, that all you want is the 0th result. If you want to iterate through each object you'd want to use a foreach loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.