2013-04-02 4 views
0

이미지 웹 사이트를 만들고 있는데 타임 스탬프 정렬에 도움이 필요합니다. 데이터베이스에서 정보를 얻으려면 약 5 가지 SQL 쿼리가 있습니다. 이 쿼리들 각각은 타임 스탬프를 얻습니다.PHP : 타임 스탬프 정렬

내가 원하는 것은 데이터베이스에서 가져 와서 foreach 루프를 사용하여 모든 쿼리의 이미지를 정렬하는 것입니다. 혼란 스러울 수 있으며, 이해하지 못한다면 의견을 말하십시오.

$images = array(); 

$stmt = $conn->prepare("SELECT images.*, group_images.* ORDER BY `timestamp` DESC"); 
$stmt->execute(); 

while ($images_row = $stmt->fetch(PDO::FETCH_ASSOC)) { 
    $images[] = array(
    'image_id' => $images_row['image_id']); 
} 
return $images; 

foreach($images as $image) { 
    echo $image['image_id']; 
    echo '<br/>'; 
} 

내가 시도한 쿼리가 작동하지 않는 것으로 판명되었습니다.

오류 :

$images = valley_images();

$sorted_data = array(); 

foreach($images as $key => $value) { 
    if ($key == 'timestamp') { 
     $sorted_data[$value][] = $images; 
    } 
} 

ksort($sorted_data); 
+0

데이터베이스 구조와 시도한 PHP를 볼 수 있습니까? – showdev

답변

1

If I understand you correctly, this might help:

$data; //data from database, assuming it has a "timestamp"-key 
$sorted_data = array(); 

foreach ($data as $key => $value) { 
    if ($key == 'timestamp') { 
     $sorted_data[$value][] = $data; 
    } 
} 

ksort($sorted_data); 

With that you get an array that is ordered by the timestamps of your values from the database. If there is only one entry to each timestamp you can spare the [].

+0

아주 좋아 보인다. 유일한 것은, 모든 데이터를 $ data에 어떻게 할당 할까? – Tuccinator

+0

예 : $ data = $ query1 + $ query2 + $ query3 ... 배열을 병합하는 데는 여러 가지 방법이 있습니다 (http://php.net/manual/de/function.array-merge.php 참조). 데이터베이스에서 데이터를 가져올 때 데이터가 어떻게 보이는지에 따라 다릅니다. – hurrtz

+0

아, 고맙습니다. 이제 $ sorted_data 내의 각 이미지를 반향 출력해야합니다. 나는 이것에 대해 어떻게 갈 것인가? 나는 보통 메인 포스트에 올린 foreach 루프를 사용한다. – Tuccinator