2016-07-14 80 views
0

好的,所以在這裏我的代碼工作得很好,但我想要得到的是第一個顯示結果post_featured ='是'且不超過10天?如何顯示特色='是'首先發布然後休息?

如有post_featured具有價值它仍然在頂部,但只有當它的年齡不超過10天我的,但不知試過爲了它不會工作

我的代碼

$search_fields = array('post_area','post_category','post_type'); 
$and_clause = array(); 
$params = array(); 

foreach($search_fields as $field){ 
    if(isset($_GET[$field]) && $_GET[$field] != 'All'){ 
     $and_clause[] = "`$field` = :$field"; 
     $params[] = array(":$field",$_GET[$field],PDO::PARAM_STR); 
    } 
} 

$where_clause = ''; 
if(!empty($and_clause)){ 
    $where_clause = "WHERE " . implode(' AND ', $and_clause); 
} 

$query = "SELECT COUNT(*) FROM posts $where_clause"; 

if(empty($params)){ 
    $stmt = $db->query($query); 
}else{ 
    $stmt = $db->prepare($query); 
    foreach($params as $param) { 
     $stmt->bindValue($param[0],$param[1],$param[2]); 
    } 
    $stmt->execute(); 
} 

$total = $stmt->fetchColumn(); 
$pages = ceil($total/$per_page); 

$page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
'options' => array(
'default' => 1, 
'min_range' => 1, 
), 
))); 

$offset = ($page - 1) * $per_page; 

$params[] = array(':per_page',$per_page, PDO::PARAM_INT); 
$params[] = array(':offset',$offset, PDO::PARAM_INT); 

$query = "SELECT * FROM posts $where_clause ORDER BY postID DESC LIMIT :per_page OFFSET :offset"; 

if(empty($params)){ 
    $stmt = $db->query($query); 
}else{ 
    $stmt = $db->prepare($query); 
    foreach($params as $param){ 
     $stmt->bindValue($param[0],$param[1],$param[2]); 
    } 
    $stmt->execute(); 
} 
$result = $stmt->fetchAll(); 
+0

IM使用時間戳作爲創建\t時間戳\t CURRENT_TIMESTAMP –

+0

所以,你想在最近10天全部是通過降時間戳,那麼所有的是有序的,並沒有通過降時間戳? - 假設post_featured只有yes和no值。 –

+0

是@P.Salmon所有記錄都照常顯示,但最上面的應該是那些第一個post_featured的值爲是,但創建時間不超過10天,否則如果它比較老,那麼不需要在頂部顯示'是'的那些post_featured值 –

回答

0

鑑於表t

+------+---------------+------------+ 
| id | post_featured | dte  | 
+------+---------------+------------+ 
| 1 | yes   | 2016-07-15 | 
| 2 | no   | 2016-07-15 | 
| 3 | yes   | 2016-07-14 | 
| 4 | yes   | 2016-06-15 | 
| 5 | no   | 2016-06-15 | 
| 1 | yes   | 2016-06-14 | 
+------+---------------+------------+ 

,並要求以按2種方式你可以

結果

+------+---------------+------------+------+ 
| id | post_featured | dte  | srce | 
+------+---------------+------------+------+ 
| 3 | yes   | 2016-07-14 | 1 | 
| 1 | yes   | 2016-07-15 | 1 | 
| 5 | no   | 2016-06-15 | 2 | 
| 4 | yes   | 2016-06-15 | 2 | 
| 2 | no   | 2016-07-15 | 2 | 
| 1 | yes   | 2016-06-14 | 2 | 
+------+---------------+------------+------+ 
相關問題