2014-04-08 42 views
2

我必須顯示最近上傳的圖片,但只有第一個條目不斷顯示,這是我們擁有的最早的圖片。我們的數據庫是建立這樣的:顯示來自PHP表格的最新年份

jos_campaigns_children

 
campaign_id | child_id |  code  | 
    1921 | 1921 | GTM-0103-A00627 | 

jos_campaigns_children_detail

 
detail_id | child_year | campaign_id | 
    5788 | 2013 |  1920 | 
    1921 | 2011 |  1921 | 
    3656 | 2012 |  1921 | 
    5789 | 2013 |  1921 | 
    1922 | 2011 |  1922 | 

HTML代碼源:

<img src="/child_images/<?php echo $row_children_list[code]; ?>-<?php echo $row_children_list[child_year]; ?>-S-1.jpg" 

當前HTML輸出:

<img src="/child_images/GTM-0103-A00627-2011-S-1.jpg" 

所需的輸出:

<img src="/child_images/GTM-0103-A00627-2013-S-1.jpg" 

我只是不知道語法用,使其僅顯示最近一年來代替$row_children_list[child_year]

下面是我找到的查詢(這是別人的代碼,所以我並不確切地知道我在做什麼:))

$query_children_list = "SELECT * FROM `jos_campaigns_children` a, `jos_regions` b, jos_campaigns_children_detail c WHERE (a.published ='1' and a.old_sponsor_id = '' and a.region_id = b.id and a.paypal_profile_status = '0' and a.campaign_id = c.campaign_id)" . $age_query . "" .$gender_query . "" . $location_query . " GROUP BY a.child_id ORDER BY a.campaign_id DESC LIMIT " . $starting_row . ", " . $row_per_page . ""; 

$result_children_list = mysql_query($query_children_list); 
+0

你的查詢在哪裏? –

+0

你如何建立'$ row_children_list []'? – paqogomez

+0

我可以建議'where child_year = max(child_year)'沒有查詢查看 –

回答

0

我看你是不是用在查詢內連接。沒有他們,很容易得到不可預知的結果。 在campaign_id的表1和表2上使用內部聯接。 Ref.

1

如果你想快速容易解決,這聽起來像你一樣,只是改變這一

$query_children_list = "SELECT * FROM `jos_campaigns_children` a, `jos_regions` b, jos_campaigns_children_detail c WHERE (a.published ='1' and a.old_sponsor_id = '' and a.region_id = b.id and a.paypal_profile_status = '0' and a.campaign_id = c.campaign_id)" . $age_query . "" .$gender_query . "" . $location_query . " GROUP BY a.child_id ORDER BY a.campaign_id DESC LIMIT " . $starting_row . ", " . $row_per_page . ""; 

這個

$query_children_list = "SELECT * FROM `jos_campaigns_children` a, `jos_regions` b, jos_campaigns_children_detail c WHERE (a.published ='1' and a.old_sponsor_id = '' and a.region_id = b.id and a.paypal_profile_status = '0' and a.campaign_id = c.campaign_id)" . $age_query . "" .$gender_query . "" . $location_query . " GROUP BY a.child_id ORDER BY c.child_year DESC LIMIT " . $starting_row . ", " . $row_per_page . ""; 

相關的變化是由child_year代替排序CAMPAIGN_ID。

+0

哦,那人看起來好像會起作用,但它只是顛倒了孩子們名單中的順序。圖像保持不變。雖然謝謝!我認爲這種效果教會了我更多關於這些事情的工作方式。 – Tony

+0

如果你想給我發電子郵件更多的代碼,我可以幫你。 –