2012-04-15 75 views
-1

我有我的代码编写来从我的数据库中选择图像,并显示它们,按月分组。我希望每个'照片月份'都在自己的“容器”中。我无法弄清楚在WHILE LOOP中如何做到这一点。我想我需要一种方法来判断下一行数据是否会迭代到新的一个月。有什么建议么?创建一个新的div在一段时间循环

我的代码:

<?php 

if (isset($_COOKIE['user_id'])) 
{ 
if (!isset($_GET['user_id'])) 
{ 
$user_id= $_COOKIE['user_id']; 
} 
else 
{ 
$user_id= $_GET['user_id']; 
} 

$connect= mysqli_connect("localhost", "root", "", "si"); 
$query= "SELECT * FROM posts WHERE user_id= $user_id ORDER BY date DESC"; 

$result= mysqli_query($connect, $query) 
or die('error with query'); 
$date = "0"; 

while ($row= mysqli_fetch_array($result)) { 

if ($date != $row['date']) { 
echo "<p> ".$row['date']."</p><br/>"; 
$date = $row['date']; 
} 

echo '<img src="'.$row['picture']. '"/>' . "<br/>"; 
} 
} 
?> 

下面是它应该如何看...

enter image description here

例如,2012年2月一个月后应该是在不同的容器DIV于2012年一月

回答

1

这是很容易在您创建的头一个月同一个地方来实现这一点。

$first = true; 
if ($date != $row['date']) { 
    if ($first) { 
     $first = false; 
    } else { 
     echo "<\div>"; 
    } 
    echo "<div><p> ".$row['date']."</p><br/>"; 
    $date = $row['date']; 
} 

然后,你必须通过调用关闭后while循环的最后一个div:

echo "</div>"; 
0

我会尝试这样来检查一个新的容器是否已被打开:

<?php 

if (isset($_COOKIE['user_id'])) 
{ 
if (!isset($_GET['user_id'])) 
{ 
$user_id= $_COOKIE['user_id']; 
} 
else 
{ 
$user_id= $_GET['user_id']; 
} 

$connect= mysqli_connect("localhost", "root", "", "si"); 
$query= "SELECT * FROM posts WHERE user_id= $user_id ORDER BY date DESC"; 

$result= mysqli_query($connect, $query) 
or die('error with query'); 
$date = "0"; 

$firstmonth = true; //used to tell if a div has been opened yet or not 

while ($row= mysqli_fetch_array($result)) { 

if ($date != $row['date']) { 
if(!$firstmonth){ //close existing row first if needed 
    echo "</div>"; 
    $firstmonth = false; 
} 
echo "<div class="month-container"><p> ".$row['date']."</p><br/>"; 
$date = $row['date']; 
} 

echo '<img src="'.$row['picture']. '"/>' . "<br/>"; 
} 
} 
?> 
0
<?php 

if (isset($_COOKIE['user_id'])) 
{ 
if (!isset($_GET['user_id'])) 
{ 
$user_id= $_COOKIE['user_id']; 
} 
else 
{ 
$user_id= $_GET['user_id']; 
} 

$connect= mysqli_connect("localhost", "root", "", "si"); 
$query= "SELECT * FROM posts WHERE user_id= $user_id ORDER BY date DESC"; 

$result= mysqli_query($connect, $query) 
or die('error with query'); 

$date = "0"; 
$it= 0; 

while ($row= mysqli_fetch_array($result)) { 

if ($date != $row['date']) { 
if ($it < 1) { 
echo "<div class='container'><p> ".$row['date']."</p><br/>"; 
$date = $row['date']; 
$it ++; 
    } 
else { 
echo "</div><div class='container'><p> ".$row['date']."</p><br/>"; 
$date = $row['date']; 
$it ++; 
} 

}

echo '<img src="'.$row['picture']. '"/>' . "<br/>"; 
} 
} 

?>

0

尝试财产以后这样的:

<?php 
for($i =1; $i <= 12; $i++) 
{ 
$result = mysql_fetch_array(mysql_query("SELECT * FROM `posts` WHERE `user_id` = '" . $user_id . "' AND `month` = '" . $i . "'")); 
if(count($result)) 
    { 
     // create the container and the images for the current month here 
    } 
} 
?> 
+0

你可以有12个月以上,你可以显示几年的照片。 – Laky 2012-04-15 17:18:08