-1

Getting an error if I am clicking on a link to view the list of certificates.It is working fine in some systems I have tested about 3-4 system with the same code it is working fine but in one system it is not working.

Here is the code were i am getting issue at count

<?php

    if (count(@$certificate_attachments)>0) {
        foreach ($certificate_attachments as $cert_key => $cert_val) {
            if ((strpos($cert_val, ".png")) || (strpos($cert_val, ".jpg")) || (strpos($cert_val, ".jpeg"))) {
                ?>
        <div class="col-md-3">
            <a href="<?php echo ATTACHMENTS_PATH; ?>certificates/<?php echo $profileInfo['email_id']; ?>/<?php echo $cert_val; ?>" title="Certificates" class="thickbox ">
                <img height="100" width="100" class="certif" src="<?php echo ATTACHMENTS_PATH; ?>certificates/<?php echo $profileInfo['email_id']; ?>/<?php echo $cert_val; ?>" />
            </a>
        </div>
    <?php
            } else {
                ?>
        <div class="col-md-3">
            <a href="#TB_inline?height=800&width=500&inlineId=pdfContent<?php echo $cert_key; ?>" title="" class="thickbox">
                <img height="100" width="100" src="<?php echo $this->config->item('img_path_portal_doctor'); ?>pdf-icon.png" class="">
            </a>
        </div>
        <div id="pdfContent<?php echo $cert_key; ?>" style="display: none;">
            <p>
                <object type="application/pdf" width="600" height="500" frame-resize data="<?php echo ATTACHMENTS_PATH; ?>certificates/<?php echo $profileInfo['email_id']; ?>/<?php echo $cert_val; ?>">
                <embed src="<?php echo ATTACHMENTS_PATH; ?>certificates/<?php echo $profileInfo['email_id']; ?>/<?php echo $cert_val; ?>" type="application/pdf" />
                </object>

            </p>
        </div>
    <?php
            }
        }
    } else {
        ?>
        Not provided any Certificates.
    <?php
    }
    ?>
</div>

getting this error:

A PHP Error was encountered

Severity: Warning

Message: count(): Parameter must be an array or an object that implements Countable

Filename: doctor/profile.php

Line Number: 533

But it is working in fine the system which i am working and tested in another 2-3 systems with the same code it is also working fine but when client is running this code it is getting this issue in client system.

IlGala
  • 3,331
  • 4
  • 35
  • 49
Latha
  • 11
  • 3
  • Is JavaScript relevant here? – VLAZ Sep 26 '19 at 10:40
  • 1
    Why you have `@` in `if(count(@$certificate_attachments)>0){` ? it should be `if(count($certificate_attachments)>0){` – Alaa Morad Sep 26 '19 at 10:44
  • @AlaaMorad if i am removing this @ symbol before variable it is geting an error as undefined variable $certificate_attachments – Latha Sep 26 '19 at 10:48
  • @Latha `@` hides errors and should very rarely be used. In this instance figure out why your variable is undefined. – Script47 Sep 26 '19 at 10:59
  • But it is working in some systems and it is not working in only one system – Latha Sep 26 '19 at 11:08
  • @Script47 does the problem occurs from xampp versions as well becuase i have installed xampp version 1.8.0 and in client system xampp version was 7.3 does this occurs the problem – Latha Sep 26 '19 at 11:26
  • try changing your code like this: if (isset($certificate_attachments) && $certificate_attachments && is_array($certificate_attachments) && count($certificate_attachments)>0) { – Vipin Kr. Singh Oct 03 '19 at 20:00

1 Answers1

0

As other people have indicated, please don't use @, it is actually an error suppression operator and it will hide bugs like this one. Since removing the @ from your original code results in "undefined variable" variable warning. I suggest you track back to where you defined the variable $certificate_attachments and check to see how it was defined. It maybe have been defined out of scope, probably of a conditional statement which would explain why it works with some systems and not others. Here is a simple example of out of scope;

// pseudo code to demonstrate out of scope

$results = data_queried_from_database_or_something();

if(!empty($results)) {
    // define attachments only if we get results
    $certificate_attachments = $results;
}
// For systems that have no attachments, calling $certificate_attachments
// will result in "undefined variable" error message

Instead of suppressing errors with the @ symbol, it is better to do type checking just before the count()

// ensure that the variable is an array and that it has values in it.
if(is_array($certificate_attachments) && count($certificate_attachments) > 0) 
{
    // your code goes here
}

It would be helpful to see the code where you define $certificate_attachments and also know the different cases where the code run successfully and where it didn't.

yesigye
  • 66
  • 5