0

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?

4
  • Check if Artist Title is same as previous, if not set counter to 1, if so increment counter. Commented Mar 13, 2019 at 15:32
  • that's helpful, thanks! Just stuck at one bit which is probably really obvious, I've got it working so the first multiples work great, but the next set of multiple values continue $i from the previous Commented Mar 13, 2019 at 15:45
  • Post that and we'll see. Commented Mar 13, 2019 at 15:46
  • I suggest you to create another json where gallery would be arrays Commented Mar 13, 2019 at 15:47

2 Answers 2

1

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:

  • You had a typo in your code. Article_Title should be Artist_Title.
  • You need to use json_decode($json); at the top of your code to convert JSON into an array of objects.
Sign up to request clarification or add additional context in comments.

3 Comments

That's perfect! My mistake with the typo, you wouldn't believe me if i said how many times I'd written artist..Thanks!
It happens! :) Feel free to accept it as an answer ;)
All done! Haha yeah I had $gallery = json_decode(file_get_contents(DIR . '/gallery.json')); Just hadn't posted it in my example ;)
0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.