2016-02-10 25 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肯定,但现在你已经自己解决了这个问题。恭喜! –