hi guys...i have these lines in my delete checkbox script. i managed to delete the data thats been check with the checkbox, but im having problem trying to delete the images that comes with it. with the existing script below, i managed only to delete the first item in the array... is there anyway I could loop the unlink to delete not only the first item in the array but the whole results of the array for the image to delete?

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "formSubmit")) {

	$frm_chk_delete = $_REQUEST['checkbox'];
	$list_of_ids = implode(",", $frm_chk_delete);

	mysql_select_db($database_connAvtar, $connAvtar);
	$query_rsImages = sprintf("SELECT thumbF, image1F, image2F, image3F, image4F FROM property WHERE id IN ($list_of_ids)");
	$rsImages = mysql_query($query_rsImages, $connAvtar) or die(mysql_error());
	$row_rsImages = mysql_fetch_assoc($rsImages);
	$totalRows_rsImages = mysql_num_rows($rsImages);

	$del[0] = $row_rsImages['thumbF'];
	$del[1] = $row_rsImages['image1F'];
	$del[2] = $row_rsImages['image2F'];
	$del[3] = $row_rsImages['image3F'];
	$del[4] = $row_rsImages['image4F'];

	for($h=0;$h<5;$h++) {		
		if($del[$h]) {
			unlink('../content/properties/' . $del[$h]);		
		}
	}

	$result = mysql_query("DELETE FROM property WHERE id IN ($list_of_ids)");

if($result){
	echo "<meta http-equiv=\"refresh\" content=\"0;URL=properties.php\">";
}
}

