0
下面的腳本打印使用PDO基於MySQL查詢的表:如何限制打印特定陣列鍵的字符數
<?php
//PDO start
$dbh = new PDO(...);
$stmt = $dbh->prepare($query);
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$arrValues = $stmt->fetchAll();
//create table
print "<table> \n";
print "<tr>\n";
//add table headers
foreach ($arrValues[0] as $key => $useless){
print "<th>$key</th>";
}
print "</tr>";
//add table rows
foreach ($arrValues as $row){
print "<tr>";
foreach ($row as $key => $val){
print "<td>$val</td>";
}
print "</tr>\n";
}
//close the table
print "</table>\n";
?>
這工作得很好,但關鍵之一在數組包含一些段落文本很長。一個例子vardump低於:
array
0 =>
array
'Name' => string 'John' (length=5)
'Day' => string 'Monday' (length=6)
'Task' => string 'This is a really long task description that is too long to be printed in the table' (length=82)
'Total Hours' => string '5.00' (length=4)
我想有「任務」鍵只能用「......」在最後打印前50個字符。我不確定如何在foreach循環中添加該條件。任何提示將不勝感激。
謝謝!這是有道理的 - 但是我怎樣才能使這隻適用於其中一個鍵(任務)? –
@DanielC編輯。 –