將空函數與 Foreach 迴圈結合使用
P粉988025835
P粉988025835 2023-09-07 12:44:13
0
1
513

我想在資料庫中沒有項目時輸出一條訊息。這 當資料庫沒有資料時,程式碼不會列印語句。

This is my code,
<?php foreach($latest_tenders as $tenders):
if(!empty($tenders)){ 
$time = strtotime($tenders['post_date']);
                      $month=date('m', $time);
                                switch($month){
                                   case 1:
                                    echo "January "; break;
                                      .
                                      .
                                      .
                                    case 12:
                                     echo "December "; break;
                                   default:
                                    echo "January ";
                            }
echo date('d', $time).', ';
echo date('Y', $time).'&nbsp';
echo date('h', $time).':'; echo date('i',     $time).' |&nbsp';?>
     <i class="fas fa-user"></i> <?php echo $tenders['author'];?></p> 
       </div><br/>
     <?php 
         }
            else{?>
      <p style=" font-size:13px; word-spacing: 5px;color:tomato;">
          Tender Name: No advertised Tenders, Check again later.
      </p>
        <?php
            }
            endforeach;
            ?>

P粉988025835
P粉988025835

全部回覆(1)
P粉283559033

假設$latest_tenders是您的資料庫結果,首先檢查$latest_tenders陣列是否不為空。如果不為空:循環遍歷 $latest_tenders 並列印其 $tenders。否則(為空):列印您的錯誤訊息。

<?php if(!empty($latest_tenders)) : ?>
    <?php foreach($latest_tenders as $tenders):
        $time = strtotime($tenders['post_date']);
        $month=date('m', $time);
        switch($month){
            case 1:
                echo "January "; break;
                    .
                    .
                    .
            default:
                echo "January ";
        }
        echo date('d', $time).', ';
        echo date('Y', $time).'&nbsp';
        echo date('h', $time).':';
        echo date('i',     $time).' |&nbsp';
    ?>
        <i class="fas fa-user"></i> <?php echo $tenders['author'];?></p></div><br/>
    <?php endforeach; ?>
<?php else : ?>
    <p style=" font-size:13px; word-spacing: 5px;color:tomato;">
        Tender Name : No advertised Tenders for No, Check again later.
    </p>
<?php endif; ?>
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板