Jump to content


Photo

Need help!


  • Please log in to reply
12 replies to this topic

#1 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 18 July 2006 - 03:35 PM

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
I don't need your script, I'll try to write it myself

#2 willfitch

willfitch
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts
  • LocationNew Hope, PA

Posted 18 July 2006 - 03:38 PM

Well, you didn't post all of your code, but the following should work:

<?php
$imageArr = array();
$imageArr[] = "/pics/fish.jpg";
$imageArr[] = "/pics/robo.jpg";
?>
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.
Zend Certified Engineer (ZEND001989)
www.willfitch.com

#3 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 18 July 2006 - 03:40 PM

thanks, I'll try. I want to display pictures in random way, that's why I assigned the numeric keys....
I don't need your script, I'll try to write it myself

#4 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 18 July 2006 - 03:43 PM

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]; ?>
I don't need your script, I'll try to write it myself

#5 willfitch

willfitch
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts
  • LocationNew Hope, PA

Posted 18 July 2006 - 03:48 PM

Use array_rand().

<?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]];
?>
That will randomize your array.
Zend Certified Engineer (ZEND001989)
www.willfitch.com

#6 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 18 July 2006 - 03:53 PM

no:( Warning: array_rand(): First argument has to be an array in /users/3d_cn/html/test/index.php on line 16
I don't need your script, I'll try to write it myself

#7 willfitch

willfitch
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts
  • LocationNew Hope, PA

Posted 18 July 2006 - 03:56 PM

I'm sorry.  I had a different var name:
<?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]];
?>

Zend Certified Engineer (ZEND001989)
www.willfitch.com

#8 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 18 July 2006 - 03:59 PM

on, it doesn't display any pictures, Should I try it another way? thanks anyway:)
I don't need your script, I'll try to write it myself

#9 willfitch

willfitch
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts
  • LocationNew Hope, PA

Posted 18 July 2006 - 04:00 PM

Come on now Gruzin! 
<?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].'" />';
?>

Zend Certified Engineer (ZEND001989)
www.willfitch.com

#10 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 18 July 2006 - 04:08 PM

thanks, man think it will work:)
I don't need your script, I'll try to write it myself

#11 willfitch

willfitch
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts
  • LocationNew Hope, PA

Posted 18 July 2006 - 04:09 PM

LOL No prob.  Please don't think that I was being mean; I was only playing!
Zend Certified Engineer (ZEND001989)
www.willfitch.com

#12 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 18 July 2006 - 04:13 PM

ye that's okay:) It doesn't display the picture corectly:( here is my directory:
/html/test/pics

I don't know why...
I don't need your script, I'll try to write it myself

#13 willfitch

willfitch
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts
  • LocationNew Hope, PA

Posted 18 July 2006 - 04:47 PM

When using abs paths, you need to specify from the web root.  If your web root is /html then do this:

<?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].'" />';
?>

Zend Certified Engineer (ZEND001989)
www.willfitch.com




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users