Jump to content

Loop query inside an array


Julian

Recommended Posts

Hello guys..

 

I found this useful script that helps me rotate my banners, but I don't know how to change the array for a query from my database.  Here's the example:

 

mysql_select_db($database_xxx, $xxx);
$query_banner = sprintf("SELECT * FROM banners");
$banner = mysql_query($query_banner, $xxx) or die(mysql_error());
$totalRows_banner = mysql_num_rows($banner);

$banners = array(
"http://www.website.com,image.png",
"http://www.example.com,images/banner.jpg"
);

//I want to change what's on the array $banners for my sql results in a loop
//tried replacing for: 

$banners = array();
while (list($link, $banner1) = mysql_fetch_row($banners)) {	 
	$banners[$banner]=array('http://www.mysite.com/click.php?id='.$link.',/images/banners/'.$banner1);
}
//no luck so far.

$randnum = rand(0, count($banners) - 1); // Choose a random banner
$exploded = explode(",", $banners[$randnum]); // Separate chosen banner by ,

$url = $exploded[0]; // Grab the URL for the banner
$img = $exploded[1]; // Grab the image for the banner

// Display the banner
echo "<a href=\"$url\">";
echo "<img src=\"$img\" border=\"0\" />";
echo "</a>";

 

Any help will be highly appreciated.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.