2013-02-15 91 views
0

我遇到問題,找不到任何解決方案。 我有一個數據庫,從那裏我選擇了很多信息,並計算這些數據。在PHP中使用格式化數字對數據表進行排序

$table->td(    
      array($row->mailing_id, "width=\"20\" style=\" border-bottom-width: 1px; border-bottom-style: solid;\""), 
      array($row->mailing_unique_id, "width=\"20\" style=\" border-bottom-width: 1px; border-bottom-style: solid;\""), 
      array($row->mailing_name, "width=\"300\" style=\" border-bottom-width: 1px; border-bottom-style: solid;\""), 
      array($row->campaign_id, "width=\"20\" style=\" border-bottom-width: 1px; border-bottom-style: solid;\""), 
      array($row->segment_name, "width=\"100\" style=\" border-bottom-width: 1px; border-bottom-style: solid;\""), 
      array($row->total_sent, "width=\"20\" style=\" border-bottom-width: 1px; border-bottom-style: solid;\""), 
      array(@$viewSum[$row->mailing_unique_id], "width=\"20\" style=\" border-bottom-width: 1px; border-bottom-style: solid;\""), 
      array(@number_format((@$viewSum[$row->mailing_unique_id] * 100)/$row->total_sent, 2, '.', '.').'%', "width=\"20\" style=\" border-bottom-width: 1px; border-bottom-style: solid;\""), 
      array(number_format(@$leadssum[$row->mailing_unique_id],0, '.', '.'), "width=\"20\" style=\" border-bottom-width: 1px; border-bottom-style: solid;\""), 
      array(@number_format((@$leadssum[$row->mailing_unique_id]*100)/$row->total_sent,2, '.', '.').'%', "width=\"20\" style=\" border-bottom-width: 1px; border-bottom-style: solid;\""), 
      array(@number_format((@$leadssum[$row->mailing_unique_id] * 100)/@$viewSum[$row->mailing_unique_id], 2, '.', '.').'%', "width=\"20\" style=\" border-bottom-width: 1px; border-bottom-style: solid;\""), 
      array(number_format(@$crossviewSum[$row->mailing_unique_id],0, '.', '.'), "width=\"20\" style=\" border-bottom-width: 1px; border-bottom-style: solid;\""), 
      array(number_format(@$crossleadsSum[$row->mailing_unique_id],0, '.', '.'), "width=\"20\" style=\" border-bottom-width: 1px; border-bottom-style: solid;\""), 
      array(@number_format((@$crossleadsSum[$row->mailing_unique_id] * 100)/@$crossviewSum[$row->mailing_unique_id], 2, '.', '.').'%', "width=\"20\" style=\" border-bottom-width: 1px; border-bottom-style: solid;\""), 
      array(@number_format(((@$leadssum[$row->mailing_unique_id][email protected]$crossleadsSum[$row->mailing_unique_id])/$row->total_sent)*100, 2, '.', '.').'%', "width=\"20\" style=\" border-bottom-width: 1px; border-bottom-style: solid;\"") 
      ); 

但是當我在DataTables中顯示所有數據時,排序是不對的。表認爲1.800小於999,但它是成千上萬的分離器。數據會是這樣的:

999 
888 
5.656 
4.655 
322 
2.333 
122 

編輯-----------------

這是主要的查詢:

