2

次のように、開始日(契約)、ランダムなドキュメント、毎月の請求書と領収書など、さまざまなエントリを含むテーブルがあります。

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

ドキュメント

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? はい、不足しているドキュメントを表示します。どこで MONTHLY? はいです。

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

いくつかのコードを書きましたが、毎月のループを含めて請求書と領収書を表示する方法がわかりません。

クエリ

// 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'];

その間

<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>
4

1 に答える 1

1

最初に、最初のテーブルのデータベースからデータをロードしているように見えるため、サンプル データに少し混乱していることを指摘する必要がありますが、この例は、別の場所に保存されているドキュメントのリストがあることを意味します。各ジョブまたはプロジェクト。

私だったら、最終テーブルを表すオブジェクトまたは配列を作成します。そのオブジェクトは、利用可能なデータに基づいて入力できます。あなたが既に使用しているように見えるデータ構造を考えると、オブジェクト指向プログラミングのアプローチを使用したくないと思います。

最初に、ドキュメント タイプと月次設定に一致する元のテーブル情報を格納する配列またはオブジェクトを作成します。データ形式を選択したら、異なる設定グループごとにデータベースからデータ形式をロードできます。あなたの例で示した設定を使用します。

個人的には、ドキュメント タイプ ID をインデックス キーとして使用し、ブール値を使用して、次のように Yes (true) と No (false) を表します。

$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";
            }
        }
    }
}

これで、データベースにレコードがあるドキュメントごとに 1 つのドキュメント レコードを含む、日付ごとに整理された配列ができました (ただし、日付ごとに各タイプのレコードは 1 つだけです...これを変更する必要がある場合は、データを変更するだけです)ニーズに合わせて配列の構造を変更するか、より自然に考えをマッピングするのに役立つオブジェクト指向アプローチを使用します)。Monthly = true ( YES ) の項目については、何かが期待されていたが見つからなかったことを示す "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 では、すべてのプログラマーがプログラミング コードを検討する前にデータの形状を検討する必要があり、多くの問題が解決されるからです。

于 2013-05-06T19:53:50.297 に答える