views:

1701

answers:

8

hey guys,

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

+10  A: 

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.

Sam152
A: 

You are missing curly braces:

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

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

Peter Perháč
A: 

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>';
      }
?>
karim79
A: 

use

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

PHP variables inside in single quote won't be evaluated

Whoa there cowboy, I think you mean '.$coverlink.'
Sam152
A: 

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.

rslite
A: 

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 :)

Kris
+1  A: 
<?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>";
      }
?>
x4tje
You don't need to escape double quotes inside single quotes.
Sam152
true... but i made his script working not my own ;)
x4tje
A: 

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...

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.
dirtside
Always add the braces. If sy don't add the braces to 1 line if/else-s, hit him hard :)
Csaba Kétszeri