offset during foreach loop

Hi,

i have a script which i have attached below i tried commenting it as best as i could. i am experiencing some problems with it and any advice or help would be greatly appreciated.

firstly i would like to describe what i am trying to do as it may make everything a little easier

FIRST PAGE - NO PROBLEMS
1. in another script i have allowed users to upload images to a temporary directory. upon completing and satesifying some parameters i take the oringinal uploaded file append [tmp] and the user's username to the file and move it to a temporary folder ($temp_dir). once everything is completed on this script i send the user to another page where this script is implemented.

SECOND PAGE - PROBLEM - NAMED: CONFIRM_PHOTO.PHP
1.  three directories are set up
    a) a temp directory where all of the non -confirmed images are stored
    b) a recent images directory - standard image directory
    c) facourite image directory - for special images

2. the temporary folder is scanned for files that are accepted image types(png jpg etc).

 3. The start and end of the post form are created. these parts of the form are static and do not rely on the scanning of the directory

3. a foreach loop is created to create the middle of the form. to create the middle inputs a couple parameters must be met
      1. the file name must contain [tmp] and the user's username
if these parameters are met the forms middle is created

4. once all the images that have met the parameters have been display the form is ended by the end form created when we created the start of the form

5. the user has the ability to select/fill in a couple options for each image. the user can leave them empty. the options are
   1. would the user like to delete the form
   2.  image name
   3. image date
   4. image location
   5. favourite image (Y/N)
   6.  image description
   7. original name of file - non editable by user

6. once the user has decided what to do with each image the form is submitted. the array which is produced and reconfigured to allow the script to loop thought the results and perform work on each individual array as it pertains to each individual image.  ( this part i am fuzzy on as i got some help here about it).

7.  once the forms output has been reconfigured i loop though it and see if certain parameters are met.
  1. the first i test for is to see if the user has selected the delete option. if so i unlink the original image and move on.
  2. if the user has not select the delete option
        2a. i update the mysql with the new photos infor.  
        2b.i check to see if the user has select the favourites options which by default is set to no
                 i. if the has the favourites option i move it to the recent image directory and then copy it to the favourites directory
                 ii. if the user has not selected the favourites option i just move the image to the recent image directory.

note* -- the uploading of directories will change in the future and mysql will handle it i just have not got around to doing it yet
8. the last array in the form contained the form submit value so i skip it and when it is processed i redirect the user to a new page as the script has been completed.

9. at the end of the form i have included a function entitled reload it essentially catches any parameters i have not set up and displays an error.

THE ERROR
it seems if i select the delete option (which is done by creating a checkbox and then the user can select it or not. if not i have created a hidden field to declare the value of no - i probably do not need to do this) the foreach loop is shifted and the wrong actions occur.


FIRST PAGE SCRIPT - WORKS AS NEEDED
<?php
//GET PHOTO INFORMATION
$sqll="SELECT * FROM photo_options WHERE id='' LIMIT 1";
$query=mysqli_query($link, $sqll) or die(mysql_error($link));
$result=mysqli_fetch_assoc($query);

//SET UP VARAIBLES
$domain_name=$_SERVER['REAL_DOCUMENT_ROOT'];
$recent_gallery=$domain_name.$result['std_dir'];
$favs_gallery=$domain_name.$result['favs_dir'];
$temp_dir=$_SERVER['DOCUMENT_ROOT']."/temp/";
$accepted_formats =array("png","gif","jpg","jpeg");
$count=0;
$username=get_username($_SESSION['uid']);

//CHECK TO SEE IF TEMPORARY DIRECTORY EXISTS
if(is_dir($recent_gallery)!==TRUE){
	mkdir($recent_gallery,0755);
	
}elseif(is_dir($favs_gallery)!==TRUE){
	mkdir($favs_gallery,0755);

}
		
