2015-04-04 65 views
0

對不起,相當長的標題。使用PHP獲取排序查詢的第一行和第二行中的值之間的差異

我目前的表在我的數據庫: Table

然後我用下面的PHP代碼顯示在我的網站上的信息:

<?php 
 

 
function time_elapsed_string($datetime, $full = true) { 
 
    $now = new DateTime; 
 
    $ago = new DateTime($datetime); 
 
    $diff = $now->diff($ago); 
 
    $diff->w = floor($diff->d/7); 
 
    $diff->d -= $diff->w * 7; 
 
    $string = array('y' => 'year','m' => 'month','w' => 'week','d' => 'day','h' => 'hour','i' => 'minute','s' => 'second',); 
 
    foreach ($string as $k => &$v) {if ($diff->$k) {$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');} else {unset($string[$k]);}} 
 
    if (!$full) $string = array_slice($string, 0, 1); 
 
\t return $string ? implode(', ', $string) . ' ago' : 'just now'; 
 
} 
 

 
$servername = "localhost"; 
 
$username = "root"; 
 
$password = ""; 
 

 
$conn = new mysqli($servername, $username, $password); 
 
mysqli_select_db($conn,"ai-database"); 
 
// if ($conn->connect_error) {die("Connection failed: " . $conn->connect_error);} 
 

 
$sql = "SELECT * FROM `steam` ORDER BY `id` DESC LIMIT 1"; 
 
$query = mysqli_query($conn, $sql); 
 
while($row = mysqli_fetch_assoc($query)){ 
 
\t $cards = $row['cards']; 
 
\t $backgrounds = $row['backgrounds']; 
 
\t $emoticons = $row['emoticons']; 
 
\t $gifts = $row['gifts']; 
 
\t $timestamp = $row['timestamp']; 
 
} 
 

 
echo " 
 
    <div class='category'> 
 
     <h2>Steam Collection</h2> 
 
     <p class='info'> 
 
     Last Updated: <span>" . time_elapsed_string("@" . $timestamp) . "</span> 
 
     </p> 
 
     <div class='item'> 
 
     <div class='title'>Cards</div> 
 
     <div class='stat'>" . number_format($cards) . "</div> 
 
     </div> 
 
     <div class='item'> 
 
     <div class='title'>Backgrounds</div> 
 
     <div class='stat'>" . number_format($backgrounds) . "</div> 
 
     </div> 
 
     <div class='item'> 
 
     <div class='title'>Emoticons</div> 
 
     <div class='stat'>" . number_format($emoticons) . "</div> 
 
     </div> 
 
     <div class='item'> 
 
     <div class='title'>Gifts</div> 
 
     <div class='stat'>" . number_format($gifts) . "</div> 
 
     </div> 
 
    </div> 
 
\t "; 
 

 
?>

過了一會的CSS,我得到了這樣一個小東西:

Display

我想通過id(Desc)排序後收集第一和第二行,找到兩個值之間的差異並將其顯示在網站上。
我希望的結果應該是這樣的:

My hopes and dreams

我使用以下試過,但我覺得它看起來相當凌亂,可能是不好的做法了。

<?php 
 

 
$sql = "SELECT * FROM `steam` ORDER BY `id` DESC LIMIT 2"; 
 
$query = mysqli_query($conn, $sql); 
 
$i = 0; 
 
while($row = mysqli_fetch_assoc($query)){ 
 
\t if($i==0){ 
 
\t \t $cards = $row['cards']; 
 
\t \t $backgrounds = $row['backgrounds']; 
 
\t \t $emoticons = $row['emoticons']; 
 
\t \t $gifts = $row['gifts']; 
 
\t \t $timestamp = $row['timestamp']; 
 
\t \t $i+=1; 
 
\t } else { 
 
\t \t $cards2 = $row['cards']; 
 
\t \t $backgrounds2 = $row['backgrounds']; 
 
\t \t $emoticons2 = $row['emoticons']; 
 
\t \t $gifts2 = $row['gifts']; 
 
\t \t $timestamp2 = $row['timestamp']; 
 
\t } 
 
} 
 

 
?>

是否有一個更清潔,更有效的方式來做到這一點?

回答

1

爲什麼不直接使用MySQL?

例如

SELECT 
(
    (SELECT cards FROM steam ORDER BY id DESC LIMIT 1) 
    - (SELECT cards FROM steam ORDER BY id DESC LIMIT 1,1) 
) AS cardsDiff 

編輯:您可以將逗號分隔爲單個查詢。那麼在PHP中不需要額外的變量。

編輯2:刨出相同的表/數據如圖所示,見下圖:

mysql> DESC steam; 
+-------------+------------------+------+-----+---------------------+-----------------------------+ 
| Field  | Type    | Null | Key | Default    | Extra      | 
+-------------+------------------+------+-----+---------------------+-----------------------------+ 
| id   | int(10) unsigned | NO | PRI | NULL    | auto_increment    | 
| cards  | int(11)   | YES |  | NULL    |        | 
| backgrounds | int(11)   | YES |  | NULL    |        | 
| emoticons | int(11)   | YES |  | NULL    |        | 
| gifts  | int(11)   | YES |  | NULL    |        | 
| timestamp | timestamp  | NO |  | 0000-00-00 00:00:00 | on update CURRENT_TIMESTAMP | 
+-------------+------------------+------+-----+---------------------+-----------------------------+ 
6 rows in set (0.00 sec) 

mysql> SELECT * FROM steam; 
+----+-------+-------------+-----------+-------+---------------------+ 
| id | cards | backgrounds | emoticons | gifts | timestamp   | 
+----+-------+-------------+-----------+-------+---------------------+ 
| 1 | 191 |   419 |  187 | 32 | 2015-04-04 16:40:42 | 
| 2 | 192 |   419 |  187 | 41 | 2015-04-04 16:40:42 | 
| 3 | 190 |   351 |  20 | 56 | 2015-04-04 16:40:55 | 
+----+-------+-------------+-----------+-------+---------------------+ 
3 rows in set (0.00 sec) 

mysql> SELECT 
    -> (
    -> (SELECT cards FROM steam ORDER BY id DESC LIMIT 1) 
    ->  - (SELECT cards FROM steam ORDER BY id DESC LIMIT 1,1) 
    ->) AS cardsDiff, 
    -> (
    -> (SELECT backgrounds FROM steam ORDER BY id DESC LIMIT 1) 
    ->  - (SELECT backgrounds FROM steam ORDER BY id DESC LIMIT 1,1) 
    ->) AS backgroundsDiff, 
    -> (
    -> (SELECT emoticons FROM steam ORDER BY id DESC LIMIT 1) 
    ->  - (SELECT emoticons FROM steam ORDER BY id DESC LIMIT 1,1) 
    ->) AS emoticonsDiff, 
    -> (
    -> (SELECT gifts FROM steam ORDER BY id DESC LIMIT 1) 
    ->  - (SELECT gifts FROM steam ORDER BY id DESC LIMIT 1,1) 
    ->) AS giftsDiff, 
    -> (
    -> (SELECT timestamp FROM steam ORDER BY id DESC LIMIT 1) 
    ->  - (SELECT timestamp FROM steam ORDER BY id DESC LIMIT 1,1) 
    ->) AS timestampDiff; 
+-----------+-----------------+---------------+-----------+---------------+ 
| cardsDiff | backgroundsDiff | emoticonsDiff | giftsDiff | timestampDiff | 
+-----------+-----------------+---------------+-----------+---------------+ 
|  -2 |    -68 |   -167 |  15 |   13 | 
+-----------+-----------------+---------------+-----------+---------------+ 
1 row in set (0.00 sec) 
+0

我** **上午使用MySQL,但有每個區別就有點過分代碼比我的查詢正在尋找。相反,我已經去了Norbet Van Nobelen提出的想法。但謝謝你的建議:) – Kain 2015-04-04 16:48:55

+0

然後,逗號將它們分開?似乎你可以在一個查詢中完成你所需要的工作,而不必緩存PHP值。 – Ing 2015-04-04 16:51:17

+0

你能解釋一下嗎? – Kain 2015-04-04 16:52:27

1

在我看來你有一個相當乾淨的版本。通過爲您的記錄引入一個對象,而不是像現在那樣擁有許多參數,可以使它更好一些。然後

目前的版本看起來是這樣:

<?php 

$sql = "SELECT * FROM `steam` ORDER BY `id` DESC LIMIT 2"; 
$query = mysqli_query($conn, $sql); 
$recordArr=array(); 
while($row = mysqli_fetch_assoc($query)){ 
    $recordObj=array(); 
    $recordObj['cards']=$row['cards']; 
    $recordObj['backgrounds'] = $row['backgrounds']; 
    $recordObj['emoticons'] = $row['emoticons']; 
    $recordObj['gifts'] = $row['gifts']; 
    $recordObj['timestamp'] = $row['timestamp']; 
    $recordArr[]=$recordObj; 
} 


?> 
相關問題