Foreach loop depending on files in directory
Is there a way to create a foreach loop, that will run for however many instances of a filename with a sequential number exists in a directory?
For example I have one directory with images. The images will have filenames such as these:
firstname-lastname_before1.jpg
firstname-lastname_after1.jpg
firstname-lastname_before2.jpg
firstname-lastname_after2.jpg
firstname2-lastname2_before1.jpg
firstname2-lastname2_after1.jpg
firstname2-lastname2_before2.jpg
firstname2-lastname2_after2.jpg
This essentially shows photos belonging to two people. Each person has four photos, two sets of two photos, a before and after.
I can already handle the individual names when echoing out, but I'm trying to find a way to set a foreach loop that will find all sequential instances of the condition that's currently applied.
ie If I'm viewing a page for one person, the foreach loop should run through all files relating to that person.
At the moment I just have one hard-coded instance of the block I want to loop.
<div class="photo-set col-md-4">
<figure class="cd-image-container">
<?php $procedure = get_post_custom_values('Procedure'); ?>
<img src="<?php echo get_template_directory_uri()."/img/before-after/".preg_replace("/[s_]/", "-", strtolower($procedure[0]))."_".preg_replace("/[s_]/", "-", strtolower($post->post_title))."_after1.jpg";?>" alt="Original Image">
<span class="cd-image-label" data-type="original">After</span>
<div class="cd-resize-img">
<img src="<?php echo get_template_directory_uri()."/img/before-after/".preg_replace("/[s_]/", "-", strtolower($procedure[0]))."_".preg_replace("/[s_]/", "-", strtolower($post->post_title))."_before1.jpg";?>" alt="Modified Image">
<span class="cd-image-label" data-type="modified">Before</span>
</div>
<span class="cd-handle"></span>
</figure>
</div>
This is just using before1 and after1, as I wanted to style it up first.
So to summarise, my question is: Can I have a foreach loop that will find all files that match the condition below, that are sequentially named, and sort them into a set of 2 (for before and after)?
UPDATE:
I've tried using the glob function, but I'm really not sure this is what I'm looking. After using this code:
$procedures = get_post_custom_values('Procedure');
$procedure = preg_replace("/[s_]/", "-", strtolower($procedures[0]));
$patientName = preg_replace("/[s_]/", "-", strtolower($post->post_title));
foreach(glob('img/before-after/*$patientName*/*.jpg') as $filename) {
Nothing is being returned on the page. I'm not sure on how the glob expression should be. As it needs to firstly find a directory that matches $patientName, and then inside there are loads of files with different procedures in the title. Like this for an example:
tummy-tuck_john-smith_before1.jpg
tummy-tuck_john-smith_after1.jpg
tummy-tuck_john-smith_before2.jpg
tummy-tuck_john-smith_after2.jpg
nose-reshaping_john-smith_before1.jpg
nose-reshaping_john-smith_after1.jpg
etc. So how can I code it so that it will pull the correct files (in sets of two, as I have a before and after for each), for the patient name, and the procedure?
UPDATE:
This code also returns nothing, and I don't know why? The $files array is returning as 0.
$procedures = get_post_custom_values('Procedure');
$procedure = preg_replace("/[s_]/", "-", strtolower($procedures[0]));
$patientName = preg_replace("/[s_]/", "-", strtolower($post->post_title));
$directory = get_template_directory_uri()."/img/before-after/".$patientName."/";
$files = glob($directory.'*', GLOB_BRACE);
foreach($files as $filename) {
Can someone please help?
You can use glob() function. You can pass first param as pattern and loop through the results of the function.
Example
foreach(glob('[a-zA-Z0-9]+-[a-zA-Z_0-9]+.jpg') as $filename)
{
echo $filename;
}
You can also use w
but it depends on your conditions.
上一篇: 将值添加到多维数组中
下一篇: Foreach循环取决于目录中的文件