I am still learning PHP so bare with me. But I know there is a better method to doing this using a while loop or variable variables or something. Can you point me in the right direction? The way I have the script works perfectly.. I just know it's sloppy and the wrong way to do it. Any help on what would be the right way to do this? <?php if ( $sbm_sponsor1 == TRUE ) { echo "<img src=\"" . $sbm_sponsor1 . "\"/>"; } if ( $sbm_sponsor2 == TRUE ) { echo "<img src=\"" . $sbm_sponsor2 . "\"/>"; } if ( $sbm_sponsor3 == TRUE ) { echo "<img src=\"" . $sbm_sponsor3 . "\"/>"; } if ( $sbm_sponsor4 == TRUE ) { echo "<img src=\"" . $sbm_sponsor4 . "\"/>"; } if ( $sbm_sponsor5 == TRUE ) { echo "<img src=\"" . $sbm_sponsor5 . "\"/>"; } if ( $sbm_sponsor6 == TRUE ) { echo "<img src=\"" . $sbm_sponsor6 . "\"/>"; } if ( $sbm_sponsor7 == TRUE ) { echo "<img src=\"" . $sbm_sponsor7 . "\"/>"; } else { echo ''; } ?> PHP: Thanks!
$sponsors[0] = array('show'=>true,'image'=>'http://example.com/img.jpg'); $sponsors[1] = array('show'=>false,'image'=>'http://sponsor.com/img.jpg'); foreach($sponsors as $sponsor): if($sponsor['show']){ echo "<img src=\"" . $sponsor['image'] . "\"/>"; } endforeach; PHP:
Thanks for the quick reply but that didn't quite do it for me. I guess I should have also prefixed it with my variables. $sponsor1 = 'http://domain.com/image1.gif' $sponsor2 = 'http://domain.com/image2.gif' $sponsor3 = 'http://domain.com/image3.gif' $sponsor4 = 'http://domain.com/image4.gif' $sponsor5 = 'http://domain.com/image5.gif' $sponsor6 = 'http://domain.com/image6.gif' $sponsor7 = 'http://domain.com/image7.gif' PHP: if ( $sbm_sponsor1 == TRUE ) { echo "<img src=\"" . $sbm_sponsor1 . "\"/>"; } if ( $sbm_sponsor2 == TRUE ) { echo "<img src=\"" . $sbm_sponsor2 . "\"/>"; } if ( $sbm_sponsor3 == TRUE ) { echo "<img src=\"" . $sbm_sponsor3 . "\"/>"; } if ( $sbm_sponsor4 == TRUE ) { echo "<img src=\"" . $sbm_sponsor4 . "\"/>"; } if ( $sbm_sponsor5 == TRUE ) { echo "<img src=\"" . $sbm_sponsor5 . "\"/>"; } if ( $sbm_sponsor6 == TRUE ) { echo "<img src=\"" . $sbm_sponsor6 . "\"/>"; } if ( $sbm_sponsor7 == TRUE ) { echo "<img src=\"" . $sbm_sponsor7 . "\"/>"; } else { echo ''; } PHP:
So would this be considered the right way to handle it? $sponsors[0] = array('show'=>true,'image'=>$sbm_sponsor1); $sponsors[1] = array('show'=>true,'image'=>$sbm_sponsor2); $sponsors[2] = array('show'=>true,'image'=>$sbm_sponsor3); $sponsors[3] = array('show'=>true,'image'=>$sbm_sponsor4); $sponsors[4] = array('show'=>true,'image'=>$sbm_sponsor5); $sponsors[5] = array('show'=>true,'image'=>$sbm_sponsor6); $sponsors[6] = array('show'=>true,'image'=>$sbm_sponsor7); foreach($sponsors as $sponsor): if($sponsor['show']){ echo "<img src=\"" . $sponsor['image'] . "\"/>"; } endforeach; PHP: