2017-08-07 58 views
0

我有兩個數組。我想根據$daterange的計數顯示tr標籤,在此範圍內,我需要檢查date值和second arraydate值。基於第一個數組顯示tr標籤並與PHP中的第二個數組進行比較

第一陣列:

$daterange = ['01/10/2017','02/10/2017','03/10/2017','04/10/2017','05/10/2017']; 

第二陣列:

$job = [0 => ['id' =>1,'date' => '03/10/2017'], 
     1 => ['id' =>2,'date' => '12/10/2017'], 
     2 => ['id' =>3,'date' => '14/10/2017'], 
     3 => ['id' =>4,'date' => '13/10/2017'], 
     4 => ['id' =>5,'date' => '03/10/2017'], 
     5 => ['id' =>6,'date' => '04/10/2017'], 
     6 => ['id' =>7,'date' => '05/10/2017'], 
     7 => ['id' =>8,'date' => '01/10/2017'] 
    ]; 

HTML代碼:顯示

<table> 
<?php foreach($daterange as $key=>$day) 
{ 
?> 
<tr> 
<td> 

    <?php foreach($job as $jdata){ 
    if(($day->format('Y-m-d') == ($jdata->date)) { 
    ?> 
    <input type="radio" checked class="radio-check" name="date" value=""> 
    <?php 
    } else { 
    ?> 
    <input type="radio" class="radio-check" name="date" value=""> 
    <?php 
    } 
    ?> 
</td> 
</tr> 
<?php 
} 
?> 
</table> 

但是tr標籤基於第二陣列計數8次。

如何顯示tr 5次,這是第一個數組的計數,並將第二個數組與內部日期進行比較?

+0

從這個代碼,我想你實際上是打印40個非檢查輸入(5'tr' 8'每個輸入「)。 – nanocv

+0

@nanocv:是啊我該如何避免這種情況? – 06011991

+0

你的第二個數組缺少一些東西,將它複製並粘貼到你將看到的編輯器中。 –

回答

1

你只需要完成這個任務一個循環,當你只修改行的單個屬性你不應該重複整個線路(作爲乾燥問題)。

array_column()將充分準備$job數據。

這是我的建議:(Demo

$daterange = ['01/10/2017','02/10/2017','03/10/2017','04/10/2017','05/10/2017']; 
$job = [0 => ['id' =>1,'date' => '03/10/2017'], 
     1 => ['id' =>2,'date' => '12/10/2017'], 
     2 => ['id' =>3,'date' => '14/10/2017'], 
     3 => ['id' =>4,'date' => '13/10/2017'], 
     4 => ['id' =>5,'date' => '03/10/2017'], 
     5 => ['id' =>6,'date' => '04/10/2017'], 
     6 => ['id' =>7,'date' => '05/10/2017'], 
     7 => ['id' =>8,'date' => '01/10/2017'] 
    ]; 
$job_dates=array_column($job,'date'); // generate 1-dimensional array of dates 

echo '<table>'; 
    foreach($daterange as $date){ 
     echo "<tr><td><input type=\"radio\" class=\"radio-check\" name=\"date\" value=\"$date\"",(in_array($date,$job_dates)?' checked':''),'></td></tr>'; 
    } 
echo "</table>"; 

/* or write it over several lines like this: 
    echo '<table>'; 
    foreach($daterange as $date){ 
     echo '<tr>'; 
      echo '<td>'; 
       echo "<input type=\"radio\" class=\"radio-check\" name=\"date\" value=\"$date\""; 
       if (in_array($date,$job_dates)){ 
        echo ' checked'; 
       } 
       echo '>'; 
      echo '</td>'; 
     echo '</tr>'; 
    } 
    echo "</table>"; 
*/ 

輸出:

<table> 
    <tr><td><input type="radio" class="radio-check" name="date" value="01/10/2017" checked></td></tr> 
    <tr><td><input type="radio" class="radio-check" name="date" value="02/10/2017"></td></tr> 
    <tr><td><input type="radio" class="radio-check" name="date" value="03/10/2017" checked></td></tr> 
    <tr><td><input type="radio" class="radio-check" name="date" value="04/10/2017" checked></td></tr> 
    <tr><td><input type="radio" class="radio-check" name="date" value="05/10/2017" checked></td></tr> 
</table> 
-1
$job = [0 => ['id' =>1,'date' => '03/10/2017'/* ' here*/ ], 
    1 => ['id' =>2,'date' => '12/10/2017'], 
    2 => ['id' =>3,'date' => '14/10/2017'], 
    3 => ['id' =>4,'date' => '13/10/2017'], 
    4 => ['id' =>5,'date' => '03/10/2017'], 
    5 => ['id' =>6,'date' => '04/10/2017'], 
    6 => ['id' =>7,'date' => '05/10/2017'], 
    7 => ['id' =>8,'date' => '01/10/2017'] 
] 
+1

你能解釋一下這可能如何解決OP的問題。它修復了代碼中的一個錯誤,並未顯示如何解決該問題。 –

+0

如果你不把'它可能會導致錯誤:/ –

-1

試試這個作爲你的for循環。

<table> 
    <?php 
    foreach ($job as $key => $jdata) { 
     if (in_array($jdata['date'], $daterange)) { 
      ?> 
      <tr> 
       <td> 
        <input type="radio" checked class="radio-check" name="date" value=""> 
       </td> 
      </tr> 
      <?php 
     } 
    } 
    ?> 
</table> 
1

所以我創建了一個功能searchForDate如果日期第二陣列中存在與否,嘗試下面的代碼,在這裏,將檢查是demo

<?php 
$daterange = ['01/10/2017','02/10/2017','03/10/2017','04/10/2017','05/10/2017']; 
<table> 
    <?php foreach($daterange as $key=>$day) 
{ 
?> 
<tr> 
    <td> 
     <?php foreach($daterange as $key=>$day){ 
    if(searchForId($day)) { 
     ?> 
     <input type="radio" checked class="radio-check" name="date" value=""> 
     <?php } else { 
     ?> 
     <input type="radio" class="radio-check" name="date" value=""> 
     <?php } ?> 
    </td> 
</tr> 
<?php } ?> 
</table> 
<?php 
function searchForId($day) { 
    $job = [ 
     0 => ['id' =>1,'date' => '03/10/2017' ], 
     1 => ['id' =>2,'date' => '12/10/2017'], 
     2 => ['id' =>3,'date' => '14/10/2017'], 
     3 => ['id' =>4,'date' => '13/10/2017'], 
     4 => ['id' =>5,'date' => '03/10/2017'], 
     5 => ['id' =>6,'date' => '04/10/2017'], 
     6 => ['id' =>7,'date' => '05/10/2017'], 
     7 => ['id' =>8,'date' => '01/10/2017'] 
    ]; 
    foreach ($job as $key => $val) { 
     if ($val['date'] === $day) { 
      return $key; 
     } 
    } 
    return null; 
} 
?> 
+0

我該如何做到這一點? – 06011991

+0

我已更新完整的代碼。希望這可以幫助你。 –

1

這裏有我的解決方案,非常類似於你的。

我試過了,它工作正常。

<?php 


$daterange = ['01/10/2017', '02/10/2017', '03/10/2017', '04/10/2017', '05/10/2017']; 

$job = [0 => ['id' => 1, 'date' => '03/10/2017'], 
    1 => ['id' => 2, 'date' => '12/10/2017'], 
    2 => ['id' => 3, 'date' => '14/10/2017'], 
    3 => ['id' => 4, 'date' => '13/10/2017'], 
    4 => ['id' => 5, 'date' => '03/10/2017'], 
    5 => ['id' => 6, 'date' => '04/10/2017'], 
    6 => ['id' => 7, 'date' => '05/10/2017'], 
    7 => ['id' => 8, 'date' => '01/10/2017'] 
] 

?> 

<table> 
    <?php 
    foreach ($daterange as $day) { 
     ?> 
     <tr> 
      <td> 
       <?php 

       $i = 0; 
       $numJobs = count($job); 
       $dateFound = 0; 

       while ($i < $numJobs && !$dateFound) { 
        if ($job[$i]['date'] == $day) { 
         $dateFound = 1; 
        } 
        $i++; 
       } 

       if ($dateFound) { 
        ?> 
        <input type="radio" checked class="radio-check" name="date" value=""> 
        <?php 
       } else { 
        ?> 
        <input type="radio" class="radio-check" name="date" value=""> 
        <?php 
       } 
       ?> 
      </td> 
     </tr> 
     <?php 
    } 
    ?> 
</table> 

$daterange每個元素我尋找該日期內$job陣列與同時循環內部。如果我找到它,它會停止搜索並顯示選中的輸入。否則,如果它遍歷所有數組,並且沒有找到該日期,則它顯示未檢查的輸入。

(您可以複製並粘貼到您的代碼)

相關問題