Gruzin Posted July 18, 2006 Share Posted July 18, 2006 hi guys, I want put the pictures in the Array, how can i do that? Here is my code, but of course it doesn't work:($imageArr[1] = "/pics/fish.jpg";$imageArr[2] = "/pics/robo.jpg";please help, thanks Quote Link to comment Share on other sites More sharing options...
willfitch Posted July 18, 2006 Share Posted July 18, 2006 Well, you didn't post all of your code, but the following should work:[code=php:0]<?php$imageArr = array();$imageArr[] = "/pics/fish.jpg";$imageArr[] = "/pics/robo.jpg";?>[/code]There is no need to explicitly assign numeric keys.Oh yeah, and note that you aren't actually storing the images in array, but the string location. Quote Link to comment Share on other sites More sharing options...
Gruzin Posted July 18, 2006 Author Share Posted July 18, 2006 thanks, I'll try. I want to display pictures in random way, that's why I assigned the numeric keys.... Quote Link to comment Share on other sites More sharing options...
Gruzin Posted July 18, 2006 Author Share Posted July 18, 2006 It doesn't work, here is the code:<?//array to hold the pictures$imageArr= array();$imageArr[1] = "/pics/fish.jpg";$imageArr[2] = "/pics/robo.jpg";//randomization$imageShow = rand(1, count ($imageArr));?>//to show the pictures in random way<? echo $imageArr[$imageShow]; ?> Quote Link to comment Share on other sites More sharing options...
willfitch Posted July 18, 2006 Share Posted July 18, 2006 Use array_rand().[code=php:0]<?php//array to hold the pictures$imageArr= array();$imageArr[1] = "/pics/fish.jpg";$imageArr[2] = "/pics/robo.jpg";$rand_keys = array_rand($input);echo $imageArr[$rand_keys[0]];?>[/code]That will randomize your array. Quote Link to comment Share on other sites More sharing options...
Gruzin Posted July 18, 2006 Author Share Posted July 18, 2006 no:( [color=red]Warning: array_rand(): First argument has to be an array in /users/3d_cn/html/test/index.php on line 16[/color] Quote Link to comment Share on other sites More sharing options...
willfitch Posted July 18, 2006 Share Posted July 18, 2006 I'm sorry. I had a different var name:[code=php:0]<?php//array to hold the pictures$imageArr= array();$imageArr[1] = "/pics/fish.jpg";$imageArr[2] = "/pics/robo.jpg";$rand_keys = array_rand($imageArr);echo $imageArr[$rand_keys[0]];?>[/code] Quote Link to comment Share on other sites More sharing options...
Gruzin Posted July 18, 2006 Author Share Posted July 18, 2006 on, it doesn't display any pictures, Should I try it another way? thanks anyway:) Quote Link to comment Share on other sites More sharing options...
willfitch Posted July 18, 2006 Share Posted July 18, 2006 Come on now Gruzin! [code=php:0]<?php//array to hold the pictures$imageArr= array();$imageArr[1] = "/pics/fish.jpg";$imageArr[2] = "/pics/robo.jpg";$rand_keys = array_rand($imageArr);echo '<img src="'.$imageArr[$rand_keys].'" />';?>[/code] Quote Link to comment Share on other sites More sharing options...
Gruzin Posted July 18, 2006 Author Share Posted July 18, 2006 thanks, man think it will work:) Quote Link to comment Share on other sites More sharing options...
willfitch Posted July 18, 2006 Share Posted July 18, 2006 LOL No prob. Please don't think that I was being mean; I was only playing! Quote Link to comment Share on other sites More sharing options...
Gruzin Posted July 18, 2006 Author Share Posted July 18, 2006 ye that's okay:) It doesn't display the picture corectly:( here is my directory:/html/test/picsI don't know why... Quote Link to comment Share on other sites More sharing options...
willfitch Posted July 18, 2006 Share Posted July 18, 2006 When using abs paths, you need to specify from the web root. If your web root is /html then do this:[code=php:0]<?php//array to hold the pictures$imageArr= array();$imageArr[1] = "/test/pics/fish.jpg";$imageArr[2] = "/test/pics/robo.jpg";$rand_keys = array_rand($imageArr);echo '<img src="'.$imageArr[$rand_keys].'" />';?>[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.