Jump to content

[SOLVED] How to I save a while loop into a string?


pneudralics

Recommended Posts

I'm trying to save $imagesubject2 into it's own string outside of the while loop. I want to be able to display all the data from $imagesubject2 outside of the loop.

//List files
while ($item = readdir ($dp) ) {
if ( (is_file ($item)) AND (substr($item, 0, 1) != '.') ) {
//Get file size
$fs = filesize ($item);

//Get file modification date
$lm = date ('F j, Y' , filemtime($item));

	//Find image
	$imagepattern = '/^([0-9]){1,}.jpg$/';
	$imagesubject = "$item";

	if (preg_match($imagepattern, $imagesubject, $imagematches)) {
		//Open file
		$imagesubject2 = "$imagesubject";

	}
}
}

if you want to display whats inside imagesubject2 outside the loop then echo it outside the loop.... I dont understand your question here

 

I can't get it to echo everything outside the loop. It only echos 1 data and not all of them if I echo it outside the loop. So I'm thinking maybe if I can save it to a string inside the loop and echo the string outside it might work.

$imagesubject2 = array();
while ($item = readdir ($dp) ) {
   if ( (is_file ($item)) AND (substr($item, 0, 1) != '.') ) {
   //Get file size
   $fs = filesize ($item);
   
   //Get file modification date
   $lm = date ('F j, Y' , filemtime($item));

      //Find image
      $imagepattern = '/^([0-9]){1,}.jpg$/';
      $imagesubject = "$item";

      if (preg_match($imagepattern, $imagesubject, $imagematches)) {
         //Open file
         $imagesubject2[] = "$imagesubject";

      }
   }
}
$i = 0;
foreach($imagesubject2 as $sub){
echo "$i : $sub<br />";
$i++;
}

 

that should do the trick. if you want an explanation let me know

 

Archived

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

×
×
  • 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.