Jump to content

[SOLVED] Adding value to end of array with while loop...having problems


atticus

Recommended Posts

Alright, so I declared the array...I put an initial value in the array.

 

I am using a while loop to add each other element into the array

 

<?php

$img = array("<!--start of hidden images for quicker load time -->");
?>

<div class="preview"><a href="preview.php?number=<?php echo $number ?>" target="_blank">
<img src="http://www.domain.com/screenshots/<?php echo $number; ?>-m.jpg" alt="<?php echo $title; ?>" width="145" border="0"  onmouseover="showtrail(250,250,'screenshots/<?php echo $number; ?>-b.jpg');" onmouseout="hidetrail();"/> <img class="button" border="0" title="preview" src="images/preview.png" alt="preview" width="88" height="22" /></a></div>

<?php
var_dump($img);
$img[] = "<img src=\"http://www.domain.com/screenshots/".$number."-b.jpg\" class=\"hidden\" />";  
} 

 

The output I am getting is only the first and last values in the array. The comment I added first, and the last run of the while loop:

 

code to get it:

 

foreach ($img as $b) {
    echo $b;
}

 

the output:

 

<!--start of hidden images for quicker load time --><img src="http://www.domain.com/screenshots/24367-b.jpg" class="hidden" />

 

this is the result of a var dump:

 

array(2) {

  [0]=>

  string(52) "<!--start of hidden images for quicker load time -->"

  [1]=>

  string(89) "<img src="http://www.websitestarterpackage.com/screenshots/24367-b.jpg" class="hidden" />"

}

 

 

Link to comment
Share on other sites

There is something about this place...I posted the question that I was stumped on for like 30 minutes...and the answer is right there in front of me...I declared the array inside of the while loop.

 

Thats what I get for working on this with 3 hours sleep for the last 10 hours.

 

Thanks anyway :)

Link to comment
Share on other sites

If all of that is encased in a while loop (you should have included it, btw) then it's because your redefining $img each time it loops, so it only exists after the last iteration of the loop. Move the $img definition outside of the loop and you should be fine. If that's not your problem, could you show us the full code including the loop and definition of these variables you are using?

 

Nevermind, glad you got it working :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.