//CREATE A THUMBNAIL TO VIEW     
//SET THE VALUE OF THE THUMBS
$thumb_height=$result['tn_height']; //set the desired thumb height
$thumb_width=$result['tn_width']; //set the desired thumb width
	


	//TO CREATE THE FIRST UPLOAD FORM
	$picture_upload_form='<h6>Select a photo to upload</h6>';
	$picture_upload_form.='
			<form enctype="multipart/form-data" method="post" name="picture_upload_form" id="picture_upload_form" action="">
				<table width="95%" border="0">
				  <tr>
					<td width="10%">
						<img style="height:'.$thumb_height.'; width:'.$thumb_width.';" src="images/add_photo.jpg"/>
					</td>
					<td style="vertical-align:middle;">	
						Select photo to upload: <input type="file" id="photo[]" name="photo[]"  multiple="multiple"></br></br>
						<input type="submit" name="select_photo_upload" id="select_photo_upload" value="Upload">
						<input type="button" name="Cancel" value="Cancel" onClick="window.location=\'test.php\';" />
					</td>
				  </tr>
				</table>
			</form>
		';
		
		$progress_bar='		
			<div class="progress">
   				<div class="bar"></div >
    			<div class="percent">0%</div >
			</div>
			<div id="status"></div>
		';
	
	
if(empty($_POST['select_photo_upload'])){	
	echo $picture_upload_form;
	
		
}elseif(!empty($_POST['select_photo_upload'])){
	
	if(empty($_FILES['photo']['name'][0])){
		echo'<p style="color:red;">ERROR: No files selected!';
		echo $picture_upload_form;
		echo $progress_bar;
		
	}else{
		echo $progress_bar;
		//REARRAGE THE ARRAY FOR EASIER USE
		$new_array= rearrange_files($_FILES['photo']);
		
		//LOOP THROUGH THE SECOND ARRAY
		foreach($new_array as $photos){		
			//GET THE IMAGES FROM THE PHOTO ARRAY

				//TEST FILE TO SEE IF IT IS A PHOTO
				$temp = explode(".", strtolower($photos['name']));
				$extension = end($temp);

				//TEST FOR IMAGE TYPE
				if ((("image/gif" == $photos['type'])) ||
					(("image/jpeg" == $photos['type'])) || 
					(("image/jpg" == $photos['type'])) || 
					(("image/pjpeg" == $photos['type'])) || 
					(("image/x-png" == $photos['type'])) || 
					(("image/png" == $photos['type']))
					&& in_array($extension, $accepted_formats)) {
					
						{
							if(@getimagesize($photos['tmp_name']) == TRUE && $photos["error"] == 0) {
				
							//MOVE IMAGES TO TEMP FOLDER
							 	$count++;
								$move_temp = @move_uploaded_file($photos['tmp_name'], $temp_dir."[tmp".time()."]".$username.$count."-".strtolower($photos['name']));
								if(!$move_temp){
									
									$msg="ERROR: File did not upload!";
								}else{
									
									//REDIRECT TO CONFIRM PHOTO
									$redirect='
										<script type="text/javascript">
											<!--
											window.location = "confirm_photo.php"
											//-->
										</script>
									';
									
									echo $redirect;
								}
								
								//THE FILE FAILED TESTING
							}else{
								echo'<p style="color:red;">ERROR: File "'.$photos['name'].'" type is not supported!</p>';
								echo $picture_upload_form;
							}
						}
					
				}else{
					echo'<p style="color:red;">ERROR: File "'.$photos['name'].'" type is not supported!</p>';
					echo $picture_upload_form;
			}
		}
	}
}
?>

Open in new window


SECOND PAGE SCRIPT - ARRAY SHIFTED IS DELETE IS SELECTED
<?php
//SCAN THE DIRECTORY FOR PROPER FILES
$temp_dir=$_SERVER['DOCUMENT_ROOT']."/temp/";
$files= glob("{$temp_dir}*.{jpg,gif,png,jpeg}", GLOB_BRACE);

//SET UP SOME LOCATIONS FOR THE TEMPORARY FILES
$website="http://".$_SERVER['HTTP_HOST'];
$location=$website."/temp/";
$fav_dir=$_SERVER['DOCUMENT_ROOT']."/gallery/favourites/test/";
$recent_dir=$_SERVER['DOCUMENT_ROOT']."/gallery/recent_images/test/";

//SET UP SOME VARIABLES
$form_submit=empty($_POST['image_submit'])?"":mysqli_real_escape_string($link,($_POST['image_submit']));
$username=get_username($_SESSION['uid']); //GETS USERNAME FROM MYSQL 

