...

View Full Version : is there an easier way to do this?



docock
06-21-2010, 01:57 PM
'I want to check if certain images are stored in three different directories, if they are I want to unlink them. And add some stuff to variable $commando.'

Like this:


if (isset($_POST['Pic1'])) {
if (file_exists("../".$Countryrow["Imagelocation"]."/thumbs/".$Countryrow['Pic1']."t.jpg")) {unlink ("../".$Countryrow["Imagelocation"]."/thumbs/".$Countryrow['Pic1']."t.jpg");}
if (file_exists("../".$Countryrow["Imagelocation"]."/thumbseffects/".$Countryrow['Pic1']."t.jpg")) {unlink ("../".$Countryrow["Imagelocation"]."/thumbseffects/".$Countryrow['Pic1']."t.jpg");}
if (file_exists("../".$Countryrow["Imagelocation"]."/thumbsoriginal/".$Countryrow['Pic1']."t.jpg")) {unlink ("../".$Countryrow["Imagelocation"]."/original/".$Countryrow['Pic1']."t.jpg");}
$commando=$commando. "Pic1='',";
}

if (isset($_POST['Pic2'])) {
if (file_exists("../".$Countryrow["Imagelocation"]."/thumbs/".$Countryrow['Pic2']."t.jpg")) {unlink ("../".$Countryrow["Imagelocation"]."/thumbs/".$Countryrow['Pic2']."t.jpg");}
if (file_exists("../".$Countryrow["Imagelocation"]."/thumbseffects/".$Countryrow['Pic2']."t.jpg")) {unlink ("../".$Countryrow["Imagelocation"]."/thumbseffects/".$Countryrow['Pic2']."t.jpg");}
if (file_exists("../".$Countryrow["Imagelocation"]."/thumbsoriginal/".$Countryrow['Pic2']."t.jpg")) {unlink ("../".$Countryrow["Imagelocation"]."/original/".$Countryrow['Pic2']."t.jpg");}
$commando=$commando. "Pic2='',";
}

if (isset($_POST['Pic3'])) {
if (file_exists("../".$Countryrow["Imagelocation"]."/thumbs/".$Countryrow['Pic3']."t.jpg")) {unlink ("../".$Countryrow["Imagelocation"]."/thumbs/".$Countryrow['Pic3']."t.jpg");}
if (file_exists("../".$Countryrow["Imagelocation"]."/thumbseffects/".$Countryrow['Pic3']."t.jpg")) {unlink ("../".$Countryrow["Imagelocation"]."/thumbseffects/".$Countryrow['Pic3']."t.jpg");}
if (file_exists("../".$Countryrow["Imagelocation"]."/thumbsoriginal/".$Countryrow['Pic3']."t.jpg")) {unlink ("../".$Countryrow["Imagelocation"]."/original/".$Countryrow['Pic3']."t.jpg");}
$commando=$commando. "Pic3='',";

// repeat this for all the other pictures


Now I need to do this for about 30 pictures. so that's a lot of coding. Is there an easier way to do this? for example with the foreach function?

jfreak53
06-21-2010, 02:25 PM
Yes, your on the right track just continue:



$i=0;
foreach ($_POST as $pic) {
$i++;
stuff
}

And keep a numbering sequence to keep track with the i variable.

PHP.net is your friend! :)
http://gt.php.net/manual/en/control-structures.foreach.php



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum