0
0

More than 1 year has passed since last update.

過去の自分のコードを修正してみる - PHP カレンダー編②

Last updated at Posted at 2022-08-24

前回のおさらい

前回は、これを


<?php
// $t…その段のなかでいくつ目の月なのか的な変数だと思う[後付けコメント]
if ($t == 0) {
  print "<table border='0' width='100%' align='center' class='calender_gyou'>\n<tr>\n<td>\n<div style='float:left;margin-right:20px;margin-bottom:8px;'>";
  $t++;
}

こうした


<?php
if ($t == 0) {
?>
  <table class="calender_gyou first-table">
    <tr>
      <td>
        <div class="div-of-first-table">
<?php
  $t++;
}


.first-table {
  border-style: none;
  width:        100%;
  text-align:   center; 
}
.div-of-first-table {
   float:       left;
   margin-right:20px;
   margin-bottom:8px;
}

範囲を広げる


<?php
if ($t == 0) {
  print "<table border='0' width='100%' align='center' class='calender_gyou'>\n<tr>\n<td>\n<div style='float:left;margin-right:20px;margin-bottom:8px;'>";
  $t++;
} elseif (($t == 1) or ($t == 2)) {
  print "<div style='float:left;margin-right:20px;margin-bottom:8px;'>";
  $t++;
} else {
  print "<div style='float:left;margin-right:20px;margin-bottom:8px;'>";
  $t = 0; //次の段へ
}

前回同様に、簡単に整えてみる


<?php
if ($t == 0) {
?>
  <table class="calender_gyou first-table">
    <tr>
      <td>
        <div class="div-of-first-table">
<?php
  $t++;
} elseif (($t == 1) or ($t == 2)) {
?>
  <div style="div-of-month">
<?php
  $t++;
} else {
?>
  <div style="div-of-month">
<?php
  $t = 0;
}


.first-table {
  border-style:  none;
  width:         100%;
  text-align:    center; 
}
.div-of-first-table {
   float:        left;
   margin-right: 20px;
   margin-bottom:8px;
}
.div-of-month {
   float:        right;
   margin-right: 20px;
   margin-bottom:8px;
}

いや、同じ処理や同じ記述がとっちらかっててモヤモヤする
あと昨日は疲れててスルーしてたけど緩やかな比較なのもモヤモヤする

処理をまとめる


if ($t === 0) :
?>
  <table class="calender_gyou first-table">
    <tr>
      <td>
<?php
endif;
?>
  <div class="<?php echo $t === 0 ? 'div-of-first-table' : 'div-of-month' ?>">
<?php
// 段の最後まで印字したら$tを0に戻して次の段にする…んだけど、
// 三項演算子で書いてみたら思ったより伝わらなさそうだったので普通にif文にする
// $t = $t > 2 ?  0 : $t++;
if ($t > 2) {
  $t = 0;
} else {
  $t++;
}
?>


.first-table {
  border-style:  none;
  width:         100%;
  text-align:    center; 
}
.div-of-first-table {
   float:        left;
   margin-right: 20px;
   margin-bottom:8px;
}
.div-of-month {
   float:        right;
   margin-right: 20px;
   margin-bottom:8px;
}

次回はもう少し観測範囲を広げてみます
cssのところは、現状がいいのか分離してクラス増やすのがいいのか理解しきれていないので空き時間にでも確認しておきます

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0