Using database mysql and php data

Asked

Viewed 41 times

1

This code works correctly with fixed dates. How to use the dates that are registered in the mysql database?

if (!empty($_REQUEST['year']) && !empty($_REQUEST['month'])) {
$dates = array(
    array(
        'date' => '2016-09-17',
        'badge' => false,
        'title' => 'Evento para o dia 22-09-17',
        'body' => 'Descrição do evento',
    ),
    array(
        'date' => '2016-09-23',
        'badge' => false,
        'title' => 'Evento para o dia 22-09-23',
        'body' => 'Descrição do evento',
    ),
    array(
        'date' => '2016-09-30',
        'badge' => false,
        'title' => 'Evento para o dia 22-09-30',
        'body' => 'Descrição do evento',
    ),
    array(
        'date' => '2016-10-02',
        'badge' => false,
        'title' => 'Evento para o dia 22-10-02',
        'body' => 'Descrição do evento',
    ),
);
echo json_encode($dates); } else { echo json_encode(array()); }
  • what have you tried? A select * from table and some loop for example?

  • I managed to solve.

  • @Diegoribeirodasilva Poste you even the answer.

  • I’ve already edited and put the answer.

  • 2

    @Diegoribeirodasilva put the answer as answer.

1 answer

1

I managed to sort it out like this.

$mysqli = new mysqli('localhost','usuario','senha_database','tabela_database');
if ($result = $mysqli->query("SELECT * FROM eventos")) {

    while($row = $result->fetch_array(MYSQL_ASSOC)) {
            $dates[] = array(
                            'date' => $row['data'],
                            'badge' => false,
                            'title' => 'Evento para o dia' . $row['data'],
                            'body' => 'Descrição do evento',
                        );
    }
    echo json_encode($dates);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.