thara Posted May 22, 2018 Share Posted May 22, 2018 This is my array. It has stored images data. [other-image] => Array ( [img] => Array ( [0] => 1526973657.jpg [1] => 1526973661.jpg [2] => 1526973665.jpg ) [path] => Array ( [0] => ../post-upload/1/ [1] => ../post-upload/1/ [2] => ../post-upload/1/ ) [type] => Array ( [0] => 1 [1] => 1 [2] => 1 ) [thumb] => Array ( [0] => thumb_1526973661.jpg [1] => thumb_1526973665.jpg [2] => thumb_1526973668.jpg ) ) Now I want to delete an image and it's all related data from sub arrays. (path, type, thumb data) This is how I tried it in php: $delkey = $_POST['key']; if(in_array($delkey, $_SESSION['other-image']['img'])){ $imgkey = array_search($delkey, $_SESSION['other-image']['img']); if($imgkey) unset($_SESSION['other-image']['img'][$imgkey]); //file path $fp = UPLOAD_DIR.$nextId."/".$delkey; unlink($fp); } As I did, its only deleting element form 'img' array, but I want to delete related data from other arrays too. Can anybody tell me how to do this? Thank you. Link to comment Share on other sites More sharing options...
requinix Posted May 22, 2018 Share Posted May 22, 2018 Can anybody tell me how to do this?By writing code to do it? The code already removes it from the img array. Make it do the same thing to the other arrays. Link to comment Share on other sites More sharing options...
Barand Posted May 22, 2018 Share Posted May 22, 2018 Is this what you mean $delkey = $_POST['key']; if(in_array($delkey, $_SESSION['other-image']['img'])){ $imgkey = array_search($delkey, $_SESSION['other-image']['img']); if($imgkey) { foreach (['img', 'path', 'type', 'thumb'] as $sub) { unset($_SESSION['other-image'][$sub][$imgkey]); } } //file path $fp = UPLOAD_DIR.$nextId."/".$delkey; unlink($fp); } Link to comment Share on other sites More sharing options...
thara Posted May 22, 2018 Author Share Posted May 22, 2018 Is this what you mean $delkey = $_POST['key']; if(in_array($delkey, $_SESSION['other-image']['img'])){ $imgkey = array_search($delkey, $_SESSION['other-image']['img']); if($imgkey) { foreach (['img', 'path', 'type', 'thumb'] as $sub) { unset($_SESSION['other-image'][$sub][$imgkey]); } } //file path $fp = UPLOAD_DIR.$nextId."/".$delkey; unlink($fp); } Yes sir. Its exactly what I needed. Now I want to insert these data into mysql. Sir, can you tell me how to do it? I tried it something like this, but it doesn't work. //Insert other post image: if(!empty($_SESSION['other-image'])) { $query = "INSERT INTO post_image ( image_type , image , thumb , image_path , sort_order , added_date ) VALUES (?,?,?,?,?,NOW())"; $stmt = $mysqli->prepare($query); $stmt->bind_param('isssi', $type , $image , $thumb , $path , $sort_order ); $order=2; foreach ($_SESSION['other-image'] as $value) { foreach ($value as $v) { $type = $_SESSION['other-image']['type'][$v]) $image = $_SESSION['other-image']['img'][$v]) $thumb = $_SESSION['other-image']['thumb'][$v]) $path = $_SESSION['other-image']['path'][$v]) $sort_order = $order; $stmt->execute(); } ++$order; } } Link to comment Share on other sites More sharing options...
Barand Posted May 22, 2018 Share Posted May 22, 2018 Rearrange the array so you gather the values for each record together $other_image = Array ( 'img' => Array ( '0' => '1526973657.jpg', '1' => '1526973661.jpg', '2' => '1526973665.jpg' ), 'path' => Array ( '0' => '../post-upload/1/', '1' => '../post-upload/1/', '2' => '../post-upload/1/' ), 'type' => Array ( '0' => 1, '1' => 1, '2' => 1 ), 'thumb' => Array ( '0' => 'thumb_1526973661.jpg', '1' => 'thumb_1526973665.jpg', '2' => 'thumb_1526973668.jpg' ) ); $record_vals = []; // values for each record to be inserted foreach ($other_image as $name => $values) { foreach ($values as $k => $v) { $record_vals[$k][$name] = $v; } } Now you have an array that you can loop through to insert the records $record_vals = Array ( [0] => Array ( [img] => 1526973657.jpg [path] => ../post-upload/1/ [type] => 1 [thumb] => thumb_1526973661.jpg ) [1] => Array ( [img] => 1526973661.jpg [path] => ../post-upload/1/ [type] => 1 [thumb] => thumb_1526973665.jpg ) [2] => Array ( [img] => 1526973665.jpg [path] => ../post-upload/1/ [type] => 1 [thumb] => thumb_1526973668.jpg ) ) Link to comment Share on other sites More sharing options...
thara Posted May 22, 2018 Author Share Posted May 22, 2018 Sir, I tried it in this way: //Insert other post image: if(!empty($_SESSION['other-image'])) { $record_vals = []; foreach ($other_image as $name => $values) { foreach ($values as $k => $v) { $record_vals[$k][$name] = $v; } } $query = "INSERT INTO post_image ( image_type , image , thumb , image_path , sort_order , added_date ) VALUES (?,?,?,?,?,NOW())"; $stmt = $mysqli->prepare($query); $stmt->bind_param('isssi', $type , $image , $thumb , $path , $sort_order ); $order=2; foreach ($record_vals as $k = > $v) { foreach ($v as $column => $value) { $image = $record_vals[$k]['img'][$value]; $path = $record_vals[$k]['path'][$value]; $type = $record_vals[$k]['type'][$value]; $thumb = $record_vals[$k]['thumb'][$value]; $sort_order = $order; $stmt->execute(); } ++$order; } } Can you tell me what is the mistake I have done? Link to comment Share on other sites More sharing options...
Barand Posted May 22, 2018 Share Posted May 22, 2018 For testing, I created an array $other_image. Yours is in a session variable. Don't just copy and paste without reading the code. As for processing the record_vals array - examine the structure. You are greatly over complicating it, $sort_order = 2; foreach ($record_vals as $rec) { $image = $rec['img']; $path = $rec['path']; $type = $rec['type']; $thumb = $rec['thumb']; $stmt->execute(); } Link to comment Share on other sites More sharing options...
Barand Posted May 23, 2018 Share Posted May 23, 2018 PS I gave you that approach to avoid giving you the complete solution yet keep the next step as simple as possible for you. An alternative, and shorter, method (note: still using my array and not your session var) $sort_order = 2; foreach ($other_image['img'] as $rec => $image) { $path = $other_image['path'][$rec]; $type = $other_image['type'][$rec]; $thumb = $other_image['thumb'][$rec]; $stmt->execute(); } Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.