0

I got a simple if else statement where I show a list of pdf files, when the id is empty (aka there is no pdf file) I want to show: 'No available downloads'. But it shows that text no matter what, even if there are pdf files present.

My code:

<div class="widget broucher">
    <h4>DOWNLOADS</h4>
    <ul>
    <?
    //pdf bestanden
    $pdf                = "SELECT * FROM `snm_attachments` WHERE parent_id = '".$conn->real_escape_string($contentcr[0]['id'])."'";
    $pdfcon             = $conn->query($pdf);
    $pdfcr              = array();
    while ($pdfcr[]     = $pdfcon->fetch_array());

    foreach($pdfcr as $pdf){
        if($pdf['id'] != ''){
            $downloads .= '<li><a href="cms/attachments/article/'.$contentcr[0]['id'].'/'.$pdf['filename'].' "target="_blank"><i class="fa fa-file-pdf-o"></i>'.$pdf['filename'].'</a></li>';   
        }else{
            $downloads .= '<li>No available downloads</li>';
        }
    }
    echo $downloads;
    ?>
    </ul>
</div>

Why is it always showing, even when there is no id present for a row?

twan
  • 2,450
  • 10
  • 32
  • 92
  • Use if(isset($pdf['id'] ) && $pdf['id'] !='') instead – Web Artisan May 24 '16 at 13:40
  • Why you make your code so complicated using `while and foreach loop `together. You can do in using single while loop !! – Saty May 24 '16 at 13:41
  • 1
    use var_dump($pdf['id']) to check what is the output – Deepak Dholiyan May 24 '16 at 13:42
  • Your code seems fine. Check that the array is returning the values you expect it to. – styke May 24 '16 at 13:43
  • My php is somewhat rusty but unless you let some part of your code out, `$downloads` is undefined. – jdepypere May 24 '16 at 13:47
  • This outputs the following @DeepakDholiyan : `string(1) "1" NULL` Does the NULL value have something to do with it? – twan May 24 '16 at 13:48
  • @jdepypere $downloads is used to output the data, it is defined in the if else statement. – twan May 24 '16 at 13:52
  • @twan: You never initialise the `$downloads`, you use `.=` right away. [Online example](http://sandbox.onlinephpfunctions.com/code/832f7847e8600a457e7f84f1d5a78ead29454650) – jdepypere May 24 '16 at 13:56
  • I didn't know that was needed, never had a warning message and it always worked right away when I did it without declaring every variable, so I just use something like `$downloads = '';` ? – twan May 24 '16 at 14:07
  • @twan yes, you can see `$downloads = '';` specified in my answer. – Marcos Pérez Gude May 24 '16 at 14:23
  • @twan, use empty() function. empty() returns true if a variable is 0, null, false or an empty string. – Deepak Dholiyan May 24 '16 at 14:43
  • if($pdf['id'] != '' && !empty($pdf['id'])) – Deepak Dholiyan May 24 '16 at 14:43
  • http://stackoverflow.com/questions/7191626/isset-and-empty-what-to-use – Deepak Dholiyan May 24 '16 at 14:45
  • Have a look here at above url, This will help you – Deepak Dholiyan May 24 '16 at 14:46
  • @DeepakDholiyan Makes sense, but it still outputs no downloads available. I can't understand why it keeps showing it, never had this problem before. – twan May 24 '16 at 14:48
  • This is probably what you are trying, but just in case it was not clear: do if(!empty($pdf['id'])) instead of if($pdf['id'] != ''). That will work even if ['id'] is not present, but if you continue to see unexpected results, you should inspect the contents of $pdf (for example you could print_r($pdf) inside the foreach loop) – billrichards May 24 '16 at 19:18
  • echo '
    '; print_r($pdfcr); echo '
    ';
    – Deepak Dholiyan May 25 '16 at 05:03
  • Check what are you getting in array – Deepak Dholiyan May 25 '16 at 05:03
  • also check after removing else part – Deepak Dholiyan May 25 '16 at 05:05
  • @DeepakDholiyan This shows two filled array values and one empty (the last one:) ` [2] => ` – twan May 25 '16 at 08:12
  • @twan, Use this if(isset($pdf['id']) && !empty($pdf['id'])) – Deepak Dholiyan May 25 '16 at 10:04
  • @DeepakDholiyan Still shows the message. Isn't that the case because it executes the if/else for every row in the database and the last array value is empty, so the first two pdfs are shown, but the last value in the array is empty, so it also shows the message: no available downloads. ? – twan May 26 '16 at 09:02
  • I got a simple if else statement where I show a list of pdf files, when the id is empty (aka there is no pdf file) I want to show: 'No available downloads'. But it shows that text no matter what, even if there are pdf files present.----------------------------------------------------------------------------------Here what was saying. and now what you are saying are both different – Deepak Dholiyan May 26 '16 at 10:02
  • You can also use $downloads as an array. like $downloads =array();----------------$downloads[] = 'code'; Then in last print this array. – Deepak Dholiyan May 26 '16 at 10:04

1 Answers1

0

Use this code:

<?
//pdf bestanden
$pdf                = "SELECT * FROM `snm_attachments` WHERE parent_id = '".$conn->real_escape_string($contentcr[0]['id'])."'";
$pdfcon             = $conn->query($pdf);

// initialize downloads variable:
$downloads = '';

// see how while statement disappears (in your code you have the fetch_array attached to the first element of the array):
$pdfcr              = $pdfcon->fetch_array();

foreach($pdfcr as $pdf){
    if($pdf['id'] != ''){
        $downloads .= '<li><a href="cms/attachments/article/'.$contentcr[0]['id'].'/'.$pdf['filename'].' "target="_blank"><i class="fa fa-file-pdf-o"></i>'.$pdf['filename'].'</a></li>';   
    }else{
        $downloads .= '<li>No available downloads</li>';
    }
}
echo $downloads;
?>

Note than the No available downloads message will be rendered in all iterations of the data array when id is different than empty. To show the message when there are no records use this code:

if(count($pdfcr) > 0) {
  foreach($pdfcr as $pdf){
    if($pdf['id'] != ''){
        $downloads .= '<li><a href="cms/attachments/article/'.$contentcr[0]['id'].'/'.$pdf['filename'].' "target="_blank"><i class="fa fa-file-pdf-o"></i>'.$pdf['filename'].'</a></li>';   
    }
  }
}
Marcos Pérez Gude
  • 21,869
  • 4
  • 38
  • 69
  • This gives the following warning: `Warning: Illegal string offset 'id' in /home/website/public_html/_intern/web/content.php on line 100` And loops the pdf about 15 times along with the no downloads available text – twan May 24 '16 at 14:04
  • According with the output, you don't write exactly what I tell you. See this line: `$pdfcr = $pdfcon->fetch_array();`, It's not `$pdfcr[] = $pdfcon->fetch_array();`. – Marcos Pérez Gude May 24 '16 at 14:22
  • Oh yes I removed that because of all the warnings, here is the output with your code: http://collabedit.com/e38wt – twan May 24 '16 at 14:34