Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


PHP Task / Todo list without MySQL
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

PHP Task / Todo list without MySQL

RaymiiRaymii Member
edited July 2012 in General

!(http://raymii.org/cms/images/phptasklist.png)

I've written a PHP task / todo list. It is very lightweight, it requires no database (SQL). It uses a JSON file for the storage.
It runs perfectly on a few of my low end boxes. The source code is available, but quite messy.

I would like to have your opinions about it. I'm thinking of adding a due date, but furthermore it is perfectly for what I need... Any ideas for features?

Link: https://raymii.org/cms/p_PHP-task-list-v2

Comments

  • Have it make me a sammich.

    Just kidding, but it looks great!

  • Due date would be fine, maybe it checks against current "date()" and if it exceeds, colour it in red. if you have more than say, 48(config) hours, then it's "green", otherwise, yellow. (Maybe better, >48 = yellow else green).
    Why are you using Json? -- I would personally of just wrote to a file and "split" it up using deliminators. Then again, i've never used JSON!

    Looks good, source isng too messy, but if you commented it, annotated, that would be great for somebody to have a look, and possibly modify for their own use.

    Thanks,
    Chris.

  • RaymiiRaymii Member
    edited July 2012

    @eastoncch Colouring would be a great idea, thanks!.

    I first tried a csv file, and an INI file, but it becomes a mess if you have to update or delete specific items. PHP has since version 5.2 built in support for json, and with json-encode and json-decode I just get an array I can use and/or manipulate. And a colleague of mine tipped me about json, so I wanted to give it a try...

    And source code is just two files of php, 1 index and 1 to do the magic (a big nested if with a few checks and file writes).

  • Wow, how easy did you find JSON? Did you have any guides to follow off it? I'm a beginner when it comes to PHP, but I'd love to have some form of data storage, without using MySQL (Becomes slightly inefficent when it's just a few columns and rows..).

    Thanks,
    Chris.

  • RaymiiRaymii Member

    @eastonch Well, to be honest, it was a breeze. Here's the code to add a task:

    $id=substr(md5(rand()), 0, 20); $file="task.json"; $value=htmlspecialchars($_GET['content']); $current = file_get_contents($file); $current = json_decode($current, TRUE); $json_a = array($id => array("task" => $value, "status" => "open")); if(is_array($current)) { $current = array_merge_recursive($json_a, $current); } else { $current = $json_a; } $current=json_encode($current); if(file_put_contents($file, $current, LOCK_EX)) { echo"Task added"; } else { echo"failure."; }

    Updating values, deleting values and whatsever, in my case is just easy array manipulation.

    I had to fiddle a little bit with the manipulating, there are a few tutorials on how to read json with php, they do not cover the manipulating or writing back. Turns out that is just array manipulating and then json_encode.

    And I agree with you, I think a database is way to much overhead for most of my projects.

  • When I start writing PHP again, I think i'll grow into some JSON arrays.

    I had a PHP Book which started quite beginner, then finished with it after about half way (mainly due to college assignments getting ontop of my love for php). Next year we have to write a simple login script using PHP. I think I may use MySQL or I may go out of the board and use JSON for that, and just encrypt everything using MD5hash & salt.

    Have you ever thought of using a SQL flatfile, rather than JSON? I don't know how much of a performance "gain" or "degain" this would give.

  • RaymiiRaymii Member

    With a login script it might be a problem that the .json file is publicly accessible if you do not address that issue. (placing it in a non-public folder). You do have the advantage of being not vulnerable for SQL injection (altough good programming can handle that ass well).

    I don't think I know what you mean by an SQL flatfile, do you mean SQLite?

  • altough good programming can handle that ass well

    Epic typo is epic :p

    I think he does mean sqlite yes

  • debugdebug Member

    You could of used the uniqid() function instead of what you have now. Also, why are you using the array_*_recursive functions? You can easily do this:

    $current[$id] = array('task' => $task, 'status' => 'open');
    

    Also, when you use the javascript:

    < script type="text/javascript">
        window.location = "index.php"
    < /script>
    

    Why not just use header(), instead? Example:

    header('Location: index.php');
    

    So people with javascript disabled would get redirected.

  • @djvdorp
    LOL! I can handle all the ass well :']!

    @Raymii
    Thanks for the heads up, I'll look into it. My tutor will be rather impressed, since this year I was the only one with ANY coding experiance, and we had to code HTML, CSS, (I did PHP Mail scripts too), and some VisualBasic units.

    Next year, I'm going to code the login script using MYSQL, have "strip" functions for any input to remove MySQL injection possibilities. May also just write a functions.php and include it, show some class with it all.

  • subigosubigo Member

    @eastonch said: Next year, I'm going to code the login script using MYSQL, have "strip" functions for any input to remove MySQL injection possibilities.

    strip_tags isn't going to protect you against SQL injections. Read up on sanitizing input and PDO.

  • RaymiiRaymii Member

    @eastonch strip tags is indeed not going to help. This one might: http://www.php.net/manual/en/mysqli.real-escape-string.php

    @Debug the javascript was for debugging, is going to be removed. And people without javascript an click the link, right?
    The ID thing was just a quick fix. I had not heart of the uniqueID one, thanks.
    The array recursive is, as I thought for the recursive arrays I use.

    Are you sure you guys can handle that much ass?

    Thanked by 1djvdorp
  • telephonetelephone Member
    edited July 2012

    @Raymii said: @Debug the javascript was for debugging, is going to be removed. And people without javascript an click the link, right?

    You can just as easily use a header().

    @Raymii said: The ID thing was just a quick fix. I had not heart of the uniqueID one, thanks.

    Since you're only using one file/array there's no reason to set a unique id. Just increment the array:

     $json_array[] = array('task => $task, 'status' => 'open');
    // return the id of the inserted value
    $id = key(end($json_array));

    @Raymii said: The array recursive is, as I thought for the recursive arrays I use.

    There's no need to be recursive with arrays unless you're dealing with unknown or multiple values.

    // Use the following to return given task
    $json_a[$taskid]['task']

    Last, you should only re-open the 'task.json' file if it's changed since opening:

    // at start of file (after initial $_GET checks)
    $filemd5  = md5_file($file);
    $jsonfile = file_get_contents($file);
    $json_a   = json_decode($jsonfile, true);
    
    /**
    * if file has been updated, re-open file
    */
    if ($filemd5 !== md5_file($file)) {
        $jsonfile = file_get_contents($file);
        $json_a   = json_decode($jsonfile, true);
    }

    I might have some free time later today, if so I'll rewrite the 'action.php' file to show you how I would have done it :)

    Thanked by 1Raymii
  • RaymiiRaymii Member

    @telephone said: Since you're only using one file/array there's no reason to set a unique id. Just increment the array:

    >
    The choice to use an ID instead of a number is that 1, I want to add more data to it, and 2, what if I have 4 tasks, and i delete task two and three? Will it go on to add task 5?

    @telephone said: There's no need to be recursive with arrays unless you're dealing with unknown or multiple values.

    >

    @telephone said: Last, you should only re-open the 'task.json' file if it's changed since opening:

    >
    Thanks for the info. I'll try to make time to add/change these little bugs. I'm however going back to the night shift so it might take a while :p

Sign In or Register to comment.