I have a problem in my code. My code is all about updating/deleting rows in textfile ig the textfile is found. I am having a trouble with it. At first I can add a new textfile and together with it I can also update this file. But it is for 1 row. If I update another row. It will just append the updated value at the end of the textfile. What I want is to update this or delete and insert the new one. But I don't know how. My process in updating the array is array_replace(). First I need to find out if the ID of my data is found in the textfile. If found I will simple update/delete and replace the existing data into my new updated data. If not found just add only.
Here's my code for that.
$restaurant_id = $post_data['company_id'];
$new_lat_entry = $post_data['new_lat'];
$new_long_entry = $post_data['new_long'];
/****Here's my new updated array ****/
$data_add = array(
'restaurant_id' => $restaurant_id,
'new_lat' => $new_lat_entry,
'new_long' => $new_long_entry,
'date_updated' => date('Y-m-d H:i:s')
);
/****This is the BASE array from the existing textfile ****/
$data = unserialize(file_get_contents('addresses.txt'));
$target = $data_add['restaurant_id'];
for ($i = 0; $i < count($data); $i++) {
$get_id = $data[$i]['restaurant_id'];
if($get_id == $target){
//If ID is found - UPDATE
$add_data = array();
$add_data = array(
$i => $data_add
);
$new_array = array();
$new_array = array_replace($data,$add_data);
$serialize_data = serialize($new_array);
$file_input_txt = fopen("addresses.txt","w+");
fwrite($file_input_txt,$serialize_data);
fclose($file_input_txt);
}else{
$new_array = array(
$i => $data_add
);
$serialize_data = serialize($new_array);
$file_input_txt = fopen("addresses.txt","w+");
fwrite($file_input_txt,$serialize_data);
fclose($file_input_txt);
}
}
The output of my text file is in serialized form.
a:1:{i:0;a:4:{s:13:"restaurant_id";s:4:"1519";s:7:"new_lat";s:8:"14.63823";s:8:"new_long";s:9:"121.02999";s:12:"date_updated";s:19:"2013-11-15 12:42:59";}}
That's all guys please help me. I have a deadline now. And I am stuck with it. :-( This is the first time I am creating a CRUD based on a text file that's why I am having a trouble debugging it.
Can you please try this,
<?php
/****This is the BASE array from the existing textfile ****/
$data = unserialize(file_get_contents('addresses.txt'));
$restaurant_id = '1519';
$new_lat_entry = '14.64823';
$new_long_entry = '121.45999';
/****Here's my new updated array ****/
$data_add = array(
'restaurant_id' => $restaurant_id,
'new_lat' => $new_lat_entry,
'new_long' => $new_long_entry,
'date_updated' => date('Y-m-d H:i:s')
);
$target = $data_add['restaurant_id'];
$Count =count($data);
$new_array =array();
for ($i = 0; $i < $Count; $i++) {
$get_id = $data[$i]['restaurant_id'];
//If ID is found - UPDATE
$add_data = array(
$i => $data_add
);
if($get_id == $target){
$new_array = array_replace($data,$add_data);
}
}
$serialize_data= serialize($new_array);
print_r($serialize_data);
$file_input_txt = fopen("addresses.txt","w+");
fwrite($file_input_txt, $serialize_data);
fclose($file_input_txt);
?>