English 中文(简体)
按日期分列的职业介绍/组数据(日)
原标题:Sorting/Grouping data by date (day)?
  • 时间:2011-10-23 10:14:12
  •  标签:
  • php
  • mysql

我正在为大家建立一个网站。 我希望有一个这样的名单。

Saturday 10/22/2011 (header)

Product title 1-city - 100$

产品名称 2-city - 74$

产品标题 3-city - 500美元

www.un.org/Depts/DGACM/index_spanish.htm 星期日 10/23/2011(标题)

产品标题 4-city - 150$

产品所有权 5-城市 - 220美元

产品标题 6-city - 10$

My code is ordering the list perfectly from newest to the oldest but I want to add something to the while loop to print out the headers (which are in bold) like (Saturday 10/22/2011). Basically i want to group the lists under the name of the day they were posted on here is my code:

$sql = "SELECT post_id,city, title, price, imgurl FROM md_post WHERE category= $cat  AND city= $city  ORDER BY timeStamp desc";

$result = mysql_query($sql);

while($row = mysql_fetch_array($result))
    {
echo "<a href= adid.php?id=".$row[ post_id ]." >". $row[ title ]. " - ". $row[ price ]." - ".$row[ city ]."</a>";
}
最佳回答

假定时间 Stamp是一个不完善的时局:

$sql = "SELECT post_id, city, title, price, imgurl, timeStamp FROM md_post WHERE category= $cat  AND city= $city  ORDER BY timeStamp desc";

$result = mysql_query($sql);

while($row = mysql_fetch_array($result))
{
    if (!isset($dateold) || $dateold != date( d ,$row[ timeStamp ]) {
        echo  <strong> .date( l  m/d/Y ,$row[ timeStamp ]).</strong><br />;
    }
    echo "<a href= adid.php?id=".$row[ post_id ]." >". $row[ title ]. " - ".$row[ price ]." - ".$row[ city ]."</a>";
    $dateold = date( d ,$row[ timeStamp ]);
}

当它不是一种不准确的时间,而是一次有效的时间时,你只能改变“条码”第一次出现在上。

问题回答
  1. add timestamp to your select fields
  2. introduce new variable that holds last used timestamp
  3. in while loop check if date form last used timestamp differs from current row, if so print header based on current row timestamp, assign it to last used timestamp and then print the rest.




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签