0

I have written a code that outputs all termins i have the day. The calendar for this is already finished. But the program output, I want to use with python don't work.

My code:

<?php

mysql_connect(deleted);
mysql_select_db("DB1367141")

$day = date("d");
$month = date("m");
$year = date("Y");

$result = mysql_query("SELECT * FROM entries WHERE day = '$day' AND month = '$month' AND year = '$year' ORDER BY hour DESC, minute");

$array = {};

while($obj = mysql_fetch_object($result))
{
    if($obj->hour != "--")
        $hour = splitf("%02d", $obj->hour);
    else
        $hour = $obj->hour;

    if($obj->minute != "--")
        $minute = splitf("%02d", $obj->minute)
    else
        $minute = $obj->minute;

    array_push($array, {"hour":$hour, "minute":$minute, "text":$obj->tex});
}

var_dump($array);
echo json_encode($array);

?>

But if i run it, it neither outputs the array than an "echo('hi');"

I tried out to put this echo at the begin, at the end and in the middle. But the output is the same: nothing

In another forum I found out that I have to write:

$array = array();

But the output is the same: nothing.


i tryed both, right syntax and error report. as i changed $array = array(); nothing changed. i added two lines in my .htaccess to get better error report, and it outputed a 500 Internal Server Error.

Prix
  • 19,417
  • 15
  • 73
  • 132
  • Have you checked that your db queries are returning results? Have you echoed that you're going to put into your array to ensure that they are set? – i alarmed alien Oct 17 '14 at 12:00
  • Please copy the relevant pieces of code into your question. – Jørgen R Oct 17 '14 at 12:01
  • Enable errors and you would see your syntax is all wrong. for a start `array_push($array, {"hour":$hour, "minute":$minute, "text":$obj->tex})` should be `array_push($array, ["hour"=>$hour, "minute"=>$minute, "text"=>$obj->tex])` you seem to be mixing js syntax with php. Any decent code editor would have highlighted this mistake for you – Steve Oct 17 '14 at 12:02
  • Please read [How to get useful error messages in php](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) . In you pastebin example you have a syntax error. The line with `$array = {};` is not valid. What have you had changed when you tried `$array = array();`? – enricog Oct 17 '14 at 12:03

2 Answers2

0

To instantiate a blank array in PHP is

$array = array();

EDIT: as enricog said :)

Also, for simplicity sake, array_push isn't needed, I'd just use:

$array[] = array("hour" => $hour, "minute" => $minute, "text" => $obj->tex);
  • ok i'll use array() but $array[] = array(...); only allows one entry right?, i want to make more than one. maybe i shoud write: array_push($array, array("hour":$hour, "minute":$minute, "text":$obj->tex)) –  Oct 17 '14 at 13:04
  • the [] simply adds the entry to the end of the array... if you put a value in there, like ["my_key"] or [0] it'll overwrite it every time.. as it stands in my code, the array will look something like this: $array[0] = first item $array[1] = second item $array[2] = third item – chris_eyekiller Oct 17 '14 at 13:12
  • ok thanks, but the problem is not fixed there is the 500 left –  Oct 17 '14 at 14:01
  • Ahh right... the 500 error might be due to the server provider (if applicable).. some hosting companies only allow certain commands. I assume you put php set commands?! If so, take them out and put ini_set("ini.name", value); in the top of your php file... If not, paste the none-sensitive parts of your htaccess file as that's def' what's causing the 500 – chris_eyekiller Oct 17 '14 at 14:09
0

Might I recommend you try converting your date storage to timestamps in the name of simplicity. That way you only need to check that the entry is between the first and last second of "today".

You'd save yourself quite a lot of hassle and if you really need to output as in that JSON layout could just parse the timestamp using:

while($obj = mysql_fetch_object($result)) {
    $array[] = array(
        'hour' => date('H', $obj->timestamp,
        'minute' => date('i', $obj->timestamp,
        'text' => $obj->tex
    );
}
echo json_encode($array);

date('n', strtotime($strTimestamp));
Simon
  • 816
  • 2
  • 7
  • 16
  • the problem is, how to get the timestamps in the database, how to make timestamps of the future? –  Oct 19 '14 at 14:52
  • That's beyond the scope of this question and for the sake of not muddying things up would say there are many resources to handle that which can be found very quickly, check out the PHP manual 'time' for manipulation and the TIMESTAMP field for your database. – Simon Oct 20 '14 at 03:22