-1

I'm trying to execute the following code but it's not working:

<?php foreach($reciepts as $value) { 
    $extension = get_file_extension($value);
    if($extension == "pdf") { ?>
        <p>Receipt:</p><br><embed src="<?php echo $value; ?>">
    <?php }
    else { ?>
        <p>Receipt:</p><br><img style="max-width:500px; max-height:500px;" src="<?php echo $value; ?>" />
    <?php } ?>

The $reciepts array iin this case is:

[7] => Array
            (
                [0] => http://coppermountain.co.uk/wp-content/uploads/gravity_forms/12-c07945f2e8e37e124bd5be2c8a979300/2016/02/Screen-Shot-2016-02-01-at-09.33.18.png
                [1] => http://coppermountain.co.uk/wp-content/uploads/gravity_forms/12-c07945f2e8e37e124bd5be2c8a979300/2016/02/Expenses-1.pdf
            )

The function to get the file extension:

function get_file_extension($value) {
    return substr(strrchr($value,'.'),1);
}

The tests I have run so far:

$extension = $form_data['field'][7][1];
echo get_file_extension($extension);

$extension echoes out pdf as expected

Also the loop works fine if I don't include the IF/ELSE statement as below:

<?php foreach($reciepts as $value) { ?>
    <p>Receipt:</p><br><img style="max-width:500px; max-height:500px;" src="<?php echo $value; ?>" /> 
<?php } ?>

Thanks

Ian Butler
  • 399
  • 7
  • 20
  • 1
    Can you describe a bit better what isn't working precisely ? Thanks – Nirnae Feb 03 '16 at 15:06
  • you sure that `$reciepts` isn't spelled as `$receipts`? which should be spelled that way. Use error reporting http://php.net/manual/en/function.error-reporting.php – Funk Forty Niner Feb 03 '16 at 15:08
  • 1
    Why aren't you using pathinfo() to get the extension? – P. Gearman Feb 03 '16 at 15:11
  • 1
    Note that `` has had a lot of troubles displaying PDF. Perhaps try the suggestions in [this post](http://stackoverflow.com/questions/1244788/embed-vs-object). – sgtdck Feb 03 '16 at 15:11
  • @P.Gearman I had never heard of it before. Is there an advantage to using it? – Ian Butler Feb 03 '16 at 15:40
  • In my opinion, yes. It is designed to retrieve path information, including the extension. http://php.net/manual/en/function.pathinfo.php – P. Gearman Feb 03 '16 at 15:44

1 Answers1

1

You are missing a close brace of your foreach loop, the following works.

<?php
function get_file_extension($value)
{
    return substr(strrchr($value, '.'), 1);
}

$reciepts = [
    'http://coppermountain.co.uk/wp-content/uploads/gravity_forms/12-c07945f2e8e37e124bd5be2c8a979300/2016/02/Screen-Shot-2016-02-01-at-09.33.18.png',
    'http://coppermountain.co.uk/wp-content/uploads/gravity_forms/12-c07945f2e8e37e124bd5be2c8a979300/2016/02/Expenses-1.pdf'
];
foreach ($reciepts as $value) {
    $extension = get_file_extension($value);
    if ($extension == "pdf") {
        ?>
        <p>Receipt:</p><br><embed src="<?php echo $value; ?>">
    <?php } else {
        ?>
        <p>Receipt:</p><br><img style="max-width:500px; max-height:500px;" src="<?php echo $value; ?>" />
    <?php
    }
}//added missing brace
?>
lookbadgers
  • 988
  • 9
  • 31