$query = sprintf(" 
      SELECT stat_mailings.mailing_id, stat_mailings.cat_id, stat_mailings.mailing_name, stat_mailings.mailing_unique_id, stat_mailings.segment_id, 
      stat_mailings.campaign_id, stat_mailings.landing_page, stat_mailings.total_sent, pecumail_segments.segment_name, categories.category FROM `stat_mailings` 
      JOIN pecumail_segments ON stat_mailings.segment_id=pecumail_segments.segment_id 
      JOIN categories ON stat_mailings.cat_id=categories.id  

這是我的代碼,在那裏我打印的所有數據:

$table = new datatable("100%", "border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"margin-left: auto;margin-right: auto;margin-left: -135px;\""); 

    $table->th("Mailing ID", "Unique id", "Mailing", "UTM campaign", "Segment", "Vezonden", "Views", "CTR", "Leads", "STL","Conversie", "Crossviews", "Crossleads","Crossconv","STLa"); 


    $analyzedata = getAnalyze(); 
    $leads = getLeads(); 
    $views = getViews(); 
    $crossviews = getCrossViews(); 
    $crossleads = getCrossLeads(); 



     while ($row1 = $leads->fetch_object()) { 
     $leadsData = count($row1->pema_mid);   
     @$leadssum[$row1->pema_mid] += $leadsData; 
     } 

     while ($row2 = $views->fetch_object()) { 
     $countedViews = count($row2->pema_mid);   
     @$viewSum[$row2->pema_mid] += $countedViews; 
     } 

     while ($row3 = $crossviews->fetch_object()) { 
     $countedCrossViews = count($row3->pema_mid);   
     @$crossviewSum[$row3->pema_mid] += $countedCrossViews; 
     } 

     while ($row4 = $crossleads->fetch_object()) { 
     $countedCrossLeads = count($row4->pema_mid);   
     @$crossleadsSum[$row4->pema_mid] += $countedCrossLeads; 
     } 


     while($row = $analyzedata->fetch_object()){ 


      $totalSent = number_format($row->total_sent, 0, '.', '.'); 

     $table->td(    
       array($row->mailing_id, "width=\"20\" style=\" border-bottom-width: 1px; border-bottom-style: solid;\""), 
       array($row->mailing_unique_id, "width=\"20\" style=\" border-bottom-width: 1px; border-bottom-style: solid;\""), 
       array($row->mailing_name, "width=\"300\" style=\" border-bottom-width: 1px; border-bottom-style: solid;\""), 
       array($row->campaign_id, "width=\"20\" style=\" border-bottom-width: 1px; border-bottom-style: solid;\""), 
       array($row->segment_name, "width=\"100\" style=\" border-bottom-width: 1px; border-bottom-style: solid;\""), 
       array($totalSent, "width=\"20\" style=\" border-bottom-width: 1px; border-bottom-style: solid;\""), 
       array(number_format(@$viewSum[$row->mailing_unique_id],0, '.', '.'), "width=\"20\" style=\" border-bottom-width: 1px; border-bottom-style: solid;\""), 
       array(@number_format((@$viewSum[$row->mailing_unique_id] * 100)/$row->total_sent, 2, '.', '.').'%', "width=\"20\" style=\" border-bottom-width: 1px; border-bottom-style: solid;\""), 
       array(number_format(@$leadssum[$row->mailing_unique_id],0, '.', '.'), "width=\"20\" style=\" border-bottom-width: 1px; border-bottom-style: solid;\""), 
       array(@number_format((@$leadssum[$row->mailing_unique_id]*100)/$row->total_sent,2, '.', '.').'%', "width=\"20\" style=\" border-bottom-width: 1px; border-bottom-style: solid;\""), 
       array(@number_format((@$leadssum[$row->mailing_unique_id] * 100)/@$viewSum[$row->mailing_unique_id], 2, '.', '.').'%', "width=\"20\" style=\" border-bottom-width: 1px; border-bottom-style: solid;\""), 
       array(number_format(@$crossviewSum[$row->mailing_unique_id],0, '.', '.'), "width=\"20\" style=\" border-bottom-width: 1px; border-bottom-style: solid;\""), 
       array(number_format(@$crossleadsSum[$row->mailing_unique_id],0, '.', '.'), "width=\"20\" style=\" border-bottom-width: 1px; border-bottom-style: solid;\""), 
       array(@number_format((@$crossleadsSum[$row->mailing_unique_id] * 100)/@$crossviewSum[$row->mailing_unique_id], 2, '.', '.').'%', "width=\"20\" style=\" border-bottom-width: 1px; border-bottom-style: solid;\""), 
       array(@number_format(((@$leadssum[$row->mailing_unique_id][email protected]$crossleadsSum[$row->mailing_unique_id])/$row->total_sent)*100, 2, '.', '.').'%', "width=\"20\" style=\" border-bottom-width: 1px; border-bottom-style: solid;\"") 
       ); 

} 


    echo $html = $table->getHTML(); 

,這是我的DataTable腳本

$(document).ready(function() { 
$('#example').dataTable({ 
    "aoColumns": [ 
     null, 
     null, 
     null, 
     { "sType": "numeric-comma" }, 
     { "sType": "numeric-comma" }, 
     { "sType": "string" }, 
     { "sType": "formated-num" }, 
     { "sType": "percent" }, 
     { "sType": "numeric-comma" }, 
     { "sType": "percent" }, 
     { "sType": "percent" }, 
     { "sType": "numeric-comma" }, 
     { "sType": "percent" }, 
     { "sType": "percent" }, 
     null 
    ] 
}); 

});

+0

您必須將實際值存儲在單獨的字段中才能排序論文 – djleop 2013-02-15 09:12:27

+0

爲什麼要隱藏所有錯誤?這是一個糟糕的設計。 – 2013-02-15 09:15:54

+0

實際上是5.656數據庫中的值?還是你把它轉換成那個?換句話說,表中的值是什麼? – djjjuk 2013-02-15 09:20:32

回答

1

你將不得不從數據庫中排序。看看你的代碼,

while ($row1 = $leads->fetch_object()) { } 

看起來像它一次抓取表1中的一個對象或行。如果這樣做,那麼以後就無法對其進行分類。你要麼必須從數據庫中對它進行排序,要麼從具有fetch_object()函數的類中對它進行排序 - 據推測這將循環遍歷結果並將它們創建爲對象?如果這個類碰巧是一個Iterator實現,那麼您只能將它從數據庫中排序爲一個選項。

唯一你可以在PHP中排序的方法是將值作爲你的鍵並按鍵對數組進行排序 - 但顯然,如果你有兩個或更多的值,它們會覆蓋每個值其他。

相關問題