English 中文(简体)
利用PHP按周分类整理和显示MySQL条目
原标题:Using PHP to group and display MySQL entries by week

I m 建造一间有几台一线车的场地。 采用简单的PHP5表格添加这些表格,储存在MySQL 5数据库中。 每一行都有一笔补贴(auto_increment)、标题、说明、状况(1或0)和日期(MySQL 日期)。

我想按月和年组别显示:

<dl>
    <dt>August 2009</dt>
    <dd><strong>title 1</strong> - description 1</dd>
    <dd><strong>title 2</strong> - description 2</dd>
    <dd><strong>title 3</strong> - description 3</dd>         
    etc...
</dl>
    <dl>
    <dt>July 2009</dt>
    <dd><strong>title 1</strong> - description 1</dd>
    <dd><strong>title 2</strong> - description 2</dd>
    <dd><strong>title 3</strong> - description 3</dd>         
    etc...
</dl>

我发现,我的SQL的幻灯塔,按月显示各群体,然而,这种争 que只能带来两倍的结果,而不是完整的表格:

SELECT `title`, `description`, `date_added`
FROM one_liners WHERE `status`=1 GROUP BY MONTH(date_added)

我将如何把他们分类,然后如上所示。

任何帮助都将受到高度赞赏。

感谢!

最佳回答

您不想使用<代码>。 按条款列出。 虽然“声音”与你想要的东西一样,但它的影响完全不同。

正常问讯:

SELECT `title`, `description`, `date_added` FROM one_liners 
WHERE `status`= 1 ORDER BY `date_added`

接下来是结果,做的是:

$query =  SELECT `title`, `description`, `date_added` FROM one_liners WHERE `status`= 1 ORDER BY `date_added` ;
$res = mysql_query( $query );
$month = null;
$year  = null;

echo  <dl> ;
while($row = mysql_fetch_assoc( $res ){
  $r_month = strtotime( $row[ date_added ] );
  if($month != date( m , $r_month) || $year != date( Y , $r_month)){
     echo  <dt>  . date( F Y , $r_month) .  </dt> ;
     $month = date( m , $r_month);
     $year  = date( Y , $r_month);
  }
  echo  <dd><strong>  . htmlentities( $row[ title ] ) .  </strong> &mdash;  . htmlentities( $row[ description ] ) .  </dd> ;
}
echo  </dl> ;

<>m>I 专门选择只使用一个<代码>dl,因为dl应当持有许多<代码>d和dt 内容,而不仅仅是每个清单的1个。 如果你不同意的话,可自由调整我的准则:

问题回答

我认为,光靠购买力平价,就不需要这样做。 通常在SUM、AVG、Lex、MIN、COUNT等场合使用KQ。 它对确定的实际记录没有做任何事情,以显示你能够进入购买力平价的某种分类。

在类似局势中,我所做的事情是一成不变的,可以追踪到我何时进入新集团。 页: 1

SELECT `title`, `description`, `date_added`
FROM one_liners WHERE `status`=1 ORDER BY MONTH date_added DESC

然后,在PHP中,你做的是:

$prevMonth =   ;
while ($row = mysql_fetch_assoc($result)) {
    $currMonth = $row[ date_added ];
    if ($currMonth != $prevMonth) {
        if ($prevMonth !=   ) {
            echo  </dl> ;
        }
        echo  <dl><dt>  . $currMonth .  </dt> ;
        $prevMonth = $currMonth;
    }
    echo  <dd><strong>  . $row[ title ] .  </strong> ;
    echo   -   . $row[ description ] .  </dd> ;
}
if (mysql_num_rows($result) > 0) {
        echo  </dl> ;
}

所有这一切都确实在使上任的月子跟随你们。 当你拿到新东西时,它将关闭老的DL,并打开一个新东西。

视你如何将日期储存在行文中(时间档和日期)而定,你需要替换<代码> curr Month = 美元行文[日期:增加]; 附有适当的代码,将其转换为“2009年8月”或你想要的。

您不想使用<代码>。 小组由代替。





相关问题
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 ...

热门标签