Page 1 of 1

having problem with random images

Posted: Sat Aug 12, 2006 5:05 am
by phpchild
onion2k | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

hi 

  i want dispaly images randomly .i have wriiten code for this. but its not working properly. i don't know what error in this.

  my database is look like

in a single id i stored multiple image file names.

id      images
10 bikes/b5.jpg,bikes/b5.jpg,bikes/b9.jp... 
11 bikes/b5.jpg,bikes/b5.jpg,bikes/b9.jp... 

-------------------

Code: Select all

<? mysql_connect("localhost", "root", "");
mysql_select_db("img");
$rangid=$_REQUEST['randid'];
 $sql="select * from newimg where imgid='$randid'";
$res=mysql_query($sql);

$row=mysql_fetch_array($res);

 $image=$row['images'];
$tmp=explode(",",$image);
foreach($tmp as $value)
{
//echo $value."<br>";
srand((float) microtime() * 10000000);
$tmp1=array_rand($value,1);
echo $value[$tmp1[0]];
}?>
onion2k | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

PHP array

Posted: Sat Aug 12, 2006 5:32 am
by PapiSolo
Hey,

I'm not much of a Guru (or anything close...), but if you just want to display random images, then you don't really need MySQL. You can just create an array ussing php, shuffle it using

Code: Select all

shuffle()
and then increment through the array.

Posted: Sat Aug 12, 2006 5:36 am
by phpchild
thanks for ur reply papi.

but first i have to split single images using explode functions. then only i should display images randomly

Posted: Sat Aug 12, 2006 5:54 am
by impulse()
Isn't it possible to do this. For example if you have 40 images and they are named 1.jpg, 2.jpg etc

Code: Select all

$imageNum = rand(0,40);

<img src = "http://yourdomain.com/$imageNum.jpg">
I know that img src tag needs a little more work but you get the idea.

Let me know if this helps. Ste

Posted: Sat Aug 12, 2006 6:55 am
by onion2k
Define "its not working properly.". What is happening?

If it's displaying the images in the same order every time, remove the line: srand((float) microtime() * 10000000);

Posted: Sat Aug 12, 2006 7:09 am
by phpchild
nothing display, because in my database i have store multiple images in a single field to put ",".

so i used explode function.

so after its stored without spaces. then how i can display random images. any help please

array

Posted: Sat Aug 12, 2006 10:11 am
by PapiSolo
After you have your images separated with the explode function, create an array and populate it with the images, shuffle and increment through it.

It should work... I think. ;)