2013-05-05 69 views
2

我有它不同的條目,如開始日期(合同),一些隨機的文件和月結單和收據一張桌子,就像這樣:生成報告與while循環

DOCUMENT TYPES 
ID TYPE  CHECK?  MONTHLY? 
1 Contract Yes  No 
2 Documents No   No 
3 Policy  Yes  No 
4 Order  No   No 
5 Invoice  Yes  Yes 
6 Receipt  Yes  Yes 

DOCUMENTS

ID TYPE DATE 
1 1 01/01/2013 
2 2 01/01/2013 
3 3 01/01/2013 
4 5 01/02/2013 
5 6 01/02/2013 
6 4 14/02/2013 
7 5 01/03/2013 
8 6 01/03/2013 


TODAY 05/05/2013 

我的目標是產生如下所示的輸出。 顯示所有存在的文件,其中CHECK?是的,每月在哪裏顯示丟失的文件?是是的。

01 Contract 01/01/2013 OK 
02 Documents 01/01/2013 OK 
03 Policy   01/01/2013 OK 
04 Invoice   01/02/2013 OK 
05 Receipt   01/02/2013 OK 
06 Invoice   01/03/2013 OK 
07 Receipt   01/03/2013 OK 
08 Invoice   01/04/2013 Missing 
09 Receipt   01/04/2013 Missing 
10 Invoice   01/05/2013 Missing 
11 Receipt   01/05/2013 Missing 

我寫了一些代碼,但我不明白如何包括每月循環顯示發票和收據。

QUERY

// Get type 
$query = "SELECT * FROM doc_type 
      WHERE check='1'"; 
$result = mysql_query($query) or die(mysql_error()); 
$num = mysql_numrows($result); 

// Get docs 
$check = array(); 
$query2 = mysql_query("SELECT document_type_id FROM documents"); 
while ($row = mysql_fetch_assoc($query2)) { 
$check[] = $row['document_type_id']; 

WHILE

<tbody> 
<?php 
    $i=0; 
     while ($i < $num) { 
     $document_type_id = mysql_result($result,$i,"document_type_id"); 
     $document_type = mysql_result($result,$i,"document_type"); 
    ?> 
    <tr class="grade"> 

    <td><?php echo $document_type_id; ?></td> 
    <td><?php echo $document_type; ?></td> 
    <td> 
    <?php 
     if (in_array($document_type_id, $check)) { 
     echo "<p style='color:green'>Ok</p>"; 
     } else { 
     echo "<p style='color:red'>Miss</p>"; 
     } 
    ?> 
    </td> 
    </tr> 
    <?php 
     $i++; 
     } 
     ?>      
</tbody> 
+0

我近視。建議的校正與接受的答案的拼寫相匹配。 – Matsmath 2016-05-05 19:26:44

回答

1

首先,我必須指出,我稍微示例數據迷惑,因爲它看起來好像你是在一個數據庫中裝載數據第一個表格,但這個例子意味着每個工作或項目都有一份保存在別處的文檔列表。

如果是我,我會創建一個對象或代表最終表的數組。您可以根據可用數據填充該對象。鑑於你似乎已經使用的數據結構,我會假設你不想使用面向對象的編程方法。

首先,創建一個數組或對象,存儲與文檔類型和每月設置相匹配的原始表信息。一旦你選擇了數據格式,你可以從你的數據庫中爲每個不同的設置組裝載數據格式。我將使用您在示例中顯示的設置。

就個人而言,我會使用的文檔類型ID作爲索引鍵和使用布爾值來表示是(真)和無(假)是這樣的:

$doc_requirements = array(
    1 => array("name" => "Contract", "check" => true, "monthly" => false), 
    2 => array("name" => "Documents", "check" => false, "monthly" => false), 
    3 => array("name" => "Policy", "check"=>true, "monthly"=>false), 
    4 => array("name" => "Order", "check"=>false, "monthly"=>false), 
    5 => array("name" => "Invoice", "check"=>true, "monthly"=>false), 
    6 => array("name" => "Receipt", "check"=>true, "monthly"=>false) 
); 

使用數據庫來存儲多達根據需要添加這些表格,並在查看文檔列表之前加載它們。我想創建一個表示輸出表的數組。按日期索引,以便確定是否缺少文檔。您的數據意味着每個日期,我們就可以有一個以上的文件,但不是同一個類型的多個文檔,所以你可能能夠使用這樣的事情:

/* pseudo_code */ $temp_table = array( 
     [date] => array( 
      [doc_type_name] => array(
       /* I assume that the document id is actually just the line number 
        on the table, so I will leave the id out of this, but if it is 
        not just the line on the table, add this field to the array: 
         "id" => [id], */ 
       "status" => [status]) 
      ), 
      [doc_type_name] => array(
       "status" => [status]) 
      ), 
      ... 
     ), 
     [date2] => array( ...), 
     .... 
); 

