2015-02-17 38 views
0

我做了一個搜索,看看我是否能找到我的答案,但這完全是我沒有使用正確的術語,所以隨時在我的背面打我頭部,並鏈接到正確的頁面,如果我錯過了:)PHP使用getResults根據結果篩選和迴應響應

我在做什麼是搜索一個特定的項目,在這種情況下MAC地址表。如果找不到,我想回顯「未找到」;

如果找到了,我想檢查該MAC的日期的另一行。如果日期早於指定日期,則回顯「回憶」;

如果MAC比日期更新,則回顯「MAC okay」;

這裏是什麼樣的能看起來像一個示例:

| mac    | date  | 
| 00-00-00-00-00-00 | 2014/10/17 | 

我的形式寫得好,它給了正確的信息到PHP,但在這裏它是無論如何:

<form action="results.php"> 
mac id 
<input type="text" name="mac"> 
<input type="submit"> 
</form> 

這裏是來自results.php的PHP。它連接表沒有問題。我認爲這個問題的核心是我的查詢:

<?PHP 
$mac= $_POST[‘mac’]; 

include "connect.php"; 

$query = mysql_query(「SELECT mac,date FROM units WHERE mac='$mac'"); 
$results = $query->getResults(); 

if(sizeof($results) > 0){ 
    // look up the documentation for date_diff, DateTime, and DateInterval. 
    $interval = date_diff($results[0]->date_built, new DateTime('2014/10/19')); 

    if($interval->invert = 1){ // i think this is how to test for before/after, but double-check me. 
    echo "recall"; 
    } 
    else { 
    echo "unit okay"; 
    } 
} 
else { 
    echo "not found"; 
} 

?> 

回答

0

假設你要檢查對當前的日期,這裏是我的嘗試:

include "connect.php"; 

$mac= $_POST[‘mac’]; 

$result = mysql_query("SELECT * FROM units WHERE mac='".$mac."'"); // query 

// no results 
if (!$result) { 
    echo 'not found'; 
} else { 
    $mac_date = $result[0]->date; 
    // check if its newer 
    if(strtotime($mac_date) > strtotime('now')) { 
    echo 'MAC ok'; 
    } 
    // check if its older 
    if(strtotime($mac_date) < strtotime('now')) { 
    echo 'recall'; 
    } 
} 
+0

這讓我在正確的方向。謝謝 – 2015-02-18 04:21:15

+0

沒問題,很高興我可以幫忙 – cch 2015-02-18 04:22:08