Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jul 2008
    Location
    Blackpool, UK
    Posts
    176
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Help with Echo statement

    Hi Guys,


    I am new to PHP been on it about 3 weeks now.

    I have a script that generates a random image.

    Now the images i will be using are banner ads so will be either gif or swf files.

    Now I know that in order to pull out a swf file i need all the embed stuff, which is fine. and the gif is the <img src blah blah.


    But what would the code be if it is generating a gif then when you refresh it goes to a swf, how would I code the echo part to decipher between the two?

    I have been looking everywhere for an answer so you guys are my last hope.

  • #2
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    659
    Thanks
    31
    Thanked 100 Times in 94 Posts
    Well first and foremost it looks like you're going to have to do this kind of dealio:

    PHP Code:
    <?php
     
    echo( "<embed src='" $myFile "' width='300' height='300' />" );
     
    //OR
     
    echo( "<img src='" $myFile "' alt='' />" );
     
    ?>
    Now it seems like your prediciment is getting it to switch the statement. This is all I can really think of right now, but there probably is another more efficent way. This should work, though.

    I am going to assume you've got all your files in an array.

    PHP Code:
    <?php
     
    //Puts all your files in a dynamic array we can use
    $myArray = array( "apple.gif""orange.gif""banana.swf""grape.gif""pear.swf" );
     
    //Seeds the random to come
    srandtime() );
    //Chooses a number between 0 and the number of elements in the array
    //minus 1 (since arrays start at 0)
    $myRandomVar rand0count$myArray ) - );
     
    $myFile $myArray[$myRandomVar];
     
    //If it finds .swf in the file name, it will embed it this way
    if( strstr$myFile".swf" ) ){
     
          echo( 
    "<embed src='" $myFile "' width='300' height='300' />" );
     
    //If not, it will be an image
    }else{
     
          echo( 
    "<img src='" $myFile "' alt='' />" );
     
    }
     
    ?>
    Last edited by ShaneC; 07-25-2008 at 05:39 PM.
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • #3
    Regular Coder
    Join Date
    Jul 2008
    Location
    Blackpool, UK
    Posts
    176
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Hi shane,


    You bit of code is actually alot cleaner and less fussier than the one i have got, and works great.

    As I was only testing this out to get both gif and swf randomly generating,

    Now the problem i have is that all these banners will be coming out of the dataase, our webiste works in postcode order therefore everything is in the database included randomly generated area banners.


    Would i use the same script as you provided but instead of the array simply query the database for the banner ID???


    Or does it get a whole lot more complicated when the database is involved?

  • #4
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    659
    Thanks
    31
    Thanked 100 Times in 94 Posts
    You could populate the array from the database, just drop the sources of the files in the array. Without seeing your database hierarchy it's tough to specifically name a method. If you want to post how your database is organized and how you are retrieving the information from your database in PHP I can be a bit clearer.
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • #5
    Regular Coder
    Join Date
    Jul 2008
    Location
    Blackpool, UK
    Posts
    176
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Hi Shane,

    The randomly generated banner at my work is already setup and works great, the only problem is that it currently does not allow SWF files, I think because it is using phpthumb.


    I could use the code already in place in terms of getting the info out of the database, what i need to know now is how i would set it up to pull the swf files.

    Or is that part already done in the echo stateent?????

    I think at the moment it pulls a banner out depending on its area id i think.

  • #6
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    659
    Thanks
    31
    Thanked 100 Times in 94 Posts
    Okay, so basically the selection from the database is already working fine? And you're echoing them fine right now, but when you pull a SWF from the database you are echoing it as an image?

    If so, just use this segment of code:

    PHP Code:
    <?php
     
     
    $myFile 
    //Put whatever you've gotten from the DB here
     
    if( strstr$myFile".swf" ) ){
     
          echo( 
    "<embed src='" $myFile "' width='300' height='300' />" );
     
    }else{
     
          echo( 
    "<img src='" $myFile "' alt='' />" );
     
    }
     
    ?>
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • #7
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    From the PHP manual on strstr():

    Note: If you only want to determine if a particular needle occurs within haystack, use the faster and less memory intensive function strpos() instead.

  • #8
    Regular Coder
    Join Date
    Jul 2008
    Location
    Blackpool, UK
    Posts
    176
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks guys you have been a great help.


    I am learning more from forums than from books. So hank you to everybody for learning php and then teaching me.

  • #9
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    Quote Originally Posted by winnard2008 View Post
    Thanks guys you have been a great help.


    I am learning more from forums than from books. So hank you to everybody for learning php and then teaching me.
    Same way I learned too. I just dove in head first with no books and started coding...and when I got to a place where I couldn't continue, I would first Google for help, or second, post on a forum. Best way to learn, IMO.

  • #10
    Regular Coder
    Join Date
    Jul 2008
    Location
    Blackpool, UK
    Posts
    176
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Hi guys,

    Just got to work this morning, and looking at implementing the code you guys gave me on Friday.

    However, when I have looked at the code our former developer has put in I have noticed it is a big chunk of code he is using to generate the banners.

    He has set it to find up to ten banners to randomly generate, then if no area banners are found it pulls out national banners to make the difference up to ten.

    On the page there is also a javascript pop up which generates info on all the sponsors of that area. IE Companies who have banners.

    I am not sure which of the code I need to put into the code you guys supplied as it is a very over complicated bit of code being used.


    Here is all the code related to the banners which i want to add swf files too.


    PHP Code:
    //now get side adverts
     
    $banners mysql_query("Select * From theorytestbanners Where area = '$postcode' AND sidebanner = '1' Order By Rand() Limit 10"); $bancount mysql_num_rows($banners);
     
     
    $output '<script language="JavaScript" type="text/JavaScript">';
     
    $output $output.' var img = new Array(12); var logo = new Array(12); var names = new Array(12); var address = new Array(12);  var links = new Array(12); var telephone = new Array(12); var email = new Array(12); var postcode = new Array(12);';
      
     
    $count 0;
      
     while(
    $banner mysql_fetch_array($banners)){
        
    $count $count 1;
        
        
    //get db info
        
    $logo $banner['logoimage'];
        
    $sidebanner $banner['bannerimage'];
        
    $sidebannername $banner['name'];
        
    $sidebanneraddress $banner['address'];
        
    $sidebannerpostcode $banner['postcode'];
        
    $sidebannertelephone $banner['telephone'];
        
    $sidebanneremail $banner['email'];
        
    $sidebannerlink $banner['url'];
              
        
    //generate javascript      
        
    $output $output.'logo['.$count.'] = "'.$logo.'"; ';
        
    $output $output.'img['.$count.'] = "'.$sidebanner.'"; ';
        
    $output $output.'names['.$count.'] = "'.$sidebannername.'"; ';
        
    $output $output.'links['.$count.'] = "'.$sidebannerlink.'"; ';
        
    $output $output.'email['.$count.'] = "'.$sidebanneremail.'"; ';
        
    $output $output.'telephone['.$count.'] = "'.$sidebannertelephone.'"; ';    
        
    $output $output.'address['.$count.'] = "'.$sidebanneraddress.'"; ';
        
    $output $output.'postcode['.$count.'] = "'.$sidebannerpostcode.'"; ';
        
        
          
      }
    //end of while loop
      
      //if there are not 10 banners for the area, find some site banners to make up the difference
      
      
    if($count 10){
        
    $required 10 $count;
        
        
        
    $banners mysql_query("Select * From theorytestbanners Where sidebanner = '1' AND siteadvert = '1' Order By Rand() Limit $required"); 
        
        
    $bc mysql_num_rows($banners);
        
        
        while(
    $banner mysql_fetch_array($banners)){
          
    $count $count 1;
          
    //get banner info from database
          
    $logo $banner['logoimage'];
          
    $sidebanner $banner['bannerimage'];
          
    $sidebannername $banner['name'];
          
    $sidebanneraddress $banner['address'];
          
    $sidebannerpostcode $banner['postcode'];
          
    $sidebannertelephone $banner['telephone'];
          
    $sidebanneremail $banner['email'];
          
    $sidebannerlink $banner['url'];
          
          
    //generate javascript      
          
    $output $output.'logo['.$count.'] = "'.$logo.'"; ';
          
    $output $output.'img['.$count.'] = "'.$sidebanner.'"; ';
          
    $output $output.'names['.$count.'] = "'.$sidebannername.'"; ';
          
    $output $output.'links['.$count.'] = "'.$sidebannerlink.'"; ';
          
    $output $output.'email['.$count.'] = "'.$sidebanneremail.'"; ';
          
    $output $output.'telephone['.$count.'] = "'.$sidebannertelephone.'"; ';    
          
    $output $output.'address['.$count.'] = "'.$sidebanneraddress.'"; ';
          
    $output $output.'postcode['.$count.'] = "'.$sidebannerpostcode.'"; ';
        }
    //end of while loop
      
    }//end of if $count < 10
      
      
    $output $output.'</script>';//close script tag and output function
      
    echo $output
    If anybody knows if i can get rid of any of this or what I need to add to the code you provided then that would be a big help.
    Last edited by winnard2008; 07-28-2008 at 09:21 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •