0

この質問に正しく名前を付ける方法がわからないため、必要に応じて自由に編集してください。

月に1つずつ12個のボックスがあり、各ボックスには次のように表示されます。1-データベースからの情報に応じて、「販売済み」または「利用可能」などのメッセージ。2-月と年の名前。月が現在の月よりも古い場合、年は1つ増えます。

データベースからの情報には、|で区切られたいくつかの値があります。シンボル、チェックする適切なものはlasのものであり、$row['custom']の値の例は次のとおりです。夕食| 新しいポート| 2012年8月

私の問題は、各ボックスを「ステータス」で更新する必要があり、現在のスクリプトでは、データベースのlasエントリのみがボックスの更新に使用されるため、2つ以上のボックスを表示する必要があることがわかっている場合メッセージ「販売済み」、最新のもののみが更新されます。

次のスクリプトを変更して、ボックスを1つずつ更新するにはどうすればよいですか?問題は、データベースまたは他の何かをクエリする方法ですか?

ヘルプやアドバイスを事前に感謝します。

物事を短くするために、12か月のうち2か月だけコーディングする

<?php
$month = date("m"); // Current month
$year = date("Y"); // Current year
$next = strtotime(date("d-m-Y", strtotime($now)) . "+1 year");
$nextyear = date("Y", $next); // Next year

// Check if this month is gone or not, if gone replace current year with next year
$january = "01";
$february = "02";

if ($month > $january) { 
  $jan = 'January - ' . $nextyear; } else { $jan = 'January - ' . $year;}

if ($month > $february) { 
  $feb = 'February - ' . $nextyear; } else { $feb = 'January - ' . $year;}

//Get info from Database

$query = "SELECT `custom` FROM `payments` WHERE `status` = 'Pending' OR `status` = 'Completed'"; 
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
  $check_custom = explode(" | ", $row['custom']);
  $month_sold = $check_custom[3];
}

// Check if month is sold or not
if ($month_sold == $jan) { $jan_status = 'Sold';} else { $jan_status = 'Available';}
if ($month_sold == $feb) { $feb_status = 'Sold';} else { $feb_status = 'Available';}

//Output the months and their status
?>

<div class="month">
  <div class="mname"><?php echo $jan;?></div>
  <div class="<?php echo $jan_status;?>">
    <?php echo $jan_status;?>
  </div>
  <?php if($jan_status == 'Available') { 
      echo 'This month is ' . $jan_status . '.';
    } else { 
      echo 'This month has been ' . $jan_status . '.';} ?>
</div>

<div class="month">
  <div class="mname"><?php echo $feb;?></div>
  <div class="<?php echo $feb_status;?>">
    <?php echo $feb_status;?>
  </div>
  <?php if($feb_status == 'Available') { 
      echo 'This month is ' . $feb_status . '.';
    } else { 
      echo 'This month has been ' . $feb_status . '.';} ?>
</div>
4

3 に答える 3

4

whileの閉じ括弧を置き忘れて、 `$ jan_status='Available'をwhileループの内側からそのすぐ上に移動しました。これが変更されたコードです

    <?php
    $month = date("m"); // Current month
    $year = date("Y"); // Current year
    $next = strtotime(date("d-m-Y", strtotime($now)) . "+1 year");
    $nextyear = date("Y", $next); // Next year

    // Check if this month is gone or not, if gone replace current year with next year
    $january = "01";
    $february = "02";

    if ($month > $january) { 
      $jan = 'January - ' . $nextyear; } else { $jan = 'January - ' . $year;}

    if ($month > $february) { 
      $feb = 'February - ' . $nextyear; } else { $feb = 'January - ' . $year;}

    //Get info from Database

    $query = "SELECT `custom` FROM `payments` WHERE `status` = 'Pending' OR `status` = 'Completed'"; 
    $result = mysql_query($query) or die(mysql_error());
$jan_status = 'Available';
$feb_status = 'Available';
    while($row = mysql_fetch_array($result)) {
      $check_custom = explode(" | ", $row['custom']);
      $month_sold = $check_custom[3];


    // Check if month is sold or not
    if ($month_sold == $jan) { $jan_status = 'Sold';} 
    if ($month_sold == $feb) { $feb_status = 'Sold';}
    }//th closing bracket should be here;
    //Output the months and their status
    ?>

    <div class="month">
      <div class="mname"><?php echo $jan;?></div>
      <div class="<?php echo $jan_status;?>">
        <?php echo $jan_status;?>
      </div>
      <?php if($jan_status == 'Available') { 
          echo 'This month is ' . $jan_status . '.';
        } else { 
          echo 'This month has been ' . $jan_status . '.';} ?>
    </div>

    <div class="month">
      <div class="mname"><?php echo $feb;?></div>
      <div class="<?php echo $feb_status;?>">
        <?php echo $feb_status;?>
      </div>
      <?php if($feb_status == 'Available') { 
          echo 'This month is ' . $feb_status . '.';
        } else { 
          echo 'This month has been ' . $feb_status . '.';} ?>
    </div>
于 2012-05-08T18:39:42.147 に答える
2

を実行するたび$row = mysql_fetch_array()に、$rowの内容がデータベースの次の行に置き換えられます。whileループを見てください。$check_customと$month_soldに値が割り当てられますが、新しい値で上書きする前は何もしません。データベース出力を解析し、ループの特定の月の情報を生成するために、すべてのコードを移動する必要があります。次に、次の月に進む前に、その月に表示したい情報を出力または保存します。

コードをよりシンプルで保守しやすくするためにできることはたくさんあります。たとえば、月ごとに個別の変数セットと個別の出力コードを作成するのではなく、月の配列を作成してそれを繰り返します。また、現在カスタム列で行っているのは、データベーステーブルのフィールドを使用して別の小さなテーブルを格納することです。これにより、多くの問題が発生します。そのデータを複数の列に分割した場合、適切な月と年のクエリを実行できます。

于 2012-05-08T18:38:14.817 に答える
1

ステータスチェックをループに移動して、データベースのすべての行で動作するようにします。それ以外の場合は、の最後の呼び出しによって返された行を参照するだけになりますmysql_fetch_arrayswitchただし、読みやすくするためにステートメントを使用することをお勧めします。

while($row = mysql_fetch_array($result)) {
    $check_custom = explode(" | ", $row['custom']);
    $month_sold = $check_custom[3];

    // Check if month is sold or not
    if ($month_sold == $jan) { $jan_status = 'Sold';}
    else { $jan_status = 'Available';}

    if ($month_sold == $feb) { $feb_status = 'Sold';}
    else { $feb_status = 'Available';}
}
于 2012-05-08T18:39:17.957 に答える