Depression Tracker

Using PHP, Javascript, and Bootstrap

Posted on 2015-06-19 01:57:00 in webdev, PHP

I think that most of the creative ideas I follow are born at the crossroads of two or more areas of knowledge. I have been looking for a way to practice PHP and I found it while reading this book called "Feeling Good: The New Mood Therapy".

I found a chart in the book that measures the well-being of the depressed and anxious that I wanted to try for myself.

I spent some time re-creating this chart in HTML and CSS and then I connected Javascript with PHP to store the form's info in a .csv file (see below), which I could later use to represent it graphically in excel or something.

Here is the javascript that hands the data off to the php in "test.php"...

var answers = [];

$('#submit_btn').click(function(){
    $('#input_column').find('input[type=text],select').each(function() {
        answers.push(this.value);
    });
    answers.push($('#total_input').val());
    $.post('test.php', {elements: answers});
});

And here is the PHP that catches the data and stores it to a .csv file...

$elements = $_POST['elements'];

$str = "";
foreach($elements as $item) {
    if ($str == "") $str .= $item;
    else $str .= ', ' . $item;
}

/* ------------ writing to new text file ------------*/

date_default_timezone_set('America/Denver');

$today = "burns_data/" . date("F j, Y, G.i.s a") . ".txt";

$ourFileName = $today;
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);

file_put_contents($ourFileName, $str);


/* ------------ writing to existing csv file ------------*/

$fs = fopen("file.csv","a");

fwrite($fs, "\n" . date("Ymd") . ", " . date("G:i:s") . ", ");

foreach ($elements as $fields) {
    fwrite($fs, $fields . ", ");
}

fclose($fs);

The most important thing that I learned from this project is that it's helpful to work towards an end product that you will actually use, or that will be used by others. I probably wouldn't have spent so much time working out the details if I didn't have a useful end goal in mind.

See it on Github