Nasıl I) (ŞİMDİ zaman göz ardı edebilirsiniz?

5 Cevap php

I have a mysql database of entries with dates. So what I want is to show all the dates in my database and then under each date, I want to show all the entries in the database entered on the specefic date. I am thinking of two loops but I don't know how to write the condition to display all the dates in my database before I loop out the entries under that date.

<?php

$sql = 'select start_date, name from events order by start_date';

$res = mysql_query($sql) or die(mysql_error());

$prev_date = null;

while ($row = mysql_fetch_assoc($res)) {   if ($row['start_date'] != $prev_date) {
    echo "<h1>{$row['start_date']}</h1>"\n;
    $prev_date = $row['start_Date'];   }

  echo "<p>{$row['name']}</p>"; }

?>

Bir önceki soruda da (http://stackoverflow.com/questions/1395657/looping-out-mysql-data), bu kodu kullanarak sonuçlandı. Bu MySQL tarih ve saati çeker, ve ben tarih ve saati depolamak için NOW () kullanılır. Onu nasıl zaman görmezden yapabilirsiniz yüzden ne istediğinizi elde edebilirsiniz?

5 Cevap

CURDATE() yerine kullanın NOW ().

Böyle bir durum ile deneyin:

SELECT * FROM `events` WHERE DATE(`start_date`) = '2009-09-09';

Bu size 9 Eylül 2009 için veritabanındaki tüm olayları alırsınız. Ben, sizin için soruyorsun ne olduğunu düşünüyorsun?

Herhalde burada düzeltmek için birisi, ancak gerekir denenmemiş kodu gider:

SQL to retrieve all the dates that exist in the table:

$sql_get_dates = 'select start_date from events order by start_date distinct';

Ve * start_date * varsayarak bir DATETIME yazın SQL to get all events on a given date: olduğunu

$sql_get_events = 'select * from events where date(start_date) = "2009-08-09"';

Bunun yerine sadece tarih seçerek, sen tarih kesecek MySQL zaman bazı fonksiyonları kullanabilirsiniz.

$sql = "select date_format(start_date, '%Y-%m-%d') as fstart_date, name from events order by start_date";

Tabii ki, PHP kodu içinde fstart_date için START_DATE değiştirmeniz gerekecek.

Çıkış the Mysql reference page for DATE_FORMAT().