2012-04-15 69 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