sql >> Base de Datos >  >> Database Tools >> phpMyAdmin

cómo hacer una cotización del día usando php y mysql en orden por id

    quotes
    ----------------------------------
    | id | data        | data2
    ----------------------------------
    | 1  | first quote | translated quote
    | 2  | second...   | bla bla

Y luego lo seleccionas como:

   $firstday="2011-06-06";
    $getquote = mysql_query("SELECT * FROM quotes WHERE id=(DATEDIFF(CURDATE()+1, '$firstday'))");
$quote = mysql_fetch_object($getquote);
echo $quote->data . $quote->data2;

¡¡EDITAR!!:He eliminado el archivo dateiff, por lo que el ID devuelto por la diferencia de fecha está DIRECTAMENTE en DONDE.

Lo que hace es calcular la diferencia entre el primer día y la fecha actual . Por lo tanto, cada día que dateiff será 1 más grande.DATEDIFF(CURDATE()+1, '$firstday') as datediff se puede interpretar como

datediff = differenceBetween(Currentday +1 and firstDay)
  • Ayer fue 2011-07-06, por lo tanto datediff = 2011-07-07 (there is +1!) - 2011-07-06 que es 1
  • hoy es 2011-07-08 - 2011-07-06 que es 2
  • mañana 2011-07-09 - 2011-07-06 que es 3
  • pasado mañana 2011-07-10 - 2011-07-06 que es 4
  • dentro de un mes será 2011-08-08 - 2011-07-06 que es 33

entonces, datediff es cada día más grande en 1

quotes
-------------------------
|id| data
-------------------------
|1| quote          day 1 (because date difference from start == 1)
|2| quote 2        day 2 (datediff == 2)
|3| quote 3        day 3 (datediff == 3)
|4| quote 4        day 4
.....

O en breve:cada día será una cotización diferente, comenzando con ID 1 en adelante.

No puedo explicar más que esto...

EDIT #2:5 cotizaciones al día

$offset = date_diff(new DateTime('now'), new DateTime('2011-08-29'))->format('%d');
$getquote = "SELECT * FROM quotes LIMIT {$offset},5";

segunda edición gracias a ajreal (Error de sintaxis SQL LIMIT )

EDIT #3:5 cotizaciones al día, cambiable por variable..

opción 1:

$choose=0; //statically defined, only first of that day will pop out

opción 2:

$choose = mysql_real_escape_string($_GET["qid"]); //which one will be defined in url.. (watch out, people can figure it out and browse through all quotes

opción 3:

$choose = rand(0,4); //will choose it randomly from those 5 daily quotes

Así que elige una de esas opciones que te gusten y agrégala antes de esta:

$offset = 5*date_diff(new DateTime('now'), new DateTime('2011-08-29'))->format('%d') + $choose;
$getquote = mysql_query("SELECT * FROM quotes WHERE id = '$offset'");
$quote = mysql_fetch_object($getquote);
echo $quote->data . $quote->data2;