I want to fill an html table from an array organized as Key => value, i use zend Framework, and my problem here that Php didn't respect the order of the value in the array when i have to print them in an html code and this is my controller:
<?php
class Dashboard_LogsController extends Dashboard_Library_Custom_Controller_Dashboard
{
function array_merge_rec($arrays)
{
$result = array();
$keys = array('delivred','open', 'click', 'unsubscribe', 'soft', 'hard', 'deferred');
foreach ($arrays as $key => $value) {
if (is_array($value)) {
foreach ($value as $item) {
$result[$item['isp']][$key] = $item['count'];
}
}
}
$results = array();
foreach($result as $key => $value)
{
foreach($keys as $k)
{
if(!isset($result[$key][$k])) $result[$key][$k] = 0;
}
}
return $result;
}
public function logsAction()
{
$this->view->headLink()->appendStylesheet(STATIC_URL . '/css/bootstrap.datetimepicker.min.css');
$this->view->headScript()->appendFile(STATIC_URL . '/js/bootstrap.datetimepicker.min.js');
$log_review = new Dashboard_Model_DbTable_logs();
$open = $log_review->getOpen();
$click = $log_review->getClick();
$unsubscribe = $log_review->getUnsubscribe();
$deferred = $log_review->getDeferred();
$delivred = $log_review->getSent();
$SB = $log_review->getSB();
$HB = $log_review->getHB();
$result = $this->array_merge_rec(array('delivred'=>$delivred,'open' => $open, 'click' => $click, 'unsubscribe' => $unsubscribe, 'hard' => $HB, 'soft' => $SB, 'deferred'=>$deferred));
$this->view->data = $result;
}
}
and the html code that i used to show the table on the screen is the follow:
<div class="row-fluid">
<div class="span12 meter-legend meter-statistics">
<table>
<tr>
<th>ISP</th><th>Email address</th><th>Delivred</th><th>Open</th><th>Clicks</th><th>Unsubscribed</th><th>Hard</th><th>Soft</th><th>Deferred</th>
</tr>
<?php foreach($this->data as $name => $isp) { ?>
<?php
$sum =0;
foreach($isp as $key => $value) {
$sum =$sum+$value;
}
if ($sum >= 500) {?>
<tr>
<td><?php echo $name; ?></td>
<td><?php echo $sum; ?></td>
<?php foreach($isp as $key => $value) { ?>
<td><?php echo $value; ?></td>
<?php } ?>
</tr>
<?php } ?>
<?php } ?>
</table>
So this code fill the table but without respecting the order of the column, and i don't really know how php do this. My question here if anyone can tell me how to do to print the first result in the array in the first ... repetition. and i don't know if i can use the key to choose the array value, but if we can i don't really know how to do it.
If you're simply trying to order your array before cycling through it, PHP has a few options for you:
http://php.net/manual/en/array.sorting.php
Pick the sorting function that works for you, get your array ordered the way you want it, then you can do your foreach() loop without issue.
Php order the array by key. In this case you should use int values as key of the array. For a good solution you can use MACRO:
const DELIVERED = 0; const OPEN = 1; ...
$array[self::DELIVERED]
Order the array before printing. This particular one will order the array by key, from low to high, but have a look at the link Malcom posted to see all the options.
<?php
ksort($this->data);
foreach($this->data as $name => $isp) {
// YOUR CODE HERE
}
?>