Seeking better method using loops? Not sure..

Discussion in 'PHP' started by platinum01, Mar 27, 2010.

  1. #1
    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!
     
    platinum01, Mar 27, 2010 IP
  2. guardian999

    guardian999 Well-Known Member

    Messages:
    376
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #2
    
    $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:
     
    guardian999, Mar 27, 2010 IP
  3. platinum01

    platinum01 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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:
     
    platinum01, Mar 27, 2010 IP
  4. platinum01

    platinum01 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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:
     
    platinum01, Mar 28, 2010 IP
  5. guardian999

    guardian999 Well-Known Member

    Messages:
    376
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #5
    good. yes like that.

    :)
     
    guardian999, Mar 28, 2010 IP