Jump to content

Generate a Log of Multiple AJAX and PHP Calls


iwato

Recommended Posts

BACKGROUND:  I have learned to keep a log that increases incrementally with the generation of each new newsletter and podcast.  

1) Read the Current Log Value

$file = './episode_index.txt';
if (file_exists($file)) {
  $json_str = file_get_contents($file);
  $episode_no = json_decode($json_str, true);
  $number = $episode_no['episode_no'];
  $new_episode = $number + 1;
} else {
  echo 'The desired file either does not exist, or does not exist in the indicated location.';
}

2) Take Some Action and Update the Log Value Incrementally


if (!is_blank($_POST['letter_no'])) {
  $episode_no['episode_no'] = $number + 1;
  $json_str = json_encode($episode_no);
  file_put_contents($file, $json_str);
}

in effect, I perform a simple file_get_contents and file_put_contents on a single document whose only value is that of a single JSON object whose value increases incrementally with the creation of each new newsletter.  In order to keep track of my podcasts I need something vastly more complex, for not only must I be able to automatically generate a new counter with each new podcast that I generate, but each counter thus created must keep track of each and every hit of any and all podcasts by each and every known and anonymous podcast user.  Further, once this data is recorded it must be easily accessible for the purpose of analysis.

In effect, I am loathe to generate  a separate document for each new podcast.  Although a workable tool for each individual podcast, i cannot imagine it as a very effective way to manage data for all of my podcasts in aggregate or any desired subset thereof.

Perhaps I could create a single MySQL table with a set number of fields for all podcasts.  Then,

1) With each new podcast insert a new row of data, and

2) with each new user hit of a particular podcast update the field  values for the affected row.

Might I receive some input in these regards?

Roddy

 

Link to comment
Share on other sites

That's the basic way people handle stuff like that, there's a reason databases have become so popular as a data store since the 60s and 70s.  People don't want to mess with files for that kind of thing, especially in a system with a lot of accesses or simultaneous connections.

You don't necessarily need a single table with a set number of columns for all podcasts though.  That's the easiest and most simple way to set it up, and works for the vast majority of use cases, but it's not required to set up the database like that.

  • Like 1
Link to comment
Share on other sites

OK.  So, I am off to a good start.

Now, what is the easiest way to increment selected column values of a MySQL selected row.

Surely, one does not have to read, process, and then update.

Roddy

Link to comment
Share on other sites

This is how you increment the value of a field with an SQL query:

UPDATE `field1` SET `field1` = `field1` + 1 WHERE `field2` = 'value'

You probably will need to adjust this query to match your table's structure and software requirements. Since I don't have any information about your project or database structure I'm just providing a very generalized solution.

  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...