-1

I have a donor list on database which all have some point data. User can select few donors to show their point table on frontend by their id. A function is for getting user point info as array from database. I have generated tab for each donor. And the first tab will be active by default.

<?php
    foreach ($userGivenDonorID as $donor){
        $data = Libary::get_point($donor);

        if($donor=== reset($userGivenDonorID )){
            echo '<li class="tablink active">'.$donor['name'].'</li>';
        } else {
            echo '<li class="tablink">'.$donor['name'].'</li>';
        }
    }
?>

Now the problem is I want to skip a donor id if it's not having any point data on database thus giving an empty array for Libary::get_point($donor). Cause if for any reason I don't have any data for the first user given id I don't get the first tab active by default.

Foysal Remon
  • 301
  • 2
  • 9
  • https://stackoverflow.com/questions/3654295/remove-empty-array-elements – ScaisEdge Jul 21 '18 at 06:50
  • I assume $data is the array? you can always do a check if $data array is empty with the empty() function – fransyozef Jul 21 '18 at 06:55
  • This is not a accurate duplicate, I think. I am not asking about the empty `$donor`. I am asking about the `$data` get by using it on a function. @Marcin Orlowski – Foysal Remon Jul 21 '18 at 07:49

2 Answers2

1

Just for common practice, you can write:

if($donor=== reset($userGivenDonorID )){
    echo '<li class="tablink active">'.$donor['name'].'</li>';
} else {
    echo '<li class="tablink">'.$donor['name'].'</li>';
}

like:

$active = $donor === reset($userGivenDonorID) ? 'active' : '';
echo '<li class="tablink '.$active.'">'.$donor['name'].'</li>';
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
fransyozef
  • 544
  • 2
  • 7
  • 18
0

If I am not mistaken you can check the value of $data and use continue

$data = Libary::get_point($donor);

Then use for example:

if (null === $data) continue;

Or

if (empty($data)) continue;
The fourth bird
  • 154,723
  • 16
  • 55
  • 70