Jump to content

[SOLVED] if inside an echo


anth0ny

Recommended Posts

How would i write this:

 

<?php if ( $stars == '5' ) {
$stars = "";
echo '<img src="../../elements/images/layout/5star.jpg" alt="5 star"/>';
}


if ( $stars == '4' ) {
$stars = "";
echo '<img src="../../elements/images/layout/4star.jpg" alt="4 star"/>';
}

if ( $stars == '3' ) {
$stars = "";
echo '<img src="../../elements/images/layout/3star.jpg" alt="3 star"/>';
}

if ( $stars == '2' ) {
$stars = "";
echo '<img src="../../elements/images/layout/2star.jpg" alt="2 star"/>';
}

if ( $stars == '1' ) {
$stars = "";
echo '<img src="../../elements/images/layout/1star.jpg" alt="1 star"/>';
}

if ( $stars == '0' ) {
$stars = "";
echo '<img src="../../elements/images/layout/0star.jpg" alt="0 star"/>';
}

echo $stars;?>

 

inside this:

 

<?php <li class=\"med\">  TO GO HERE?  </li> ?>

Link to comment
https://forums.phpfreaks.com/topic/139861-solved-if-inside-an-echo/
Share on other sites

<li class="med">
<?php 
switch($stars) {
case '5':
   echo '<img src="../../elements/images/layout/5star.jpg" alt="5 star"/>';
   break;
case '4':
   echo '<img src="../../elements/images/layout/4star.jpg" alt="4 star"/>';
   break;
//etc
   default:
   die("Invalid result");
   break;
} 
$stars = "";
?>
</li>

-or just echo the open LI before the IFs and the close LI after the IFs

-or put the LIs inside each IF

 

I like this way:

<?php
  echo '<li class="">'; 
  switch($star){
    case 5:
      echo '<img src="../../elements/images/layout/4star.jpg" alt="4 star"/>';
      break;
    case 4:
      echo '<img src="../../elements/images/layout/4star.jpg" alt="4 star"/>';
      break;
    case 3:
      echo '<img src="../../elements/images/layout/3star.jpg" alt="3 star"/>';
      break;
    case 2:
      echo '<img src="../../elements/images/layout/2star.jpg" alt="2 star"/>';
      break;
    case 1:
      echo '<img src="../../elements/images/layout/1star.jpg" alt="1 star"/>';
      break;
    case 0:
      echo '<img src="../../elements/images/layout/0star.jpg" alt="0 star"/>';
      break;
  }
  echo '</li>';
?>

or this way:

<?php
  echo "<li class=\"\"><img src=\"../../elements/images/layout/{$star}star.jpg\" alt=\"{$star} star\"/></li>";
?>

Why all the conditions? Just do

 

(preserving your quotes)

echo '<img src="../../elements/images/layout/' . $star . 'star.jpg" alt="' . $star . ' star"/>';

 

or

 

(changing quotes)

echo "<img src='../../elements/images/layout/{$star}star.jpg' alt='{$star} star'/>";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.