display certain number of banners

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

display certain number of banners

Post by nite4000 »

Hey everyone its been a while but I am in a bind.

I am developing a script and a feature in the script is to display banners and with that the admin can choose how many spots are for banners

Lets say they enter 5

and in the table there is 4 so that leaves 1 spot available

so what I need to do is display the 4 and then that 5th spot I need to display a default banner

Here is what I have so far

Code: Select all



<?php $r = mysql_query("SELECT * FROM announcement_banners") or die(mysql_error());
 if(@mysql_num_rows($r) == 0) {
	echo'<img src=../images/'.$pop['defaultbanner'].'>';
	} else {
    while($row = mysql_fetch_array($r, MYSQL_ASSOC)) {
echo'<a href='.$row['link_url'].'><img src='.$row['banner_url'].'></a></br>
</br>';
	}
	}
  ?>

now as you see I have it telling it that if its 0 to show the default banner and I know this will need to change but not sure how to code it/word it to make it do what I need as stated above.

any help would be great


User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: display certain number of banners

Post by requinix »

Your code currently outputs 1 default banner or X custom banners. Since you want X custom banners and 5-X default banners you need to rearrange the logic. First step is removing the if block since it's not a matter of "if there are banners then show them" but "show whatever is available".

Code: Select all

<?php

// to avoid deleting banners, make sure you return the results in some kind of order. probably newest first
// also limit the query so it only returns up to the limit - don't want 6 banners when you're only showing 5
$r = mysql_query("SELECT * FROM announcement_banners ORDER BY something so more recent banners come first LIMIT $number_of_banners_to_show") or die(mysql_error());
while($row = mysql_fetch_array($r, MYSQL_ASSOC)) {
    echo'<a href='.$row['link_url'].'><img src='.$row['banner_url'].'></a></br>
</br>';
}
After the custom banner loop have another loop to count out the 5-X default banners.

Code: Select all

for ($i = mysql_num_rows($r); $i < $number_of_banners_to_show; $i++) {
Inside that output the default banner.
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: display certain number of banners - SOLVED

Post by nite4000 »

thanks for the reply I will give it a shot and see what happens

UPDATE - it works perfectly thank you so much I knew it was along them lines but just could not get it out in code

Thank you so much
Post Reply