//START THE FORM
$start_form = '
	<form enctype="multipart/form-data" method="post" name="picture_confirm_form" id="picture_confirm_form" action="">	

';

//END THE FORM
$end_form= '
	<div class="clear"></div>
	<div class="image_submit">
		<input type="submit" id="image_submit" name="image_submit" value="Image Submit"/><br />
		</form>
	</div>
';

//FORM HAS NOT BEEN SUBMITTED
if(empty($form_submit)){
	echo $start_form;

	//PERFORM WORK ON FILES
	foreach($files as $image){
		
		if(strpos($image,"tmp") && strpos($image,$username)){ //MAKES SURE THE IMAGE HAS THE USERNAME AND "TMP" INCLUDED IN IT WHICH I SET LAST PAGE
			//REMOVE THE TEMP DIRECTORY FROM NAME OF IMAGE
			$clean=str_replace($temp_dir,$location,$image);
			
			//SET UP VARIABLES
			$image_name=empty($_POST['image_name'])?ltrim(strstr($image,"-"),"-"):mysqli_real_escape_string($link,($_POST['image_name']));
			$image_org=empty($_POST['image_org'])?$image:($_POST['image_name']);
			$image_location=empty($_POST['image_location'])?"":mysqli_real_escape_string($link,($_POST['image_location']));
			$image_date=empty($_POST['image_date'])?"":mysqli_real_escape_string($link,($_POST['image_date']));
			$image_fav=empty($_POST['image_fav'])?"":mysqli_real_escape_string($link,($_POST['image_fav']));
			$image_desc=empty($_POST['image_desc'])?"":mysqli_real_escape_string($link,($_POST['image_desc']));

			//CREATE THE FORM INPUTS
			$input ='
				<div class="photo_container">
					<div>
						<input type="hidden" id="delete_photo[]" name="delete_photo[]" value="no" />
						<input type="checkbox" class="myinput large custom" title="delete image" id="delete_photo[]" name="delete_photo[]" value="delete" />
					</div>
					<div class="photo_box">
						<div class="photo">
							<img class="photo" src="'.$clean.'"/>
						</div>
					</div>
					<div class="info_container">
						<div class="info_input">
							Image name:<br />
							<input type="text" id="image_name[]" name="image_name[]" value="'.$image_name.'"/><br />
							<input type="hidden" id="image_name[]" name="image_org[]" value="'.$image_org.'"/><br />
							image date:<br />
							<input type="date" id="image_date[]" name"image_date[]" value="'.$image_date.'"/><br />
							Location photo was taken:<br />
							<input type="text" id=image_location[]" name="image_location[]" value="'.$image_location.'"/><br />
							Add to favoutites?
							<select name="image_fav[]" id="image_fav[]"><br>
								<option id="image_fav[]" name="image_fav[]">no</option>
								<option id="image_fav[]" name="image_fav[]">yes</option>
							</select>
							Image description:<br />
							<textarea  rows="5" cols="20 id=image_desc[]" name="image_desc[]">'.$image_desc.'</textarea>
						</div>
					</div>
				</div>	
			';
					
			//DISPLAY RESULTS
			echo $input;	
		}
	}
	//END FORM
	echo $end_form;
	
}elseif(!empty($form_submit) ){	
	//SET NEW VARIABLE FOR ALL POST VALUE UPON FORM SUBMIT
	$arr=$_POST;

	// SAVE A COPY OF THE INITIAL STATE
	$old = $arr;
	
	// REMOVE STRINGS
	foreach ($arr as $key => $sub)
	{
		if (is_string($sub)) unset($arr[$key]);
	}
	
	// MATRIX-INVERT ARRAYS
	$cnt = 0;
	while (TRUE)
	{
		foreach ($arr as $key => $sub)
		{
			$new[$cnt][$key] = $sub[$cnt];
			unset($arr[$key][$cnt]);
		}
		$cnt++;
		if (!isset($arr[$key][$cnt])) break;
	}
	
	// KEEP STRINGS, USE NEW ARRAYS
	$cnt = 0;
	foreach ($old as $key => $sub)
	{
		if (is_string($sub))
		{
			$out[][$key] = $sub;
		}
		elseif (!empty($new[$cnt]))
		{
			$out[] = $new[$cnt];
			$cnt++;
		}
	}
	
	//PROCESS THE NEW ARRAY
	foreach($out as $image){
	//SET UP SOME VARIABLES
		$image_delete=empty($image['delete_photo'])?"":$image['delete_photo'];
		$image_name=empty($image['image_name'])?"":$image['image_name'];
		$image_org=empty($image['image_org'])?"":$image['image_org'];
		$image_location=empty($image['image_location'])?"EARTH":$image['image_location'];
		$image_date=empty($image['image_date'])?date("Ymds",time()):$image['image_date'];
		$image_fav=empty($image['image_fav'])?"":$image['image_fav'];
		$image_desc=empty($image['image_desc'])?"":$image['image_desc'];
		$image_submit=empty($image['image_submit'])?"":$image['image_submit'];
		
		if(empty($image_submit)){ //THE LAST ARRAY IS IGNORED (LAST ARRAY IS THE SUBMIT BUTTON VALUE)
			if(strtolower($image_delete) !== "delete"){ //THE USER DOES NOT WANT TO DELETE THE PHOTO
				//UPDATE THE MYSQL
				if(!empty($image_name) || !isset($image_name)){ //THERE IS A NAME -- SHOULD ALWAYS BE TRUE
					$sql="INSERT INTO photos (name,bb_uploader, description, photo_date,photo_fav,photo_loc)";
					$sql.="VALUES ('".$image_name."','".$username."','".$image_desc."','".$image_date."',";
					$sql.="'".$image_fav."','".$image_location."')";
					$query=mysqli_query($link, $sql) or die(mysqli_error($link));
				
					//CHECK IT YOU MOVE IT TO FAVS
					if(strtolower($image_fav=="no") || empty($image_fav)){ // THE IMAGE HAS NOT BEEN SELECTED AS A FAVOURITE
							//MOVE FILES TO GALLERY FOLDER
							$rename=@rename($image_org, $recent_dir.$image_date.$image_name); //MOVE THE FILE TO THE RECENT IMAGES DIRECTORY DISPLAY ERROR IF NESSECARY
							if(!$rename){
								echo '<p style="color:#FF0000;">
										ERROR1: Could not move '.strtoupper($image_name).' file to gallery
									  </p>';	
							}else{
								echo '<p style="color:green;">SUCCESS: file '.strtoupper($image_name).' uploaded to recent images directory!</p>';
							}
				
					}elseif(strtolower($image_fav=="yes") || !empty($image_fav)){ //THE IMAGE HAS BEEN SELECTED AS A FAVOURITE
						//MOVE FILES TO GALLERY FOLDER
						$rename=@rename($image_org, $recent_dir.$image_date."-".$image_name);//MOVE IT TO THE RECENT IMAGES DIRECTORY- DISPLAY ERROR IF NESSECARY
						if(!$rename){
							echo '<p style="color:#FF0000;">ERROR2: Could not move '.strtoupper($image_name).' file to recent images directory</p>';	
						}else{
							echo '<p style="color:green;">SUCCESS: file '.strtoupper($image_name).' uploaded to recent images directory!</p>';
						}
						
						//COPY FILES TO FAVOURITE
						$copy=@copy($recent_dir.$image_date."-".$image_name, $fav_dir.$image_date."-".$image_name); //COPY FROM RECENT IMAGES TO FAVOURITES
						if(!$copy){
							echo '
								<p style="color:#FF0000;">
									ERROR3: Could not copy '.strtoupper($image_name).' file to favourite images directory
								</p>';	
						}else{
							echo '<p style="color:green;">SUCCESS: file '.strtoupper($image_name).' uploaded to favourite images directory!</p>';
						}
					}
				}
			}elseif(strtolower($image_delete) =="delete"){			
				//DELETE THE PHOTO
				$delete=@unlink($image_org);
				if(!$delete){ //DISPLAY ERROR IF SOMETHING GOES WRONG
					echo '<p style="color:#FF0000;">ERROR: Cannot delete '.strtoupper($image_name).'</p>';
					
				}else{
					echo '<p style="color:red;">'.strtoupper($image_name).' deleted!</p> ';
					
				}
				unset($image);
			}
		
		}elseif(!empty($image_submit)){ // THE ARRAY HAS BEEN COMPLETED REDIRECT BACK TO NEW PAGE
			//GO TO DIFFERENT PAGE
			$redirect='
				<script type="text/javascript">
					<!--
					setTimeout("window.location=\'media.php\'",5000)
					//-->
				</script>
			';
			
			//echo $redirect;
			
		}else{ //IF ANYTHING ELSE OCCURED THAT HAS NOT BEEN EXPLAINED ABOVE
			$e_msg = 'ERROR: System malfunction!';
			reload(3000,"fail",$e_msg); //DISPLAYS ERROR AND REFRESHES PAGE
		}
	}
}
?>

Open in new window



im not sure why the delete option shifts the array but i would appreciate some help

thanks a TON!!
LVL 6
J NUnicorn wranglerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GaryCommented:
TMI
What do you mean shifted - the index is being advanced too much?
Exactly where is it getting shifted?
0
J NUnicorn wranglerAuthor Commented:
Sorry i tried to include as much as i could to make it as easy and possible it seems like is getting shifted on extra

for example if i select delete on img 1 then img2 is being deleted
0
J NUnicorn wranglerAuthor Commented:
if i do not select the delete option then the processing works correctly
0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

GaryCommented:
Can you do a var_dump of $out before you start the loop and paste the result here.
0
J NUnicorn wranglerAuthor Commented:
Hi,

this is a var_dump on two images and the delete option has been unchecked

array(3) {
	 [0]=> array(6) { 
     	["delete_photo"]=> string(2) "no" 
        ["image_name"]=> string(12) "dsc_0002.jpg" 
        ["image_org"]=> string(85) "/var/chroot/home/content/21/10892721/html/temp/[tmp1378763857]jaymenagy1-dsc_0002.jpg" 
        ["image_location"]=> string(0) "" 
        ["image_fav"]=> string(2) "no" 
        ["image_desc"]=> string(0) ""
        } 
        
	[1]=> array(6) { 
    	["delete_photo"]=> string(2) "no" 
        ["image_name"]=> string(12) "dsc_0011.jpg" 
        ["image_org"]=> string(85) "/var/chroot/home/content/21/10892721/html/temp/[tmp1378763857]jaymenagy2-dsc_0011.jpg" 
        ["image_location"]=> string(0) "" 
        ["image_fav"]=> string(2) "no" 
        ["image_desc"]=> string(0) "" 
        } 
        
	[2]=> array(1) { 
    	["image_submit"]=> string(12) "Image Submit" 
        } 
	}

