2016-02-10 27 views
0

我不是一個php忍者,所以請在我的查詢中忍受我。我有以下代碼(在這裏簡化示例格式,因爲很多其他不相關的東西正在實際代碼中發生):如何從php數組中以有序方式提取數據?

數據庫連接已經成功定義並由php文件開頭處的include處理。

// Define our $now value to compare times with 
$now = time(); 

// Define the numerical timestamps required for calculating stuff 
$min1st = 5140800; //59.5 days 
$max1st = 5227200; //60.5 days 
$min2nd = 7732800; //89.5 days 
$max2nd = 7819200; //90.5 days 
$delmar = 10368000; //120 days 

// Get each relevant entry from the key table 
try { 
    $stmt = $conn->prepare("SELECT * FROM keyTable WHERE `status` = 'neutral' ORDER BY `lastModified` ASC"); 
    $stmt->execute(); 
    $result = $stmt->fetchAll(); 
} catch(PDOException $e) { catchMySQLerror($e->getMessage()); } 

// Set up some instance counters 
$a = 0; 
$b = 0; 
$c = 0; 

// Create some arrays to pop the data into from the next stage so we can use it again later 
$arrA = array(); 
$arrB = array(); 
$arrC = array(); 

到目前爲止好。下一部分旨在通過使用我們在頂部附近設置的一些數據來發現哪些數據庫結果在指定的時間範圍內設置了「lastModified」值。這工作也相當好(我認爲)。

foreach($result as $value) { 
    // Lets find the ones that qualify for first email notification 
    if((($value['lastModified'] + $min1st) < $now) && (($value['lastModified'] + $max1st) > $now)) { 
     // Now only the entries that have been untouched for 60 days from today are listed here. Yay! Send out an email to the people responsible and remind them! Get user data... 

     try { 
      $stmt = $conn->prepare("SELECT * FROM users WHERE `uID` = :uid"); 
      $stmt->bindValue(':uid', $value['uID']); 
      $stmt->execute(); 
      $uRes = $stmt->fetchAll(); 
     } catch(PDOException $e) { catchMySQLerror($e->getMessage()); } 

     // Add +1 to the incremental count 
     $a++; 

     // Collect some data for the log email 
     $arrA[] = $value['entryKey']."-"$value['entryID']; 
     $arrA[] = $value['entryName']; 
     $arrA[] = $value['entryTitle']; 
     $arrA[] = $value['dateStarted']; 
     $arrA[] = $value['lastModified']; 
     $arrA[] = $uRes[0]['title']." ".$uRes[0]['firstName']." ".$uRes[0]['lastName']; 
     $arrA[] = $uRes[0]['email']; 

然後它會發送每個人在這if出現。用於發送電子郵件等的代碼完美無缺,所以不需要爲此帶來任何煩惱。每次使用其他參數重複相同的過程 - 您可能會通過觀察頂部附近的其他數字時間戳值來猜測工作原理。所以,讓我們進入本月底(這是問題所在,在後:

} 
} 

現在在這裏,我想拉的所有數據從$ ARRA的(以及相應的其他陣列$ ARRB和$ arrC),所以我可以將它們全部放入一個整潔的表格中,然後將它郵寄給管理員,這樣他們就可以知道在全部運行時發生了什麼。

我的問題是,我不知道如何以可用的方式提取,$ arrA。If I var_dump($arrA);我成功地獲得了應該在那裏的所有東西,但是我沒有看到一種方法來按照每個循環執行的行方法對它進行排序,我傾向於在執行var_dump($arrA);

array(14) { [0]=> string(15) "ABC-63" [1]=> string(36) "Fish and Chips" [2]=> string(33) "Cod Disposal" [3]=> string(22) "1447283057" [4]=> string(22) "1447286317" [5]=> string(21) "Mr Bob Smith" [6]=> string(22) "[email protected]" [7]=> string(15) "XYZ-104" [8]=> string(23) "Blue Socks" [9]=> NULL [10]=> string(22) "1447286691" [11]=> string(22) "1447326523" [12]=> string(20) "Mrs Rosie Jones" [13]=> string(34) "[email protected]" } 

顯然這裏有兩個數據輸入的輸出。我倒是知道如何塑造成一個輸出將基本上是這樣的:

<tr> 
<td>ABC-63</td> 
<td>Fish and Chips</td> 
<td>Cod Disposal</td> 
<td>1447283057</td> 
<td>1447286317</td> 
<td>Mr Bob Smith</td> 
<td>[email protected]</td> 
</tr> 
<tr> 
<td>XYZ-104</td> 
<td>Blue Socks</td> 
<td>NULL</td> 
<td>1447286691</td> 
<td>1447326523</td> 
<td>Mrs Rosie Jones</td> 
<td>[email protected]</td> 
</tr> 

表開始和結束顯然是到位前,分別在循環之後。

如何從$ arrA中獲得這個結果?我是否需要以某種方式修改我以前在數組中存儲這些數據的方式,以便在接近尾聲時能夠以某種簡單的方式管理該輸出?

在此先感謝。

+0

認爲我的輸出可能不夠清晰。更新輸出需要更好理解... – df0

回答

0

感謝@quentinadam我的靈感來試試這個。這不是我正在考慮使用的角度,但它確實有效。

echo "<table border=\"1\">\n<tbody><tr>\n"; 
$i = 0; 
foreach($arrA as $thing) { 
    if($i == 7) { 
     echo "</tr>\n<tr>\n"; 
    } 
    echo "<td>".$thing."</td>\n"; 
    $i++; 
    if($i == 8) { 
     $i = 1; 
    } else { 
     $i = $i; 
    } 
} 
echo "</tr></tbody>\n</table>\n\n"; 

我倒是贊成票quentinadam爲是在正確的軌道上,但還不能贊成票。

1

如果我理解正確,您要輸出$arrA的元素,每行7個元素。

你可以做的是迭代$arrA,用$arrA的元素填充一個臨時數組$line。每7個元素,該行將被填滿。然後,您可以對其進行任何操作,例如將其添加到標準輸出中,然後將其添加到電子郵件變量中,然後重置$line

$line = []; 
for ($i = 0; $i < count($arrA); $i++) { 
    $line[] = $arrA[$i]; 
    if ($i % 7 == 6) { //line is filled up with 7 elements 
    echo implode($line, " | "); //output elements of line separated with pipe char 
    $email .= implode($line, " | ") . "\n"; //add the line to email body 
    $line = []; //reset line 
    } 
} 
+0

試試看看發生了什麼。只導致了一些1245未定義的抵消錯誤.... – df0

+0

其實我做了我自己的版本,你在做什麼,它似乎工作! – df0

0

這人會幫助:

foreach($arrA as $index=>$value){ 
    echo($value.' |'); 
} 

造型是你的。但是如果你需要進一步的幫助,讓我更新。

+1

謝謝,但這只是在每個項目之後用管道吐出數組中的所有內容。它沒有達到什麼願望。我已經澄清了所需的輸出,你可以再看一次嗎?謝謝。 – df0

+0

@ df0肯定,但現在你已經自己解決了這個問題。恭喜! –