Database data by days of the week

Asked

Viewed 141 times

0

I was thinking here and I couldn’t understand the way it would work.

Let’s assume that I have 1 database with 10 post and in one column had "Day of the week"

ID | NOME | DIA DA SEMANA
-------------------------
1  | ALAN | SEGUNDA-FEIRA
2  | ALEX | QUINTA-FEIRA
3  | BIA  | SEGUNDA-FEIRA
4  | CAIO | SEXTA-FEIRA
5  | KAIO | TERÇA-FEIRA
6  | LANA | QUARTA-FEIRA
7  | LUAN | SEGUNDA-FEIRA
8  | EVA  | SÁBADO
9  | ADÃO | DOMINGO
10 | MELO | DOMINGO

And when it was Monday my database log would show up on my home page, if every Tuesday with Tuesdays would show up on the home page and so on and so forth.

Could someone explain to me how this employee ?

  • You’re using Pdo, mysqli?

2 answers

1


First, take today’s date. O using the date(), it generates the days in English (Sat, Sun, Mon...), so I will use an array to form:

<?php
    $data = date('D');   
    $semana = array(
        'Sun' => 'Domingo', 
        'Mon' => 'Segunda-Feira',
        'Tue' => 'Terca-Feira',
        'Wed' => 'Quarta-Feira',
        'Thu' => 'Quinta-Feira',
        'Fri' => 'Sexta-Feira',
        'Sat' => 'Sábado'
    );
$dia_semana = $semana["$data"]; ?>

Note: the date('D') generates the date as day of the week as text. If you use lowercase, it will generate the day as number.

Now it’s simple, just SQL

$sql = "SELECT * FROM sua_tabela WHERE DIA_DA_SEMANA = '$dia_semana'";
  • Vlw man got it now I’m gonna do some exercises here.

  • Good studies! :)

0

If I understood your doubt correctly, it would be so:

# Para segunda:
SELECT * FROM tabela WHERE dia_da_semana = 'SEGUNDA-FEIRA'

# Para terça:
SELECT * FROM tabela WHERE dia_da_semana = 'SEGUNDA-FEIRA'

# E assim por diante...

Browser other questions tagged

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