emediastudios Posted October 13, 2007 Share Posted October 13, 2007 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 ""; } Quote Link to comment https://forums.phpfreaks.com/topic/73077-solved-shrink-my-code/ Share on other sites More sharing options...
Barand Posted October 13, 2007 Share Posted October 13, 2007 There's certainly a better way to format it Quote Link to comment https://forums.phpfreaks.com/topic/73077-solved-shrink-my-code/#findComment-368518 Share on other sites More sharing options...
kenrbnsn Posted October 13, 2007 Share Posted October 13, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/73077-solved-shrink-my-code/#findComment-368520 Share on other sites More sharing options...
emediastudios Posted October 13, 2007 Author Share Posted October 13, 2007 Used your code and it works, deletes the images ( photos ), but i get this error, Parse error: syntax error, unexpected ';' in C:\Program Files\Apache Group\Apache2\htdocs\gcproperty\admin\property_deleted.php on line 114 Quote Link to comment https://forums.phpfreaks.com/topic/73077-solved-shrink-my-code/#findComment-368525 Share on other sites More sharing options...
emediastudios Posted October 13, 2007 Author Share Posted October 13, 2007 The line of code is for($i=0;$i<count($_POST['photo'];$i++) { Quote Link to comment https://forums.phpfreaks.com/topic/73077-solved-shrink-my-code/#findComment-368528 Share on other sites More sharing options...
kenrbnsn Posted October 13, 2007 Share Posted October 13, 2007 Sorry, I forgot a closing parenthesis. <?php for($i=0;$i<count($_POST['photo']);$i++) { ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/73077-solved-shrink-my-code/#findComment-368529 Share on other sites More sharing options...
emediastudios Posted October 13, 2007 Author Share Posted October 13, 2007 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']; Quote Link to comment https://forums.phpfreaks.com/topic/73077-solved-shrink-my-code/#findComment-368530 Share on other sites More sharing options...
emediastudios Posted October 13, 2007 Author Share Posted October 13, 2007 Thanks for your help ken, i changed it but now this error Parse error: syntax error, unexpected T_STRING in C:\Program Files\Apache Group\Apache2\htdocs\gcproperty\admin\property_deleted.php on line 116 The line is this one unlink($myFile[$i]); Quote Link to comment https://forums.phpfreaks.com/topic/73077-solved-shrink-my-code/#findComment-368533 Share on other sites More sharing options...
kenrbnsn Posted October 13, 2007 Share Posted October 13, 2007 Use an array: <?php $myFile = array(); for ($i=1;$i<count($_POST['photo']);$i++) $myFile[$i] = '../images/' . $_POST['photo'][$i]; ?> This assumes you've made the input into an array also. Ken Quote Link to comment https://forums.phpfreaks.com/topic/73077-solved-shrink-my-code/#findComment-368534 Share on other sites More sharing options...
kenrbnsn Posted October 13, 2007 Share Posted October 13, 2007 Sometimes that error says that the error really is in the line before. Please post some of the lines prior to that one. Ken Quote Link to comment https://forums.phpfreaks.com/topic/73077-solved-shrink-my-code/#findComment-368536 Share on other sites More sharing options...
emediastudios Posted October 13, 2007 Author Share Posted October 13, 2007 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)); } Quote Link to comment https://forums.phpfreaks.com/topic/73077-solved-shrink-my-code/#findComment-368541 Share on other sites More sharing options...
kenrbnsn Posted October 13, 2007 Share Posted October 13, 2007 Missing end parenthesis on this line: <?php if (!empty($_POST['photo'][$i]) ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/73077-solved-shrink-my-code/#findComment-368542 Share on other sites More sharing options...
emediastudios Posted October 13, 2007 Author Share Posted October 13, 2007 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]); } Quote Link to comment https://forums.phpfreaks.com/topic/73077-solved-shrink-my-code/#findComment-368543 Share on other sites More sharing options...
emediastudios Posted October 13, 2007 Author Share Posted October 13, 2007 Thanks for your help, really appreciate it, it deletes the images still and im stoked bout that, just the error i need to fix, thanks again Quote Link to comment https://forums.phpfreaks.com/topic/73077-solved-shrink-my-code/#findComment-368544 Share on other sites More sharing options...
emediastudios Posted October 13, 2007 Author Share Posted October 13, 2007 I got it thanks champ, your the best Quote Link to comment https://forums.phpfreaks.com/topic/73077-solved-shrink-my-code/#findComment-368545 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.