0

I got this PHP script from http://tutorialzine.com/2010/02/php-mysql-download-counter/ and integrate it to my wordpress successfully. It works but the "No Download File" message appears even the value is true. Here is the code:

<?php
    $value = get_post_custom_values("kpm_UploadFile");
    $url = ($value[0]);
    $tokens = explode('/', $url);
    $dlvalue = $tokens[sizeof($tokens)-1];

    if (current_user_can('read')) {

        foreach($files_array as $key => $val) {
        if ($dlvalue == $val){
            echo '<a href="http://drrknowledge.net/download.php?file='.urlencode($val).'"><strong>Download File</strong><br />'.$val.' has been downloaded <strong>'.(int)$file_downloads[$val].'</strong> times</a>';
            }else {
                echo 'No Download File';
            }
        }
    } else { echo 'Register to download';}
?>

What im trying to achieve here is to show the download file. If there is no file in "kpm_UploadFile" custom field or if its not present, a else statement saying "No Download File".

I can remove the

echo 'No Download File';

but I want to show a message if there is no value.

  • And what is files_array? You do this in foreach loop, so for one $key this if is true and for other it can be false - I guess this is the reason you see this No download File. – Krzysiek Dróżdż Jul 31 '13 at 17:39
0

OK, it's a lot of guessing, because I can't see the rest of your code, but something like this should work, I hope...

<?php
    $value = get_post_custom_values("kpm_UploadFile");
    $url = ($value[0]);
    $tokens = explode('/', $url);
    $dlvalue = $tokens[sizeof($tokens)-1];

    if (current_user_can('read')) {
        $found = 0;
        foreach ($files_array as $key => $val) {
            if ($dlvalue == $val) {
                $found = 1;
                echo '<a href="http://drrknowledge.net/download.php?file='.urlencode($val).'"><strong>Download File</strong><br />'.$val.' has been downloaded <strong>'.(int)$file_downloads[$val].'</strong> times</a>';
            }
        }
        if ( !$found ) {
            echo 'No Download File';
        }
    } else { echo 'Register to download';}
?>
  • Wow @Krzysiek Dróżdż you solved my problem mate. Thanks a lot. Working now! – MightyGas Jul 31 '13 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.