Jump to content

Create new text file monthly


DexterTheCat

Recommended Posts

I got an idea with my homepage in progress. It's a blog and I want to sort my post in a navigation bar. Is it possible to do a script with $date function to create a new file the 1th of every month. Then use file_get_content on the current month.

 

To clarify in this stage I haven't started connecting my database.

Edited by DexterTheCat
Link to comment
Share on other sites

Yes of course it's possible - not likely tho. If I were doing this, I would do it this way:

 

1 - store each day's post(s) in a db table with the date as the key field. Or perhaps, the date and a sequence number if you wanted to have multiple posts per day ie, multiple records per day.

2 - use php to build a dynamic dropdown to select the available month/year posts

3 - use a second dropdown built with ajax to show the available days for the month/year selected in #2

4 - use ajax again to then show the post(s) for the specific day selected in #3.

 

No nav bar cause it's not really (imho) navigation, but merely selection. I don't think you want to put any references to individual posts in a nav bar or immediately in a dropdown. You're probably going to have way too many posts to be able to present them to the user that way. Let the user drill down to the item he wants to see. Of course when the page is first displayed, I'd actually show the user the current day's post and then he/she can use the dropdowns to move to a different one. Maybe even a button to automatically go back one day and another button to go forward one day.

Link to comment
Share on other sites

To clarify in this stage I haven't started connecting my database.

 

Create a database and do it the right way. You are only creating more work and problems by trying to do this through flat files. Yes, there is a learning curve to get started with databases. But, the benefits are huge. For a simple blog it would not be a lot of work once you grasp the basics.

Link to comment
Share on other sites

Create a database and do it the right way. You are only creating more work and problems by trying to do this through flat files. Yes, there is a learning curve to get started with databases. But, the benefits are huge. For a simple blog it would not be a lot of work once you grasp the basics.

 

Yeah! I agree but I must make this one without...

Link to comment
Share on other sites

Yeah! I agree but I must make this one without...

 

Odd, you originally stated

 

 

To clarify in this stage I haven't started connecting my database

Which indicates you intend to use one at some point. But, now you say you must implement this w/o one.

 

 

Then use a flat-file database. Once you set up a real database later it would be a simple process to transition the data and update the code and add/change the connection settings. You would already have the queries and logic set up for accessing/modifying the data.

Link to comment
Share on other sites

Sounds awesome ginerjm! I will try that out. When I put my post to file now with a+ does it fill the file from the last post. Is there a way ro reverse so the most present post comes first?

 

 

You could read the file into an array with file():

http://www.php.net/manual/en/function.file.php

 

You can then loop through the array backwards...or reverse the array with array_reverse():

http://www.php.net/manual/en/function.array-reverse.php

Link to comment
Share on other sites

My final saying on this post.

 

You are choosing to IGNORE what Psycho told you - a guy with over 10k posts! - and proceeding to waste your time developing for a flat file system that will NOT be able to be used in the fashion I described for you, which YOU YOURSELF thought was awesome. Really?

 

Tell me you aren't going to be silly about this.

Link to comment
Share on other sites

My final saying on this post.

 

You are choosing to IGNORE what Psycho told you - a guy with over 10k posts! - and proceeding to waste your time developing for a flat file system that will NOT be able to be used in the fashion I described for you, which YOU YOURSELF thought was awesome. Really?

 

Tell me you aren't going to be silly about this.

I am not silly but I was told to do it this way!

I don't really get how a university course can teach php but not let the student use MySQL...

Link to comment
Share on other sites

If you are to store the blog entries in a flat file format. You'd probably want to also include the year and month in the filename, so not to overwrite blog entries entered from a previous year.

 

You could then do something like this

// define the path to the blogs
$blog_entries_path = 'path/to/blog_files/' . date('Y-m') . '.txt'; // example file path produced path/to/blog_files/2014-06.txt

// check to see if the path does not exists.
if(!file_exists($blog_entries_path))
{
    // create a new empty blog file for current year and month.
    file_put_contents($blog_entries_path, '');
}

// read the contents of the current months blog entries
$current_entries = file_get_contents($blog_entries_path); // or use file() - docs http://php.net/file

 

 

 don't really get how a university course can teach php but not let the student use MySQL...

Maybe they first want you to create an application using PHP's file functions, so you can develop a good understanding of the PHP language. Then maybe later on in your course they will start to introduce you to databases.

Link to comment
Share on other sites

If you are to store the blog entries in a flat file format. You'd probably want to also include the year and month in the filename, so not to overwrite blog entries entered from a previous year.

 

You could then do something like this

// define the path to the blogs
$blog_entries_path = 'path/to/blog_files/' . date('Y-m') . '.txt'; // example file path produced path/to/blog_files/2014-06.txt

// check to see if the path does not exists.
if(!file_exists($blog_entries_path))
{
    // create a new empty blog file for current year and month.
    file_put_contents($blog_entries_path, '');
}

// read the contents of the current months blog entries
$current_entries = file_get_contents($blog_entries_path); // or use file() - docs http://php.net/file

Maybe they first want you to create an application using PHP's file functions, so you can develop a good understanding of the PHP language. Then maybe later on in your course they will start to introduce you to databases.

 

MySQL is my own summer project when the university don't have any courses in MySQL or databases.

 

Thank for your helpfull adivce and tips.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.