Seems simple enough right? I have put many hours into a solution...and nothing. I have tried multiple scripts and codes. Finally, I decided to ask for help.

Here is the background...

  • PHP web site
  • Homepage shows images from database
  • Images are pulled randomly and displayed on homepage
  • New image each time page is refreshed

My needs...

  • To continue to pull images from the database at random, AND...pull a new random image from the database every 10 seconds even if the page is not refreshed.

Makes sense? Here is my code:

<?
$dbhost = "blah blah blah"; 
$dbuname = "blah blah blah"; 
$dbpass = "blah blah blah"; 
$dbname = "blah blah blah"; 
$link = mysql_connect($dbhost, $dbuname, $dbpass);
if (!$link) 
{
    die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db($dbname);

$q="select * from `site_cars` order by RAND() limit 1";
$r=mysql_query($q);
if(!$r)
{
    echo mysql_error();
}
while($row=mysql_fetch_array($r))
{
?>

<a class=toggleopacity href="details.php?id=<?=$row['car_id']?>"><img src="upload/car_<?=$row['car_id']?>" width="625" height="370" border="0"></a>
</p>

<?
}
?>

The above code goes into the database. Pulls and displays one car from the database at random. The image of the car is displayed each time the page is refreshed. That's fine. I simply want to "refresh" the image every 10 seconds. I only want to refresh the image, not the entire page.

Can someone guide me in the right direction? Greatly appreciated!

You are tring to develop slideshow of images..or rotation at specified interval.
Here you will find many patterns for image rotation.
Dynamicdrive

Thank you for your response chintan@dani! However, Dynamic Drive is a well known web site. I use them all the time. It sounds like my needs are being overlooked. I don't need a slide show! I need to rotate images at a specific time interval. I am using PHP to pull the images from the database. The images are random and different each time the page is refreshed. I would like the images to continue being random and different each time the page is refreshed. Additionally, I would like the images to be random and different every 10 seconds...even if the page is not refreshed. You can call it whatever you want. But, the bottom line remains the same. I need help with the code, as opposed to passing the "ball" to another web site. Thanks anyways! Anyone else that is familiar with coding...please advise.

what i can tell you is if you are getting images from database, store all images in array. & using rand() function call image from within array after specified interval.
Sorry i can't post running code for you..Using javascript you can apply interval.

Perhaps you can use the download on one of my pages. You can view the demo to see whether or not it suits your needs.

Storing images in a php array is the one of the ways.
You can also get the from the folder.
OR from DB

Its all up to your taste .


Its very simple. ;)

Hi, I need to do the same thing, have looked at what Chintan posted, thank you so much, it worked very well!

I need to do something more though, i need the array of image to populate from a mysql db.

The code used from the ons sample on Chintan's post looks like this

var slideimages=new Array("topbanner/1.jpg","topbanner/2.jpg","topbanner/3.jpg")

I need the array to contain the names of images in mysql. Does anyone know of such a script

TY

You have to create arrays by using PhP and MySQL. Or you can take help by using Flash.

thanks for the advice thus far guys, the array gets populated in Java, the whole system works fine exept tht the array is populated manualy in teh java script section in a PHP page. so being a VB, VB-SCRIPT & ASP developer and wanting to learn i made teh following - what i now call mistake - lol
The mistake last night at 0:23 i coded php to link to my DB, retreive teh records display them on the working file abouve the rotating iamges, I tried to get these Echo'd names to populate my Java Array ... stupid i know lol i then went to bed and realized what if i either create an array in PHP then send it to that array .. but then i thaught is it possible that I could connected to the DB with java, like i did with PHP .. My code below

<?php

error_reporting (0);

// Connects to your Database 
 mysql_connect("localhost", "root", "") or die(mysql_error()) ; 
 mysql_select_db("pictures_db") or die(mysql_error()) ; 

 echo "Connected to Database <p>";

//Select Query for images from db
$query="SELECT id, image_filename FROM tbl_images";
$result = mysql_query($query) or die(mysql_error());  

//Loop to get image names from query
while($row = mysql_fetch_array( $result )) {

//Write image path and names on page - NOT GOING TO DO THIS IN FINAL PAGE JUST WANTED TO SEE IF NAMES WERE PULLED FROM DB
echo "topbanner/".$row['image_filename']. ","; ?><br>

<?php } ?>	

<script language="JavaScript">

//THE VAR SLIDERIMAGES BELOW IS WHERE THE ARRAY IN JAVA STARTS 
//I TYPE THEM IN MANUALY AND THE SLIDER WORK FINE, I NEED THE IMAGE NAMES TO BE RAPLACED BELOW, THE ARRY NEEDS TO RECEIVE THE NAMES FROM MYSQL

//specify images
var slideimages=new Array("topbanner/1.jpg","topbanner/2.jpg","topbanner/3.jpg")

//CODE I HAVE TRIED BELOW
//var slideimages=new Array("topbanner/" </script> <? .$row['image_filename']. ","; ?> <script language="JavaScript"> "")


//Speed of slide images
var slidespeed=3000

.....

thats where im stuck at this moment, and i dontgive up ... lol i wantto use it this way

$images = array ();
while ($row = mysql_fetch_array($result)) {
  $images[] = 'topbanner/' . $row['image_filename'];
}
$jsimages = '"' . implode('","', $images) . '"';
echo 'var slideimages = new Array(' . $jsimages . ');';

pritaeas, i think i missed what you tried to show me ..lol

Let me explain in short, I get the data from mysql via my php code. The data as yo can see looks like this when echo'd "topbanner/1.jpg","topbanner/2.jpg","topbanner/3.jpg"
this is the part of code in PHP ROW19. (or exact result form your code )

I then want to update the JAVA array with these values that come from mysql.
as you can se cleary in the code ROW29 is the array i cant update with mysql data

<script language="JavaScript"> 
//I TYPE THE IMAGE NAMES IN MANUALY AND THE SLIDER WORKS FINE, I NEED THE IMAGE NAMES ("topbanner/1.jpg","topbanner/2.jpg","topbanner/3.jpg") TO BE RAPLACED WITH THE VALUES I RECEIVE FROM MYSQL.

//specify images
var slideimages=new Array("topbanner/1.jpg","topbanner/2.jpg","topbanner/3.jpg")

I tried getting the values into my java array like this - yes im new at this lol so dont laugh, it gives me n block with red X in...

//specify images
var slideimages= new Array("<?php echo [$jsimages]; ?>") 
//=new Array("<? . $jsimages . ?>")

ty in advance if anyone knows how to get php fetch array data into new java array
Im losing hair over this .. lol

Mike

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.