hope somebody could assist me with it... thank you in advance!

    replace:

        $del[0] = $row_rsImages['thumbF'];
        $del[1] = $row_rsImages['image1F'];
        $del[2] = $row_rsImages['image2F'];
        $del[3] = $row_rsImages['image3F'];
        $del[4] = $row_rsImages['image4F'];
    
        for($h=0;$h<5;$h++) {        
            if($del[$h]) {
                unlink('../content/properties/' . $del[$h]);        
            }
        }

    with

    foreach($row_rsImages as $del){
    unlink('../content/properties/'.$del);

    }

    I would put in the full file path just to be sure

      dagon wrote:

      replace:

          $del[0] = $row_rsImages['thumbF'];
          $del[1] = $row_rsImages['image1F'];
          $del[2] = $row_rsImages['image2F'];
          $del[3] = $row_rsImages['image3F'];
          $del[4] = $row_rsImages['image4F'];
      
          for($h=0;$h<5;$h++) {        
              if($del[$h]) {
                  unlink('../content/properties/' . $del[$h]);        
              }
          }

      with

      foreach($row_rsImages as $del){
      unlink('../content/properties/'.$del);

      }

      I would put in the full file path just to be sure

      hi dagon...the current changes that you suggest still only deletes the first in the array $frm_chk_delete thats why I have this line :

      $list_of_ids = implode(",", $frm_chk_delete);

      to make a delete :

      $result = mysql_query("DELETE FROM property WHERE id IN ($list_of_ids)");

      but the problem is that only the first in $frm_chk_delete image could be deleted.

      for example, what if I checked 3 checkboxes that has a value of 1,3 and 4. the data that has an ID 1, 3 and 4 were deleted in the database, but only the unlink of images of 1 is deleted. what I want is that after retrieving the images of 1, 3 and 4, it unlink it images, then delete it from the database.

        oh i see why, you need a while look for returning the data from the db currently your only grabbing the first one.

          dagon wrote:

          oh i see why, you need a while look for returning the data from the db currently your only grabbing the first one.

          hi dagon...care to show how will I do that? would it be ok if you could add it to my existing script to how i should work that out?

          thank you very much by the way!

            if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "formSubmit")) {
            
                $frm_chk_delete = $_REQUEST['checkbox'];
                $list_of_ids = implode(",", $frm_chk_delete);
            
                mysql_select_db($database_connAvtar, $connAvtar);
                $query_rsImages = sprintf("SELECT thumbF, image1F, image2F, image3F, image4F FROM property WHERE id IN ($list_of_ids)");
                $rsImages = mysql_query($query_rsImages, $connAvtar) or die(mysql_error());
                $row_rsImages = mysql_fetch_assoc($rsImages);
            
            
                $totalRows_rsImages = mysql_num_rows($rsImages);
            
            	  while ($row_rsImages = mysql_fetch_assoc($rsImages)){
            
            
               foreach($row_rsImages as $del){
            unlink('../content/properties/'.$del);
            }
            }
            
                }
            
                $result = mysql_query("DELETE FROM property WHERE id IN ($list_of_ids)");
            
            if($result){
                echo "<meta http-equiv=\"refresh\" content=\"0;URL=properties.php\">";
            }
            } 
            

            should work bar any typos

              dagon wrote:
              if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "formSubmit")) {
              
                  $frm_chk_delete = $_REQUEST['checkbox'];
                  $list_of_ids = implode(",", $frm_chk_delete);
              
                  mysql_select_db($database_connAvtar, $connAvtar);
                  $query_rsImages = sprintf("SELECT thumbF, image1F, image2F, image3F, image4F FROM property WHERE id IN ($list_of_ids)");
                  $rsImages = mysql_query($query_rsImages, $connAvtar) or die(mysql_error());
                  $row_rsImages = mysql_fetch_assoc($rsImages);
              
              
                  $totalRows_rsImages = mysql_num_rows($rsImages);
              
              	  while ($row_rsImages = mysql_fetch_assoc($rsImages)){
              
              
                 foreach($row_rsImages as $del){
              unlink('../content/properties/'.$del);
              }
              }
              
                  }
              
                  $result = mysql_query("DELETE FROM property WHERE id IN ($list_of_ids)");
              
              if($result){
                  echo "<meta http-equiv=\"refresh\" content=\"0;URL=properties.php\">";
              }
              } 
              

              should work bar any typos

              hi dagon....i added these lines

              while ($row_rsImages = mysql_fetch_assoc($rsImages)){
              
              
                 foreach($row_rsImages as $del){
              unlink('../content/properties/'.$del);
              }
              }
              
              

              right after $totalRows_rsImages = mysql_num_rows($rsImages); but still didnt work. still only the first in the array of its images was being unlink.

                echo everything make sure the right values are coming from the while loop and going in to the foreach

                  dagon wrote:

                  echo everything make sure the right values are coming from the while loop and going in to the foreach

                  hi dagon...i did these :

                  		while ($row_rsImages = mysql_fetch_assoc($rsImages)){ 
                  
                          foreach($row_rsImages as $del) { 
                  			echo $del;
                  			unlink('../content/properties/'.$del); 
                  		} 
                  	}
                  
                  

                  the echo only shows the first in the array thats why it only deletes the images of the first.

                    dagon wrote:

                    what about the while?

                    hi dagon...what do you mean what about the while?...sorry for being so innocent on this one. would it be ok if you could complete / fix the script to why its only deleting the first images of the array?

                    thanks again!

                      badzv wrote:

                      hi dagon...what do you mean what about the while?

                      print_r($row_rsImages); in the while loop

                        dagon wrote:

                        print_r($row_rsImages); in the while loop

                        hi dagon...this is my code now :

                        mysql_select_db($database_connAvtar, $connAvtar);
                        		$query_rsImages = sprintf("SELECT thumbF, image1F, image2F, image3F, image4F FROM property WHERE id IN ($list_of_ids)");
                        		$rsImages = mysql_query($query_rsImages, $connAvtar) or die(mysql_error());
                        		$row_rsImages = mysql_fetch_assoc($rsImages);
                        		$totalRows_rsImages = mysql_num_rows($rsImages);
                        
                        while ($row_rsImages = mysql_fetch_assoc($rsImages)){ 
                                       print_r($row_rsImages); 
                                    foreach($row_rsImages as $del) { 				
                        				unlink('../content/properties/'.$del); 
                        			} 
                        		}
                        
                        	$result = mysql_query("DELETE FROM property WHERE id IN ($list_of_ids)");
                        

                        only the last entry of the array is deleted...so for example i checked two checkboxes having a value of 3 and 8, only the images of 8 are being deleted. and i noticed also that when i checked only one checkbox, the result of print_r($row_rsImages) wont show up unlike checking 2 checkboxes wherein the results would appear. i assumed that when i only checked one checkbox, the while condition wont make it to true. but i maybe wrong.

                        hope to hear anything from you again! thanks for answering me and guiding me with my php problem. really appreciate it.

                          Can you show us the output of the print_r() in the while loop? Also, is display_errors set to On and error_reporting to E_ALL ?

                            bradgrafelman wrote:

                            Can you show us the output of the print_r() in the while loop? Also, is display_errors set to On and error_reporting to E_ALL ?

                            here is the result :

                            Array ( [thumbF] => thumb.jpg [image1F] => left.jpg [image2F] => image1.jpg [image3F] => image2.jpg [image4F] => image3.jpg )

                            it suppose to show 2 arrays since i checked 2 checkboxes. for example, i check two checkboxes having a value of 2 and 5, only the 5 of its images are being read. it should read also 2 at the same time. so im expecting that there should be 2 arrays should be read.

                              for the last time....anyone...please???

                                Write a Reply...