having problem with random images

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phpchild
Forum Newbie
Posts: 20
Joined: Mon Jul 31, 2006 6:27 am

having problem with random images

Post 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]
PapiSolo
Forum Newbie
Posts: 24
Joined: Wed Aug 09, 2006 12:16 pm

PHP array

Post 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.
phpchild
Forum Newbie
Posts: 20
Joined: Mon Jul 31, 2006 6:27 am

Post 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
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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);
phpchild
Forum Newbie
Posts: 20
Joined: Mon Jul 31, 2006 6:27 am

Post 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
PapiSolo
Forum Newbie
Posts: 24
Joined: Wed Aug 09, 2006 12:16 pm

array

Post 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. ;)
Post Reply