Home  >  Article  >  Backend Development  >  求一段依据日期隔行显示内容列表的PHP代码

求一段依据日期隔行显示内容列表的PHP代码

WBOY
WBOYOriginal
2016-06-13 12:12:52876browse

求一段根据日期隔行显示内容列表的PHP代码

本帖最后由 ikaimi 于 2014-12-30 17:03:46 编辑
<br /><?php<br />  $sql = mysql_query("select title,times from table order by times desc limit 0,50"); <br />  while($row=mysql_fetch_array($sql)){ <br />  //每个日期只显示一次<br />    echo "<h2>".$row['times']."</h2>";<br />	//该日期发布的全部内容<br />	echo "<li>".$row['title']."</li>";<br />  }<br />?><br />


显示方式如下
12-30
12-30发布的内容1
12-30发布的内容2
12-30发布的内容3
12-30发布的内容4
12-30发布的内容5
12-29
12-29发布的内容1
12-29发布的内容2
12-29发布的内容3
12-29发布的内容4
12-29发布的内容5
12-28
12-29发布的内容1
12-29发布的内容2
12-29发布的内容3
12-29发布的内容4
12-29发布的内容5
------解决思路----------------------
<br /><?php<br />  $sql = mysql_query("select title,times from table order by times desc limit 0,50"); <br />  $init_time="";<br />  while($row=mysql_fetch_array($sql)){ <br />	if($init_time==''){ //第一次循环 显示时间和第一条信息类容<br />		$init_time=$row['times'];<br />		echo "<h2>".$row['times']."</h2>";<br />		echo "<li>".$row['title']."</li>";<br />	}else{   //不是第一条判断 时间与上一条是否相等<br />		if($init_time==$row['times']){  //相等的话说明是同一时间段的 直接打印内容<br />			echo "<h2>".$row['title']."</h2>";<br />		}else{	//不一样  打印时间 和 内容<br />			echo "<h2>".$row['times']."</h2>";<br />			//该日期发布的全部内容<br />			echo "<li>".$row['title']."</li>";<br />		}<br />	}<br />  }<br />?><br />

在你的代码基础上改了下,可以试试。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn