Printing Array Object names in .php page

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
kosaic
Forum Newbie
Posts: 2
Joined: Thu Dec 03, 2009 9:09 am

Printing Array Object names in .php page

Post by kosaic »

pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


I have this code running, and it works perfectly … however, see my bottom bit about what I see when I view source, versus what I want to see … I am having image re-sizing issues that started when I use this code. When I hard code img tags directly to the images, all works fine.


rotate.php

Code: Select all

 
<?php 
$images=array( 
 "darin_1.jpg",  "darin_2.jpg",  "jeffb_1.jpg",  "jeffb_2.jpg", "jeffb_3.jpg",  "glenda_1.jpg", "glenda_2.jpg",  "glenda_3.jpg", "glenda_4.jpg",  "kevin_1.jpg", "kevin_2.jpg", "kevin_3.jpg", "kevin_4.jpg", "petrina_1.jpg", "petrina_2.jpg",  "mia_1.jpg", "mia_2.jpg", "dale_1.jpg", "paul_1.jpg", "raino_1.jpg", "raino_2.jpg", "raino_3.jpg", "sherri_1.jpg", "sherri_2.jpg"
 ); 
$total=count($images); 
$secondsFixed=5; // seconds to keep list the same 
$seedValue=(int)(time()/$secondsFixed); 
srand($seedValue); 
for ($i=0;$i<$total;++$i) // shuffle list 'randomly' 
{ 
 $r=rand(0,$total-1); 
 $temp =$images[$i]; 
 $images[$i]=$images[$r]; 
 $images[$r]=$temp; 
} 
$index=(int)($_GET['i']); // image index passed in 
$i=$index%$total; // make sure index always in bounds 
$file=$images[$i]; 
header("Location: $file"); // and pass file reference back 
?>
 

In my PHP page I have:

Code: Select all

 
<img src="profiles/images/rotate.php?i=1" alt="" border="1" width="90" height="90"/>
<img src="profiles/images/rotate.php?i=2" alt="" border="1" width="90" height="90"/>
<img src="profiles/images/rotate.php?i=3" alt="" border="1" width="90" height="90"/>
Etc ….
 
When I view Source, I see: (for every one of them …)

Code: Select all

 
<img src="profiles/images/rotate.php?i=1" alt="" border="1" width="90" height="90"/>
<img src="profiles/images/rotate.php?i=2" alt="" border="1" width="90" height="90"/>
<img src="profiles/images/rotate.php?i=3" alt="" border="1" width="90" height="90"/>
Etc….
 
I want to see the actual image name (darin_1.jpg for example) in the line immediately about this.

Code: Select all

 
<img src="profiles/images/darin_1.jpg" alt="" border="1" width="90" height="90"/>
<img src="profiles/images/darin_2.jpg" alt="" border="1" width="90" height="90"/>
<img src="profiles/images/jeffb_1.jpg" alt="" border="1" width="90" height="90"/>
 
How do I print the value of that array object in my <img> tag, as opposed to it displaying "rotate.php?i=1" ??


pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Printing Array Object names in .php page

Post by pickle »

First off, look into shuffle() for randomizing your array - much easier.

Second, you're looking in the wrong place. The source code that references rotate.php as an image is already generated before rotate.php runs. You need to be looking at the file that generates the <img> tags & get it to output the filenames explicitely.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
kosaic
Forum Newbie
Posts: 2
Joined: Thu Dec 03, 2009 9:09 am

Re: Printing Array Object names in .php page

Post by kosaic »

Second, you're looking in the wrong place. The source code that references rotate.php as an image is already generated before rotate.php runs. You need to be looking at the file that generates the <img> tags & get it to output the filenames explicitely.
I'm not following at all ....

the file that generates the <img> tags is "index_img.php" which has the image tags I referenced above in it.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Printing Array Object names in .php page

Post by pickle »

Right. It's index_img.php that is generating the <img> tags. So it's that file that you need to modify to show the image names rather than rotate.php. Basically a lot of the logic in rotate.php will need to be moved into index_img.php. You can't have rotate.php change the <img> tag, as it's being treated as a separate HTTP request (ie: it's an image).
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply