Jump to content

[SOLVED] Problem with arrays!


poleposters

Recommended Posts

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

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++;
}
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.