Open in new window

0
J NUnicorn wranglerAuthor Commented:
this is a var_dump with the second image having the delete option enabled
 array(3) { 
 	[0]=> array(6) { 
    	["delete_photo"]=> string(2) "no" 
        ["image_name"]=> string(12) "dsc_0010.jpg" 
        ["image_org"]=> string(85) "/var/chroot/home/content/21/10892721/html/temp/[tmp1378764004]jaymenagy1-dsc_0010.jpg" 
        ["image_location"]=> string(0) "" 
        ["image_fav"]=> string(2) "no" 
        ["image_desc"]=> string(0) "" 
        } 
        
	[1]=> array(6) { 
    	["delete_photo"]=> string(6) "delete" 
        ["image_name"]=> string(12) "dsc_0015.jpg" 
        ["image_org"]=> string(85) "/var/chroot/home/content/21/10892721/html/temp/[tmp1378764004]jaymenagy2-dsc_0015.jpg" 
        ["image_location"]=> string(0) "" 
        ["image_fav"]=> string(2) "no" 
        ["image_desc"]=> string(0) "" 
        } 
        
	[2]=> array(1) { 
    	["image_submit"]=> string(12) "Image Submit" 
        } 
	}
    

Open in new window

0
GaryCommented:
So running the code which image is getting deleted if not [tmp1378764004]jaymenagy2-dsc_0015.jpg
0
GaryCommented:
Why are you removing a row from the array with unset($image); This will result in the index being wrong.
0
J NUnicorn wranglerAuthor Commented:
I wanted to see if it would yield a different result and compensate for the offset. it did not and i forgot to remove it
0
J NUnicorn wranglerAuthor Commented:
Correct the image that is getting deleted is  [tmp1378764004]jaymenagy2-dsc_0015.jpg

