Page 1 of 1

display certain number of banners

Posted: Mon May 20, 2013 11:49 am
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



Re: display certain number of banners

Posted: Mon May 20, 2013 12:38 pm
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.

Re: display certain number of banners - SOLVED

Posted: Mon May 20, 2013 12:44 pm
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