Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to update a text file using a condition in PHP?
    primarykey
    data
    text
    <p>Hello guys I have a problem with my code. Because I want to create a tool for updating google maps. But my data is in txt file not in database. That's why I am having a hard time coding it. Ok the scenario is:</p> <ol> <li>The user will locate it's new location</li> <li>After that, the user will click UPDATE and the program will write a txt file and a csv as well</li> <li>Now the condition is if the user update the map (meaning existing ID) in the txt file or csv. Instead of adding new updated location. It will update only.</li> </ol> <p>Here's my code.</p> <pre><code>if ($_SERVER['REQUEST_METHOD'] == 'POST') { $post_data = $_POST; if (!empty($post_data['company_id']) &amp;&amp; !empty($post_data['new_lat']) &amp;&amp; !empty($post_data['new_long'])) { // Save $restaurant_id = $post_data['company_id']; $new_lat_entry = $post_data['new_lat']; $new_long_entry = $post_data['new_long']; $datenow = date_create(); $timestamp = $datenow-&gt;format('U'); //THIS WILL CREATE ARRAY OF NEW DATA $data_add = array( 'restaurant_id' =&gt; $restaurant_id, 'new_lat' =&gt; $new_lat_entry, 'new_long' =&gt; $new_long_entry, 'date_updated' =&gt; $timestamp ); //GET THE EXISTING TXT FILE AND UNSERIALIZE IT $data = unserialize(file_get_contents('addresses.txt')); $data[] = $data_add; //ADD LATEST DATA TO ARRAY //SERIALIZE UPDATED ARRAY $serialize_data = serialize($data); file_put_contents("addresses.txt", $serialize_data, LOCK_EX); //write the text file //WRITE UPDATE OR NEW txtfile $array = unserialize(file_get_contents('addresses.txt')); //CREATE CSV $file_input = fopen("addresses.csv","w"); foreach ($array as $row) { fputcsv($file_input, $row); } fclose($file_input); } else { // prompt missing inputs } </code></pre> <p>What function do I need to use for that? That's all guys thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload