5

配列要素ごとに個別のレポートをメールで送信するforeachループを作成しようとしていますが、最後の要素のみを出力し続けます。私はこれをたくさんの方法で書いてみたので、繰り返し始めていると思います。助けてください。

$items = array(1 => 1, 2 => 2, 3 => 3, 7 => 4, 8 => 5,9 => 6, 17 => 8, 18 => 338, 19 => 50, 23 => 52, 11 => 7, 16 => 44,  4 => 11, 5 => 22, 6 => 33);

foreach ($items as $id => $number) {
//I am querying a pervasive database here
$wtdVar = $dbx->getOne('SELECT SUM(sales) FROM table WHERE date1 >= '.$wkstart3.' AND date2 <= '.$today3.' AND category <> \'ABC\' AND number = '.$number);

if (DB::isError($wtdVar)) {
    echo '<div class="error">Error: - '.$wtdVar->getDebugInfo().'</div>';
}

//get the goal for the week so far, use date variables with 2 at the end for postgres queries
$wtdgoal = $db->getOne("SELECT SUM(goal) FROM table2 WHERE id = $id AND gdate BETWEEN '$wkstart2' AND '$today2'");
if (DB::isError($wtdgoal)) {
    echo '<div class="error">Error: - '.$wtdgoal->getDebugInfo().'</div>';}
}

配列には15個のアイテムがあり、レポートの電子メールは問題なく機能しますが、レポートのデータは15倍の最後のアイテムです。より多くのデータがあり、配列要素がある静的変数を使用すると完全に機能しますが、このデータを多くの場所で使用する必要があるため、ループする必要があります。

4

1 に答える 1

0

取得したすべての値を含むように配列を設定する必要があります。

$wtdVars = array();
$wtdgoals = array();

foreach ($items as $id => $number) {
    //I am querying a pervasive database here
    $wtdVar = $dbx->getOne('SELECT SUM(sales) FROM table WHERE date1 >= '.$wkstart3.' AND date2 <= '.$today3.' AND category <> \'ABC\' AND number = '.$number);

    $wtdVars[] = $wtdVar;

    if (DB::isError($wtdVar)) {
        echo '<div class="error">Error: - '.$wtdVar->getDebugInfo().'</div>';
    }

    //get the goal for the week so far, use date variables with 2 at the end for postgres queries
    $wtdgoal = $db->getOne("SELECT SUM(goal) FROM table2 WHERE id = $id AND gdate BETWEEN '$wkstart2' AND '$today2'");

    $wtdgoals[] = $wtdgoal;

    if (DB::isError($wtdgoal)) {
        echo '<div class="error">Error: - '.$wtdgoal->getDebugInfo().'</div>';
    }
}

次に、おそらくレポートに$wtdVaror $wtdgoal- を使用し続けるループがあると思われます。代わりにこれを使用する必要が$wtdVars[$i]あり、レポート内のループの反復ごとに増加するゼロから始まる変数は$wtdgoals[$i]whereです。$i

于 2015-09-02T07:47:43.143 に答える