...

View Full Version : Help with Echo statement



winnard2008
07-25-2008, 04:43 PM
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.

ShaneC
07-25-2008, 05:17 PM
Well first and foremost it looks like you're going to have to do this kind of dealio:



<?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

//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
srand( time() );
//Chooses a number between 0 and the number of elements in the array
//minus 1 (since arrays start at 0)
$myRandomVar = rand( 0, count( $myArray ) - 1 );

$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='' />" );

}

?>

winnard2008
07-25-2008, 06:48 PM
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?

ShaneC
07-25-2008, 07:02 PM
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.

winnard2008
07-25-2008, 07:11 PM
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.

ShaneC
07-25-2008, 07:15 PM
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


$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='' />" );

}

?>

JohnDubya
07-25-2008, 07:27 PM
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.

winnard2008
07-25-2008, 07:36 PM
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.

JohnDubya
07-25-2008, 07:41 PM
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. :)

winnard2008
07-28-2008, 09:18 AM
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.




//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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum