TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 04-14-2005, 09:08 PM   #1 (permalink)
The Wanderer
 
Join Date: Apr 2005
Posts: 15
Thanks: 0
ammo is on a distinguished road
Default Tear Me Apart [Banner Rotation]

Hey guys well this is me doing a tear me apart.

I havent been coding long but i got this to work Its pretty simple im not sure what can be changed, If anyone can tell me better ways of coding it and why there better that would help me alot. Sometimes a banner doesnt show up, Can someone tell me why this is aswell?

Code:
<?

srand(time());
$rand = (rand()%4);


switch ($rand)
{
case 1:
    echo 'banner 1<img src = "ban1.jpg" width="150" height="150"  />';
    break;
case 3:
    echo 'banner 3<img src = "ban3.jpg" width="150" height="150"  />';
    break;
case 2:
    echo 'banner 2<img src = "ban2.jpg" width="150" height="150"  />';
    break;

}
?>
Like i said simple of simple im not that good yet, Cab anyone add any good addons to it to make it more interesting?
ammo is offline  
Reply With Quote
Old 04-14-2005, 09:23 PM   #2 (permalink)
The Acquainted
 
Join Date: Mar 2005
Posts: 177
Thanks: 0
CreativeLogic is on a distinguished road
Default

First off, it's not too bad. At least you're using switch logic and not if. That usually takes people more time to learn.

First off:
Quote:
Originally Posted by php.net
As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is now done automatically.
Second off:
It's good practice not to name variables the same as a php function, ie: rand and $rand.

Third off:
$rand = (rand()%4);
That line is going to return 4 numbers, 0-3. One thing you'll need to get used to is that a lot of things with programming start with 0 rather than 1. That's why you're getting a blank every once in a while because you have 0 being included in there and it's not being taken care of in the switch logic. So with that being said, change %4 to %3 and use numbers 0-2 rather than 1-3.

Forth off:
Kind of changing what I said before, but you could use just rand and not do that line you had before.
So do: rand(1,3)
This would return numbers 1-3 not 0-2 like I mentioned above.

Firth off:
You don't need that variable. Just put rand(1,3) in the switch statement.
Let me know if you need any furthre explaination! :)
CreativeLogic is offline  
Reply With Quote
Old 04-15-2005, 12:51 AM   #3 (permalink)
The Wanderer
 
Join Date: Apr 2005
Posts: 18
Thanks: 0
Veolus is on a distinguished road
Default

yep you should listen to Ryans tips, he's an absolute genius at PHP
__________________
Please Join >> http://www.streetballforum.com :o
Veolus is offline  
Reply With Quote
Old 04-15-2005, 07:58 PM   #4 (permalink)
The Wanderer
 
Join Date: Apr 2005
Posts: 7
Thanks: 0
edmarriner is on a distinguished road
Default

lol , all hail the almighty one :p

anywayz , keep at it and your learn php fast :D

ed :)
edmarriner is offline  
Reply With Quote
Old 04-17-2005, 11:36 AM   #5 (permalink)
The Wanderer
 
Join Date: Apr 2005
Posts: 15
Thanks: 0
ammo is on a distinguished road
Default

That is great, i didnt know i could simplify it anymore cheers for that, This will help me out alot, and i wasnt to sure about the blank page but you cleaned that up as well cheers, Im Making a minature bank script at the moment, I will post that up here when i am done. Its simple but could be fun.
ammo is offline  
Reply With Quote
Old 04-18-2005, 12:40 AM   #6 (permalink)
The Wanderer
 
Join Date: Apr 2005
Posts: 5
Thanks: 0
Sergio965 is on a distinguished road
Default

Argh, well, Ryan said everything I was gonna say plus more. To make it more interesting, you could use if commands so that when it's a certain time or a certain day, certain ads apear.

:)
Sergio965 is offline  
Reply With Quote
Old 07-17-2005, 01:40 AM   #7 (permalink)
The Acquainted
Inquisitive 
 
Join Date: Jul 2005
Location: UK
Posts: 121
Thanks: 41
Brook is on a distinguished road
Default

Slightly off-topic, but, anyone else reading this who's looking to rotate banners, phpadsnew.com is excellent. It's free and comprehensive.
Brook is offline  
Reply With Quote
Old 07-17-2005, 01:47 AM   #8 (permalink)
The Acquainted
 
Join Date: May 2005
Posts: 106
Thanks: 0
jaswinder_rana is on a distinguished road
Default

one more suggestion,
you can just put all the iamge names in array and then it can spare you one more step, which is a big switch statement. like this
PHP Code:
<?php
$imageArr 
= array('ban1.gif','ban2.jpg','ban3.gif');
$iamgeCount count($imageArr);
srand(time());
$rand rand(0,($imageCount-1));//0-2 in this case
echo '<img src = "'.$imageArr[$rand].'" width="150" height="150"  />';
?>
and to make it more easy, you can make it read adirectory and store all the image names in an array and then use that array,

this will give you flexibility like,
in the first method, you can put any number of images in array
in second, just put images in directory and it'll automatically pick them up.

and ofcourse, using database is a totally different option

hope this helps
__________________
---------------------------
Errors = Improved Programming.
Portfolio
Send a message via MSN to jaswinder_rana
jaswinder_rana is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Tear me apart... CreativeLogic General 5 04-14-2005 09:05 PM


All times are GMT. The time now is 08:27 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design