Julian Posted June 26, 2009 Share Posted June 26, 2009 Hello gurus! Maybe someone here can help me. I'm making a rotating banner system. I found this excellent script and it works perfectly. Here's my situation. I want retrieve from the database all the banners and rotate them using this script. But as you can see I have to store the results on an array(); I really need a lead on how to do that in the script format: Here we go: <?php require_once('../../admin/Connections/red.php'); mysql_select_db($database_red, $red); $query_banner4 = "SELECT * FROM banners WHERE status = 1 AND position = 4 AND CURDATE() < termino"; $banner4 = mysql_query($query_banner4, $red) or die(mysql_error()); $row_banner4 = mysql_fetch_assoc($banner4); $totalRows_banner4 = mysql_num_rows($banner4); // an array of banners $banners = array ( '<a href="http://www.mysite..com/click.php?id=' . $row_banner4['link'] . '" target="_blank"><img src="/images/banners/' . $row_banner4['banner1'] .'" alt="' . $row_banner4['cliente'] . '" border="0" /></a>' ); // pick a random one $html = $banners[array_rand($banners)]; // send XML headers header('Content-type: text/xml'); echo '<?xml version="1.0" ?>'; // print the XML response ?> <banner> <content><?php echo htmlentities($html); ?></content> <reload>5000</reload> </banner> On $banners I have to loop for the records (maybe while or foreach) but I don't know how to put the result in the right format. On the script it works fine but is not looping, only get the first record. Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/163713-loop-inside-an-array/ Share on other sites More sharing options...
Ken2k7 Posted June 26, 2009 Share Posted June 26, 2009 Try this SQL instead of the one you have - SELECT * FROM banners WHERE status = 4 AND position = 4 AND CURDATE() < termino ORDER BY RAND() LIMIT 1; That should give a more random effect. As for looping, either while or foreach works. And I don't know what hyou mean by the right format. Link to comment https://forums.phpfreaks.com/topic/163713-loop-inside-an-array/#findComment-863828 Share on other sites More sharing options...
Julian Posted June 26, 2009 Author Share Posted June 26, 2009 // an array of banners $banners = array ( '<a href="http://www.costaricamd.net/admin/ccount/click.php?id=' . $row_banner4['link'] . '" target="_blank"><img src="/images/banners/' . $row_banner4['banner1'] .'" alt="' . $row_banner4['cliente'] . '" border="0" /></a>' ); Thanks for looking. How can I loop all the results inside the $banners = array(.... The code above only shows the first record on the database. I need to loop for all the records and put them in that format. Like $banners = array( foreach... Thanks again Link to comment https://forums.phpfreaks.com/topic/163713-loop-inside-an-array/#findComment-863833 Share on other sites More sharing options...
Julian Posted June 26, 2009 Author Share Posted June 26, 2009 Anyone? In short words i'm trying to fill the array with mysql results. The problem here is to loop with the array format in order to work with the rest of the script. Thanks Link to comment https://forums.phpfreaks.com/topic/163713-loop-inside-an-array/#findComment-863873 Share on other sites More sharing options...
xtopolis Posted June 26, 2009 Share Posted June 26, 2009 <?php require_once('../../admin/Connections/red.php'); mysql_select_db($database_red, $red); $banners = array(); $query_banner4 = "SELECT * FROM banners WHERE status = 1 AND position = 4 AND CURDATE() < termino"; $banner4 = mysql_query($query_banner4, $red) or die(mysql_error()); // $row_banner4 = mysql_fetch_assoc($banner4); $totalRows_banner4 = mysql_num_rows($banner4); // an array of banners while($row_banner4 = mysql_fetch_assoc($banner4)) { $banners[] = '<a href="http://www.mysite..com/click.php?id=' . $row_banner4['link'] . '" target="_blank"><img src="/images/banners/' . $row_banner4['banner1'] .'" alt="' . $row_banner4['cliente'] . '" border="0" /></a>'; } // pick a random one // you could probably use shuffle() here; $html = $banners[array_rand($banners)]; // send XML headers header('Content-type: text/xml'); echo '<?xml version="1.0" ?>'; ?> // print the XML response <banner> <content><?php echo htmlentities($html); ?></content> <reload>5000</reload> </banner> ?> Something like this? shuffle for the array_rand part. Link to comment https://forums.phpfreaks.com/topic/163713-loop-inside-an-array/#findComment-863880 Share on other sites More sharing options...
Julian Posted June 26, 2009 Author Share Posted June 26, 2009 Maybe I explained myself the wrong way. I don't care about the the random image. I need to populate the array with mysql results in this format: $banner = array ( result1, result2, result3, etc ); thanks Link to comment https://forums.phpfreaks.com/topic/163713-loop-inside-an-array/#findComment-864042 Share on other sites More sharing options...
xtopolis Posted June 26, 2009 Share Posted June 26, 2009 Did you even try my code? I was attempting that with this section: while($row_banner4 = mysql_fetch_assoc($banner4)) { $banners[] = '<a href="http://www.mysite..com/click.php?id=' . $row_banner4['link'] . '" target="_blank"><img src="/images/banners/' . $row_banner4['banner1'] .'" alt="' . $row_banner4['cliente'] . '" border="0" /></a>'; } As well as 1-2 modifications elsewhere. Link to comment https://forums.phpfreaks.com/topic/163713-loop-inside-an-array/#findComment-864198 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.