tags:

views:

51

answers:

3

i want to create dynamic banner rotater wih php ajax i want to pass the mysql_fetch_array() to an array to create a new array() to create xml response..........

here is my code

$sql = mysql_query("SELECT * FROM ads");
header('Content-type: text/xml');
echo '<?xml version="1.0" ?>';
while($row = mysql_fetch_array($sql)){
    $title = $row['title'];
    $img = $row['file'];
    $body = $row['body'];
    $ban = '<b>'.$title.'</b><br><br><img src="ads/'.$img.'"><br><br>'.$body;

    $banners = array(    
      $ban,    
    );    
    $html = $banners[array_rand($banners)];

}

<banner>
    <content><?php echo htmlentities($html); ?></content>
    <reload>3000</reload>    
</banner>

but it is return only one ad not return multiple ads how can i fix that

A: 

Use this in the while loop:

$banners[] = $ban

Instead of

$banners = array(

                $ban,

);

and

$html = array_rand($banners);

instead of

$html = $banners[array_rand($banners)];
codaddict
A: 

And to what bzabhi said, define your

$banners = array();

before the while loop, and the randomization part has to go after the loop.

styts
A: 

The proble is here: $banners = array($ban);. What you're trying to do is include all the ads in the $banners array as an entry but you are failing to achieve that.

The correct code for including an entry in an array would be $banner[] = $ban. That way each ad that comes as a result from your query will be stored as an individual entry.

So the correct code would be:

$sql = mysql_query("SELECT * FROM ads");
$banner = array(); //Define the array before trying to add elements.

header('Content-type: text/xml');

while($row = mysql_fetch_array($sql))
{
    $title = $row['title'];
    $img = $row['file'];
    $body = $row['body'];
    $ban = '<b>'.$title.'</b><br><br><img src="ads/'.$img.'"><br><br>'.$body;

    $banner[] = $ban;                //Adding a new entry at the end.
    $html = array_rand($banner);    //Getting a random entry.

}
johnnyArt
thank a lot man i have solved my problem
Web Worm
No problem, and merry Christmas.
johnnyArt