4

i have the following code in my detailansicht.php:

<div class="file_description_box">
    <b>Beschreibung:</b><br /><br />
        <?php 
             if(!empty($beschreibung))
                 echo '<div align="center">';
                 echo '<img src="$coverlink" alt="Cover">';
                 echo '</div><br />';
                 echo format_content($beschreibung); 
             **else**
             echo '<i>Keine Beschreibung vorhanden</i>';
        ?>
</div>

but i think there has to be a mistake in the img tag. everytime i trie to open the page, he shows me an error: "Parse error: syntax error, unexpected T_ELSE in bla/bla/include/detailansicht.php on line 123" (Line 123 is the else marked bold). I tried several methods but i always get this error. It would be nice, if someone could help me with this.

-sTarbuZz

1
  • 1
    Consider writing the whole code in english und nicht die hälfte in Deutsch. It's very annoying to maintain bilanguage code.. Commented May 18, 2009 at 13:02

8 Answers 8

15

You are missing some curly brackets and your PHP variable wasn't embedded property. Your code should look like this:

<div class="file_description_box">
    <b>Beschreibung:</b><br /><br />
        <?php 
             if(!empty($beschreibung)){
                 echo '<div align="center">';
                 echo '<img src="'.$coverlink.'" alt="Cover">';
                 echo '</div><br />';
                 echo format_content($beschreibung); 
             }else{
                 echo '<i>Keine Beschreibung vorhanden</i>';
                }
        ?>
</div>

Just on a side note it really doesn't matter what you use PHP for, if there was a fault with the image tag and it wasn't being displayed properly it would generally be a HTML problem assuming you have outputted it correctly.

In this case it was a PHP problem because there were a few errors in the code, it hasn't really got anything to do with the image tag.

Sign up to request clarification or add additional context in comments.

Comments

2
<?php 
     if(!empty($beschreibung)) {
         echo "<div align=\"center\">";
         echo "<img src=\"$coverlink\" alt=\"Cover\">";
         echo "</div><br />";
         echo format_content($beschreibung); 
      } else {
         echo "<i>Keine Beschreibung vorhanden</i>";
      }
?>

1 Comment

You don't need to escape double quotes inside single quotes.
1

You are missing curly braces:

if () {
...
} else {
...
}

so your PHP is not syntactically correct and will not be parsed by PHP hypertext pre-processor.

Comments

1

Try using curly braces:

<?php 
     if(!empty($beschreibung)) {
         echo '<div align="center">';
         echo '<img src="$coverlink" alt="Cover">';
         echo '</div><br />';
         echo format_content($beschreibung); 
      } else {
         echo '<i>Keine Beschreibung vorhanden</i>';
      }
?>

Comments

1

use

echo '<img src="', $coverlink', " alt="Cover">';

PHP variables inside in single quote won't be evaluated

Comments

1

Yep, you're missing the curly braces. Simply formatting the code nicely with tabs won't make it a block.

Also you missed the ending of the img tag (/>), but that's unrelated to your question.

Comments

1

thanks for all your answers... I'm trying Kris' method now...

And the thing with the brackets... i don't know how this could happen... i didn't write the original script (i just added the part with the cover) and i didn't recognize that there were missing brackets, because $beschreibung is never empty and i think php ignores the if and else if the brackets aare missing...

2 Comments

No, the reason it worked before is because if there's only one statement following the "if", then you don't need the curly braces for the "else". "if ($foo) blah(); else bleh();" is valid PHP, but if "blah() is more than one statement, you need the braces.
Always add the braces. If sy don't add the braces to 1 line if/else-s, hit him hard :)
1

I would go for the alternative syntax which i feel is easier on the eyes intermixed with html:

<? if(!empty($beschreibung)) : ?>
    <div align="center">
        <img src="<?= $coverlink; ?>" alt="Cover">
    </div><br />
    <?= format_content($beschreibung); ?> 
<? else : ?>
    <i>Keine Beschreibung vorhanden</i>
<? endif ; ?>

PS: i am not advocating putting logic inside the markup, just noting it can be done.

edit: fixed syntax error (; after endif instead of :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.