1

以前にも同様の質問をしましたが、絞り込んで、新しい質問を開始したいと思います。

次のスクリプトは、To と From の日付を DB レコードに挿入します。隔週スキップのオプションを追加したい。以下は非常に近いですが、やりたくない最初の週をスキップします。COUNT 機能を使用して、Count の奇数レコードのみを使用して挿入する方法はありますか?

$week_day = date('w', $curr); # 0 - 6 to access the $week array

if ($week[$week_day]) { # skip if nothings in this day

    $date = date('Y-m-d', $curr);
    $sql->query("SELECT COUNT(schedule_id) FROM $pageDB WHERE doctor_id = $doc_id AND schedule_date = '$date'");

    if (!$sql->result(0)) { # skip if this is already defined
        $date = date('Y-m-d', strtotime("+1 week", $curr));
        $sql->query("INSERT INTO $pageDB (schedule_date, time, doctor_id, location_id) VALUES ('$date', '".$week[$week_day]."', $doc_id, '".$location[$week_day]."')");

    }
}
4

2 に答える 2

0

カウンターを使用して、偶数/奇数であるかどうかを確認できますmodulus- %-if($j%2 != 0)

$week_day = date('w', $curr); # 0 - 6 to access the $week array

$j = 1; // counter

if ($week[$week_day]) { # skip if nothings in this day

    $date = date('Y-m-d', $curr);
    $sql->query("SELECT COUNT(schedule_id) FROM $pageDB WHERE doctor_id = $doc_id AND schedule_date = '$date'");

    if ($j%2 != 0) {
        $date = date('Y-m-d', strtotime("+1 week", $curr));
        $sql->query("INSERT INTO $pageDB (schedule_date, time, doctor_id, location_id) VALUES ('$date', '".$week[$week_day]."', $doc_id, '".$location[$week_day]."')");
         $j++; // increase the counter
    }
}
于 2013-08-15T23:06:29.283 に答える