Jump to content

text file to array using a for loop


BandonRandon

Recommended Posts

Hello,

 

I am a graphic designer who does some programming but a lot of it is out of my realm. This is one of those cases. I have all the code written except for the last "for loop". Here is what I'm trying to do: I'm trying to use a text file to call images into a loop for displaying as gallery. the first item is the image name and the second is the description separated by a ";"

 

Here is the contents of the text file:

 

Image1.gif;Image1 Description
Image2.gif;Image2 Description
Image3.gif;Image3 Description
Image4.gif;Image4 Description

 

Here is the php code i'm trying to use:

 

$pic_info = file("$path_to_project/$pic_info_file");
$number_pics = count ($pic_info);


if (($currentPic > $number_pics)¦¦($currentPic == $number_pics)¦¦!$currentPic)
$currentPic = '0';
}


$item = preg_split('/;/', rtrim($pic_info[$currentPic]), 2);
$image_title = isset($item[1]) ? $item[1] : '';
$images .= "$item[0]";
$first_image = "$item[0]";


echo('<a href="'. $first_image . '" rel=lightbox[' . $path_to_project .'] class="highlight" title="$image_title"><img src="'. $project_thumb_url . '" width="200" /></a>');
echo("<div style=\"display:none\">");


//this code needs to loop:
echo('<a href="'. /* the remaining file names need to loop here */ . '" rel="lightbox[' . $path_to_project. '] title="'. /*description after the l needs to go here */ .'"></a>');
//end loop

 

echo("</div>");

Link to comment
https://forums.phpfreaks.com/topic/103818-text-file-to-array-using-a-for-loop/
Share on other sites

Image1.gif;Image1 Description
Image2.gif;Image2 Description
Image3.gif;Image3 Description
Image4.gif;Image4 Description

 

$pic_info = file("$path_to_project/$pic_info_file");
$number_pics = count ($pic_info);


if (($currentPic > $number_pics)¦¦($currentPic == $number_pics)¦¦!$currentPic)
$currentPic = '0';
}


$item = preg_split('/;/', rtrim($pic_info[$currentPic]), 2);
$image_title = isset($item[1]) ? $item[1] : '';
$images .= "$item[0]";
$first_image = "$item[0]";


echo('<a href="'. $first_image . '" rel=lightbox[' . $path_to_project .'] class="highlight" title="$image_title"><img src="'. $project_thumb_url . '" width="200" /></a>');
echo("<div style=\"display:none\">");


for($i=1; $i<$number_pics; $i++){
    list($image, $description) = explode(";", $pic_info[$i]);
    echo('<a href="'. $image. '" rel="lightbox[' . $path_to_project. '] title="'. $escription .'"></a>');
}
echo("</div>"); 
[/quote]

 

 

list($image, $description) = explode(";", $pic_info[$i]);

Makes $image equal to the text before the ";" and $description equal to the text after the ";".

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.