poleposters Posted March 24, 2008 Share Posted March 24, 2008 Hi, I have s php slideshow script which I'm trying to modify so that all the slide URLs are pulled from a database instead of manually coded. Here is the original script. <?php //include slideshow.php in your script include "slideshow.php"; //add 3 slides $slideshow[ 'slide' ][ 0 ] = array ( 'url' => "images/plant0.jpg" ); $slideshow[ 'slide' ][ 1 ] = array ( 'url' => "images/plant1.jpg" ); $slideshow[ 'slide' ][ 2 ] = array ( 'url' => "images/plant2.jpg" ); //send the slideshow data Send_Slideshow_Data ( $slideshow ); ?> My approach has been to grab the data from the database and store it into an array then run a loop. As below. $query = 'SELECT slide FROM slideshow WHERE business_id=2 '; $result = mysql_query ($query); $numlinks=mysql_num_rows($result); while($row = mysql_fetch_array ($result)){ $slideshow[ 'slide' ][ 0 ] = array ( 'url' => "$row[0]" ); When I run this query I only get one result? What am I doing wrong? Link to comment https://forums.phpfreaks.com/topic/97600-solved-problem-with-arrays/ Share on other sites More sharing options...
cooldude832 Posted March 24, 2008 Share Posted March 24, 2008 because you are constantly rewritten the same array index 0 try <?php $query = 'SELECT slide FROM slideshow WHERE business_id=2 '; $result = mysql_query ($query); $numlinks=mysql_num_rows($result); if($numlinks >0){ $i = 0; while($row = mysql_fetch_assoc($result)){ $slideshow['slide'][$i] = array ( 'url' => "$row['slide']" ); $i++; } } ?> Link to comment https://forums.phpfreaks.com/topic/97600-solved-problem-with-arrays/#findComment-499377 Share on other sites More sharing options...
poleposters Posted March 24, 2008 Author Share Posted March 24, 2008 Thanks cooldude. That sorted the problem, Cheers. Link to comment https://forums.phpfreaks.com/topic/97600-solved-problem-with-arrays/#findComment-499395 Share on other sites More sharing options...
Barand Posted March 24, 2008 Share Posted March 24, 2008 You seem to be over-complicating things. Wouldn't this suffice <?php while($row = mysql_fetch_assoc($result)){ $slideshow[] = $row['slide']; } Link to comment https://forums.phpfreaks.com/topic/97600-solved-problem-with-arrays/#findComment-499453 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.