Reputation: 23
I've exported some entries from a database which has given me the following JSON:
[
{
"Artist_Title": "17 Hippies",
"Gallery_Image_Filename": "17Hippies-2011-web.jpg"
},
{
"Artist_Title": "17 Hippies",
"Gallery_Image_Filename": "christo.jpg"
},
{
"Artist_Title": "17 Hippies",
"Gallery_Image_Filename": "kiki1.jpg"
},
{
"Artist_Title": "17 Hippies",
"Gallery_Image_Filename": "17-hippies.jpg"
},
{
"Artist_Title": "17 Hippies",
"Gallery_Image_Filename": "Photo1.jpg"
},
{
"Artist_Title": "Ann Savoy Trio",
"Gallery_Image_Filename": "Savoy-Trio-B&W-resize342x.jpg"
},
{
"Artist_Title": "Baghdaddies",
"Gallery_Image_Filename": "Baghdaddies-(Small).JPG"
},
{
"Artist_Title": "Baghdaddies",
"Gallery_Image_Filename": "Baghdaddies2.jpg"
}
]
Which has gallery images associated against a post. I've exported the Post Title (Artist_Title) and the Image file name (Gallery_Image_Filename).
One post can have multiple gallery images, the end goal here is to do an foreach and var dump a list like for example
wget www.[domain].com/images/17Hippies-2011-web.jpg -O 17-hippies-gallery-1.jpg
wget www.[domain].com/images/christo.jpg -O 17-hippies-gallery-2.jpg
wget www.[domain].com/images/kiki1.jpg -O 17-hippies-gallery-3.jpg
wget www.[domain].com/images/17-hippies.jpg -O 17-hippies-gallery-4.jpg
wget www.[domain].com/images/Photo1.jpg -O 17-hippies-gallery-5.jpg
wget www.[domain].com/images/Savoy-Trio-B&W-resize342x.jpg -O ann-savoy-trio-1.jpg
wget www.[domain].com/images/Baghdaddies-(Small).JPG -O baghdaddies-gallery-1.jpg
wget www.[domain].com/images/Baghdaddies2.jpg -O 17-baghdaddies-gallery-2.jpg
So essentially I am creating a wget list of image urls along with a new file name for them. I'm taking the post title, sanitising it, appending -gallery-[number], where the number needs to match the amount of Artist_Title's it has found.
I have this foreach loop, I just need to amend it to add the -[number] based on amount of posts now
foreach($gallery as $image) {
echo 'wget https://www.accessallareas.info/images/' . $image->Gallery_Image_Filename . ' -O ' . sanitize_title($image->Article_Title) . '-gallery.jpg';
echo '<br>';
}
Is this achievable?
Upvotes: 0
Views: 340
Reputation: 2968
You can save a counter for each post and check that in your loop.
Try this:
$pCounter = 0; // for Post counter
$artistTitle = '';
foreach($gallery as $image) {
if ($image->Artist_Title !== $artistTitle) {
$pCounter = 0;
}
$counter++;
echo 'wget https://www.accessallareas.info/images/' . $image->Gallery_Image_Filename . ' -O ' . $image->Artist_Title . '-gallery-'. $pCounter . '.jpg';
echo '<br>';
$artistTitle = $image->Artist_Title;
}
Note:
Article_Title
should be Artist_Title
.json_decode($json);
at the top of your code to convert JSON into an array of objects. Upvotes: 1
Reputation: 401
Everything is achievable.
$counter = 0;
$article_title = '';
foreach($gallery as $image) {
if ($image->Article_Title !== $article_title) {
$counter = 0;
}
$counter++;
echo 'wget https://www.accessallareas.info/images/' . $image->Gallery_Image_Filename . ' -O ' . sanitize_title($image->Article_Title) . '-gallery-'. $counter .'.jpg';
echo '<br>';
$article_title = $image->Article_Title;
}
Upvotes: 0