加載此數組你知道從您的文檔數組的文件:

(注意:您使用的是mysql功能,這是目前棄用,因此,你應該看看使用替代msyqli功能)

$sql = #### /* SQL query string for your list of documents that are not missing */ 
$result = mysql_query($sql) or die(mysql_error()); 
if($result){ 
    while($document = mysql_fetch_assoc($result)){ 
     $date = $document[ "date" ]; 
     $doc_type_id = $document[ "type_id" ]; 
     $doc_type_name = $doc_requirements[ $doc_type_id ]["name"]; 
     $temp_table[ $date ][ $doc_type_name ]["status"]= "OK" 
     /* we have the document, therefore, we know it is okay, 
      we can set that value immediately */ 
    } 
} 
$start_date=#### /* set start date of contract */ 
$end_date=##### /*set end date of contract */ 
foreach($doc_requirements as $requirement){ 
    if($requirement["monthly"] == true){ 
     for($cur_date = $start_date; $cur_date <= $end_date; $cur_date=####){ 
       /*increment the date by whatever function you choose, 
        I recommend using a DateTime object 
        for your dates and incrementing by using the add() method */ 
      $current_doc_name = $requirement[ "name"]; 
      if(!isset($temp_table[$cur_date][ $current_doc_name ])){ 
       /* if the array value is not set, 
        then a document of the type_name 
        and current date specified does not exist, 
        because the requirement for "monthly" == true, 
        we know that we should have it, so we will 
        set the status to "Missing" */ 
       $temp_table[$cur_date][$current_doc_name]["status"] = "Missing"; 
      } 
     } 
    } 
} 

現在我們有一個數組組織日期,包含我們在數據庫中記錄的每個文檔的一個文檔記錄(但每個日期不超過每種類型的一個...如果您需要更改此設置,只需更改數組的數據結構以滿足您的需求,或者使用面向對象的方法幫助更自然地映射出您的想法)。對於任何每月=真的項目(),我們爲它創建了一個「缺失」存根,表示有預期的東西但沒有找到。有了這個數據數組,我們可以遍歷它併產生輸出。

我上面說你的文檔ID似乎只是在輸出表中的行數已經注意到了,所以我將代表在這裏以同樣的方式:

$doc_id = 1; 
foreach($temp_table as $cur_date){ 
    foreach($cur_date as $doc_name){ 
     $doc_id_string = your_func_format_id($doc_id); 
     /* use whatever function you like to make your doc_id two digits. 
      Perhaps printf() would be useful */ 
     $color_code = "style='color:green'"; 
     if($doc_name["status"]=="Missing") $color_code = "style='color:red'"; 
     echo "<tr class='grade'><td>$doc_id_string</td><td>$doc_name</td><td>$cur_date</td><td><p $color_code>{$doc_name["status"]}</p></td>"; 
    } 
} 

我已經瞭解到,使用正確的數據結構使一切變得更加簡單。我試圖使用反映您示例代碼的數據結構。我強烈建議使用面向對象編程(OOP)技術來實現您的設計,因爲OOP迫使每位程序員在考慮編程代碼之前考慮數據的形狀,並且它解決了許多問題。