anth0ny Posted January 7, 2009 Share Posted January 7, 2009 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 More sharing options...
bluesoul Posted January 7, 2009 Share Posted January 7, 2009 <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> Link to comment https://forums.phpfreaks.com/topic/139861-solved-if-inside-an-echo/#findComment-731691 Share on other sites More sharing options...
flyhoney Posted January 7, 2009 Share Posted January 7, 2009 <li class="med"> <img src="../../elements/images/layout/<?php echo $stars ?>star.jpg" alt="<?php echo $stars ?> star"/> </li> Link to comment https://forums.phpfreaks.com/topic/139861-solved-if-inside-an-echo/#findComment-731692 Share on other sites More sharing options...
envexlabs Posted January 7, 2009 Share Posted January 7, 2009 You could also set a variable called $result if(1 == 1){ $stars = ""; $result = '<img src="" />' } <li class=""><?= $result; ?></li> Link to comment https://forums.phpfreaks.com/topic/139861-solved-if-inside-an-echo/#findComment-731693 Share on other sites More sharing options...
rhodesa Posted January 7, 2009 Share Posted January 7, 2009 -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>"; ?> Link to comment https://forums.phpfreaks.com/topic/139861-solved-if-inside-an-echo/#findComment-731705 Share on other sites More sharing options...
anth0ny Posted January 7, 2009 Author Share Posted January 7, 2009 cool thanks for the help really picked up a lot from this cheers! Link to comment https://forums.phpfreaks.com/topic/139861-solved-if-inside-an-echo/#findComment-731721 Share on other sites More sharing options...
.josh Posted January 7, 2009 Share Posted January 7, 2009 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'/>"; Link to comment https://forums.phpfreaks.com/topic/139861-solved-if-inside-an-echo/#findComment-731724 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.