Jump to content

Archived

This topic is now archived and is closed to further replies.

Gruzin

Need help!

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
thanks, I'll try. I want to display pictures in random way, that's why I assigned the numeric keys....

Share this post


Link to post
Share on other sites
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]; ?>

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
on, it doesn't display any pictures, Should I try it another way? thanks anyway:)

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
LOL No prob.  Please don't think that I was being mean; I was only playing!

Share this post


Link to post
Share on other sites
ye that's okay:) It doesn't display the picture corectly:( here is my directory:
/html/test/pics

I don't know why...

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.