opalepatrick Posted April 27, 2014 Share Posted April 27, 2014 I accidentally batch uploaded too many images. So now I have 3 or 4 identical images per product in this case. I wanted to delete all but the first. I have tried umpteen options using the API but am a bit stumped. Could someone put me on the right track? Thanks Link to comment Share on other sites More sharing options...
adrian Posted April 27, 2014 Share Posted April 27, 2014 Try this: $p=$pages->get("/mypage/"); $p->of(false); $i=0; foreach($p->images as $image){ if($i!=0) $p->images->delete($image); $i++; } $p->save("images"); 1 Link to comment Share on other sites More sharing options...
opalepatrick Posted April 28, 2014 Author Share Posted April 28, 2014 Thanks for the pointer adrian. I took so long because I tried to solve the issues I came across. Unsuccessfully! The adapted code I used: $p = $pages->get("parent=/products/"); $p->of(false); $i=0; foreach($p->prod_image as $image){ //if($i!=0) $p->images->delete($image); if($i!=0){ echo $image . " selected to delete<br />"; } $i++; } It took me a while to realise that it was only getting the first product where the parent was products. So $p was a one page array? There should be at least 300+ items in the array. Am I getting confused. Anyway, going around in circles and in danger of disappearing up.... Not a pretty idea. Any help once again? Link to comment Share on other sites More sharing options...
adrian Posted April 28, 2014 Share Posted April 28, 2014 In a bit of a rush, but ->get will only ever return one page. You need ->find if you want to get all pages with parent=/products/ So maybe: $all_pages = $pages->find("parent=/products/"); foreach($all_pages as $p){ $p->of(false); $i=0; foreach($p->prod_image as $image){ //if($i!=0) $p->images->delete($image); if($i!=0){ echo $image . " selected to delete<br />"; } $i++; } } Note - really rushed and untested, but hopefully you get the idea. Link to comment Share on other sites More sharing options...
opalepatrick Posted April 28, 2014 Author Share Posted April 28, 2014 Thanks for the feedback. It worked as above and I printed out precisely the correct files. However, when I then tried the delete line. It appeared to do something (no errors) but didnt actually delete anything. I then tried remove and the same thing. I have seen other threads that talks about using remove, but as I say nothing disappeared. Latest code: $all_pages = $pages->find("parent=/products/"); foreach($all_pages as $p){ $p->of(false); $i=0; foreach($p->prod_image as $image){ if($i!=0) $p->prod_image->remove($image); // if($i!=0){ // echo $image . " selected to delete<br />"; // } $i++; } } $p->save("prod_image"); I am still looking, but a bit baffled. Link to comment Share on other sites More sharing options...
Soma Posted April 28, 2014 Share Posted April 28, 2014 Save is outside foreach? This on saves the last page. Link to comment Share on other sites More sharing options...
opalepatrick Posted April 28, 2014 Author Share Posted April 28, 2014 Ow! That is a bit embarrassing. How many times did I stare at that and not see it? Thanks a bunch Soma & Adrian. Much appreciated. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now