essentially when the user uploads their image they have their own name for the file. in this case it is dsc_0015.jpg but i add [tmp1378764004]jaymenagy2 so i can identify it and run some queries so it fits into the framework i like
0
GaryCommented:
Now I'm confused.
If dsc_0015.jpg is selected for deletion and it is getting deleted where is it going wrong?
0
J NUnicorn wranglerAuthor Commented:
Oh sorry!

What i meant is that you are correct that i want to delete the image dsc_0015.jpg but this does not occur.


on the above example the delete value is on the second image so i would want it to delete that one however if i run the script it would delete the next one so delete on array [0] would delete [1] or if i had a third image delete on array[1] would delete array [2]
0
GaryCommented:
:o)
So the array is correct prior to the $out loop

And if you echo $image_org before
$delete=@unlink($image_org);

Is the correct image shown or not?
If not can you do an echo of the index number.
I cannot see anything advancing the index.
0
J NUnicorn wranglerAuthor Commented:
with the example above and the delete option on the second image i get the following
 array(3) { 
 	[0]=> array(6) { 
    	["delete_photo"]=> string(2) "no" 
        ["image_name"]=> string(12) "dsc_0010.jpg" 
        ["image_org"]=> string(85) "/var/chroot/home/content/21/10892721/html/temp/[tmp1378767953]jaymenagy1-dsc_0010.jpg" 
        ["image_location"]=> string(0) "" 
        ["image_fav"]=> string(2) "no" 
        ["image_desc"]=> string(0) "" } 
        
    [1]=> array(6) { 
   		["delete_photo"]=> string(2) "no" 
    	["image_name"]=> string(12) "dsc_0015.jpg" 
    	["image_org"]=> string(85) "/var/chroot/home/content/21/10892721/html/temp/[tmp1378767953]jaymenagy2-dsc_0015.jpg" 
    	["image_location"]=> string(0) "" 
    	["image_fav"]=> string(2) "no" 
    	["image_desc"]=> string(0) "" } [
    
    2]=> array(1) { ["image_submit"]=> string(12) "Image Submit" } 
    
    }

Open in new window

0
J NUnicorn wranglerAuthor Commented:
is it something to do with this

<input type="hidden" id="delete_photo[]" name="delete_photo[]" value="no" />
						<input type="checkbox" class="myinput large custom" title="delete image" id="delete_photo[]" name="delete_photo[]" value="delete" />

Open in new window

0
GaryCommented:
What do you get with my comment above
0
J NUnicorn wranglerAuthor Commented:
if i include an echo i do not get anything

i included it here
//DELETE THE PHOTO
				echo $image_org;
				$delete=@unlink($image_org);
				if(!$delete){ //DISPLAY ERROR IF SOMETHING GOES WRONG
					echo '<p style="color:#FF0000;">ERROR4: Cannot delete '.strtoupper($image_name).'</p>';
					
				}else{
					echo '<p style="color:red;">'.strtoupper($image_name).' deleted!</p> ';
					
				}
			}else{
				echo '<p style="color:#FF0000;">ERROR5: System Malfunction!</p>';
			}

Open in new window

0
GaryCommented:
Maybe it would be easier to just give your checkbox a value equal to the filename (not delete) and then just loop the array on post and delete any images in it.
0
J NUnicorn wranglerAuthor Commented:
is that not what i am doing except instead of "delete" i would just put image name
0
GaryCommented:
No you're relying on a separate array of the image names and using that for the index.
Putting the image name in the delete checkbox will forgo any index problems as you can just cycle the array in one go without needing to get the index.
0
J NUnicorn wranglerAuthor Commented:
Hi,

i think the problem is this

<input type="hidden" id="delete_photo[]" name="delete_photo[]" value="no" />

however if i delete it i get a weird offset when i rearrange my data at line 108

$new[$cnt][$key] = $sub[$cnt];

