Jump to content

[SOLVED] Shrink my code


emediastudios

Recommended Posts

Could there be a simple way, less code and do the same task?

 

This is the code i have

 if (!empty($_POST['photo']))
     {

unlink($myFile);
}
else
     {
       echo  "";
     }
       if (!empty($_POST['photo2']))
     {

unlink($myFile2);
}
else
     {
       echo  "";
     }
    if (!empty($_POST['photo3']))
     {

unlink($myFile3);
}
else
     {
       echo  "";
     }
    if (!empty($_POST['photo4']))
     {

unlink($myFile4);
}
else
     {
       echo  "";
     }
    if (!empty($_POST['photo5']))
     {

unlink($myFile5);
}
else
     {
       echo  "";
     }
    if (!empty($_POST['photo6']))
     {

unlink($myFile6);
}
else
     {
       echo  "";
     }
       if (!empty($_POST['photo7']))
     {

unlink($myFile7);
}
else
     {
       echo  "";
     }
    if (!empty($_POST['photo8']))
     {

unlink($myFile8);
}
else
     {
       echo  "";
     }
       if (!empty($_POST['photo9']))
     {

unlink($myFile9);
}
else
     {
       echo  "";
     }

Link to comment
Share on other sites

The easiest way to shrink your code would be to change your form to reference an array for the photos and  in your script to change "$myFiile" to an array, then you could do:

<?php
for($i=0;$i<count($_POST['photo'];$i++) {
     if (!empty($_POST['photo'][$i])
          unlink($myFile[$i]);
}
?>

 

BTW, you can shorten your code immediately by removing the

<?php
else
   {
      echo "";
    }
?>

in each "if" block. It is not needed.

 

Ken

Link to comment
Share on other sites

Can i simplify this code too.

$myFile = "../images/". $_POST['photo']; 
$myFile2 = "../images/". $_POST['photo2']; 
$myFile3 = "../images/". $_POST['photo3']; 
$myFile4 = "../images/". $_POST['photo4']; 
$myFile5 = "../images/". $_POST['photo5']; 
$myFile6 = "../images/". $_POST['photo6']; 
$myFile7 = "../images/". $_POST['photo7']; 
$myFile8 = "../images/". $_POST['photo8']; 
$myFile9 = "../images/". $_POST['photo9']; 

Link to comment
Share on other sites

 if (isset($_SERVER['QUERY_STRING'])) {
    $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
    $deleteGoTo .= $_SERVER['QUERY_STRING'];
    $myFile = "../images/". $_POST['photo']; 
$myFile2 = "../images/". $_POST['photo2']; 
$myFile3 = "../images/". $_POST['photo3']; 
$myFile4 = "../images/". $_POST['photo4']; 
$myFile5 = "../images/". $_POST['photo5']; 
$myFile6 = "../images/". $_POST['photo6']; 
$myFile7 = "../images/". $_POST['photo7']; 
$myFile8 = "../images/". $_POST['photo8']; 
$myFile9 = "../images/". $_POST['photo9']; 
}

     for($i=0;$i<count($_POST['photo']);$i++) {
     if (!empty($_POST['photo'][$i])
          unlink($myFile[$i]);
}
  
  header(sprintf("Location: %s", $deleteGoTo));
}

Link to comment
Share on other sites

Now this error

 

Parse error: syntax error, unexpected '[' in C:\Program Files\Apache Group\Apache2\htdocs\gcproperty\admin\property_deleted.php on line 115

 

  if (isset($_SERVER['QUERY_STRING'])) {
    $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
    $deleteGoTo .= $_SERVER['QUERY_STRING'];
    $myFile = "../images/". $_POST['photo']; 
$myFile2 = "../images/". $_POST['photo2']; 
$myFile3 = "../images/". $_POST['photo3']; 
$myFile4 = "../images/". $_POST['photo4']; 
$myFile5 = "../images/". $_POST['photo5']; 
$myFile6 = "../images/". $_POST['photo6']; 
$myFile7 = "../images/". $_POST['photo7']; 
$myFile8 = "../images/". $_POST['photo8']; 
$myFile9 = "../images/". $_POST['photo9']; 
}

     for($i=0;$i<count($_POST['photo']);$i++) {
     if (!empty($_POST['photo'])[$i])         /////////This is line 115////////////
          unlink($myFile[$i]);
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.