Pregunta

I'm making a news website for games and I want a page where you can find all the news subject. So I decided to use it like a lot of webpages do.

Example:

enter image description here

Now I want it like that, but i don't know how to make that like that. I've made a little beginning and I hope you guys can help me with this!

$query = mysql_query("SELECT * FROM `gb_news` order by `datetime` DESC");

while($news = mysql_fetch_array($query)) {

        echo $news['title'];
        echo $news['datetime']; 

}

It's a really simple code, but I only want to know how I add these days when an artical has a new date in the database.

¿Fue útil?

Solución

Store the date outside your loop, then display a title only when the date changes.

$date = "";
while ($news = mysql_fetch_array($query)) {
    if ($news['datetime'] != $date) {
        $date = $news['datetime'];
        echo $date;
    }
    echo $news['title'];
}

Otros consejos

Something like this may work for you:

$query = mysql_query("SELECT * FROM `gb_news` order by `datetime` DESC");

$prevDate ='';
while($news = mysql_fetch_array($query))
{
    $tmpDate = DateTime::createFromFormat('Y-m-d H:i:s', $news['datetime']);
    if($prevDate!=$tmpDate)
    {
        $prevDate = $tmpDate->format('d/m/Y');
        echo '<h1>'.$prevDate.'</h1><hr />';
    }
    echo '<p>'.$tmpDate->format('H:i').' - '.$news['title'].'</p>';
}

This is assuming your datetime column is in the format YYYY-MM-DD hh:mm:ss and you want to display the date as DD/MM/YYYY, obviously edit to taste!

Also your PHP version must be >= 5.3.0 to use the DateTime class

Well what you can do is store the date and then change the layout each time the date changes:

while($news = mysql_fetch_array($query)) {
  if(empty($date) || $date != date("format", $news['datetime'])){
     $date = date("format", $news['datetime']);
     //echo/print some html to show new section
  }  
  echo $news['title'];
  echo $news['datetime']; 
}

This will handle the date comparisons and knowing when to change dates. "format" indicates the kind of date formatting you would like since you can echo/print the $date.

PHP date

NOTICE: Do not use MySQL_* for it has been deprecated as of PHP 5.5. Use MySQLi_* or PDO instead

I'm not sure I quite understand what you're trying to accomplish. To get your data to display like the image you included you would want to group items of the same date either in MySQL or PHP (or combination really).

Something like:

while ($news...) {

    // Get this article date
    $thisDate = new DateTime($news['datetime']) // Assuming this is a true datetime value

    // Compare this article date with previous article date to determine if we should start a new date header
    if (!empty($onDate) // We have previous articles to compare with
        && ($thisDate->format('l d F') == $onDate)) // This article date matches previous so keep in same group
    {
        echo $news['title'];
    } else { // No existing articles or new date = new header
        $onDate = $thisDate->format('l d F'); // See options: http://www.php.net/manual/en/function.date.php
        echo $onDate
        echo $news['title'];
    }

}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top