how would i compensate for removing the default value?
0
J NUnicorn wranglerAuthor Commented:
So if i remove the hidden input and var_dump the $out variable on the same images i get the following
Array
(
    [0] => Array
        (
            [image_name] => dsc_0010.jpg
            [image_org] => /var/chroot/home/content/21/10892721/html/temp/[tmp1378774801]jaymenagy1-dsc_0010.jpg
            [image_location] => 
            [image_fav] => no
            [image_desc] => 
            [delete_photo] => delete
        )

    [1] => Array
        (
            [image_submit] => Image Submit
        )

)
/var/chroot/home/content/21/10892721/html/temp/[tmp1378774801]jaymenagy1-dsc_0010.jpg

Open in new window


there are still two images
0
J NUnicorn wranglerAuthor Commented:
i think this is due to the fact that if the checkbox is not selected the array is not set and then the offset to rerrange the $_post variables is thrown out of wack. however i did not create this part of the script i got help and i do not fully understand it.
0
GaryCommented:
doh I'm an idiot.
You are right, if the checkbox isn't selected no value is sent so the first checked checkbox will always be first in the array.
So do as I said above and pass the filename in the value field, so you can just loop it. It will only contain the files that need to be deleted
0
J NUnicorn wranglerAuthor Commented:
Hi,

i dont understand how this would be any different then setting the value to delete is it not just another value?
0
GaryCommented:
By setting the value to the image name then on the posted to page you can just do

foreach($_POST['delete_photo'] as $image){
	$delete=@unlink($image);
	if(!$delete){ //DISPLAY ERROR IF SOMETHING GOES WRONG
		echo '<p style="color:#FF0000;">ERROR: Cannot delete '.strtoupper($image).'</p>';			
	}else{
		echo '<p style="color:red;">'.strtoupper($image).' deleted!</p> ';
					
	}
}

Open in new window


as a separate function and remove the delete part from the rest of your code.
The checkbox will not be posted if it is not checked.
0
J NUnicorn wranglerAuthor Commented:
Hi,

would it only be set only if the checkbox is selected and then when i rearrange the post array it does not work
0
J NUnicorn wranglerAuthor Commented:
i figured out the offset

when i use
<input type="hidden" id="delete_photo[]" name="delete_photo[]" value="no" />
to have a default value for the checkbox and i when i select it i create a duplicate entry.  there is an entry from the hidden field and there is an entry from the user input. however i need the default value so i can rearrange the array properly. without the default amount i get an uneven array and the offset is improper.

i guess the question becomes how can i maintain the checkbox and maintain the array values so it does not have an improper offset
0
J NUnicorn wranglerAuthor Commented:
if i select only one of the images to delete this is the array i get which creates an offset again and i get an error when i attempt to rearrange the post data
Array
(
    [delete_photo] => Array
        (
            [0] => delete
        )

    [image_name] => Array
        (
            [0] => analysis.jpg
            [1] => balance.jpg
            [2] => beach ball.jpg
        )

    [image_org] => Array
        (
            [0] => /var/chroot/home/content/21/10892721/html/temp/[tmp1378788867]jaymenagy1-analysis.jpg
            [1] => /var/chroot/home/content/21/10892721/html/temp/[tmp1378788867]jaymenagy2-balance.jpg
            [2] => /var/chroot/home/content/21/10892721/html/temp/[tmp1378788867]jaymenagy3-beach ball.jpg
        )

    [image_location] => Array
        (
            [0] => 
            [1] => 
            [2] => 
        )

    [image_fav] => Array
        (
            [0] => no
            [1] => no
            [2] => no
        )

    [image_desc] => Array
        (
            [0] => 
            [1] => 
            [2] => 
        )

    [image_submit] => Image Submit
)


Notice:  Undefined offset: 1 in /home/content/21/10892721/html/cms/system/includes/confirm_photo_config.php on line 111



Notice:  Undefined offset: 2 in /home/content/21/10892721/html/cms/system/includes/confirm_photo_config.php on line 111

Open in new window

0
GaryCommented:
You cannot pass a checkbox unless it is checked. If you have 10 checkboxes and only 2 are checked then only the values of those 2 checkboxes will POSTed. So the delete checkboxes will never be in the same order as other input elements on the page.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.