2013-05-05 34 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迫使每位程序员在考虑编程代码之前考虑数据的形状,并且它解决了许多问题。