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 Link to comment https://forums.phpfreaks.com/topic/14940-need-help/ 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. Link to comment https://forums.phpfreaks.com/topic/14940-need-help/#findComment-59931 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.... Link to comment https://forums.phpfreaks.com/topic/14940-need-help/#findComment-59933 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]; ?> Link to comment https://forums.phpfreaks.com/topic/14940-need-help/#findComment-59936 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. Link to comment https://forums.phpfreaks.com/topic/14940-need-help/#findComment-59937 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] Link to comment https://forums.phpfreaks.com/topic/14940-need-help/#findComment-59939 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] Link to comment https://forums.phpfreaks.com/topic/14940-need-help/#findComment-59941 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:) Link to comment https://forums.phpfreaks.com/topic/14940-need-help/#findComment-59943 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] Link to comment https://forums.phpfreaks.com/topic/14940-need-help/#findComment-59945 Share on other sites More sharing options...
Gruzin Posted July 18, 2006 Author Share Posted July 18, 2006 thanks, man think it will work:) Link to comment https://forums.phpfreaks.com/topic/14940-need-help/#findComment-59946 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! Link to comment https://forums.phpfreaks.com/topic/14940-need-help/#findComment-59947 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... Link to comment https://forums.phpfreaks.com/topic/14940-need-help/#findComment-59950 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] Link to comment https://forums.phpfreaks.com/topic/14940-need-help/#findComment-59964 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.