我無法操作兩個(gè)數(shù)據(jù)庫。 如果有等于日期的數(shù)據(jù),我會(huì)在一列中打印平均值。 如果沒有等于日期的數(shù)據(jù),我會(huì)收到以下錯(cuò)誤。 我需要輸入一個(gè)“else”,但是當(dāng)我輸入時(shí)我無法讓它運(yùn)行。 我的代碼如下;
<?php $Sor = $conn->query("SELECT * FROM yemeklistesi"); //burada sat?rlar? dizi olarak ?ektik $rows = $Sor->fetchAll(PDO::FETCH_ASSOC); foreach ($rows as $row) { $id = $row['id']; $kullaniciadi = $row['yemek']; $sifre = $row['tarih']; ?> <tr> <td><?php echo $row['id']; ?></td> <td><?php echo $row['yemek']; ?></td> <td><?php echo $row['tarih']; ?></td> <td style="background-color: green; color:#fff; font-size:20px;"> <?php // echo strtotime($row['tarih']); $bilgiler = $conn->query("SELECT * FROM yemekhane_anket"); $bilgilercek=$bilgiler->fetchAll(PDO::FETCH_ASSOC); $say = 0; $mem = 0; foreach ($bilgilercek as $row2) { $a = strtotime($row2['tarih']); if(strval($row['tarih']) == strval(date('Y-m-d',$a))){ $say = $say + 1; } if(strval($row['tarih']) == strval(date('Y-m-d',$a)) && strval($row2['memnuniyet_durumu']) == '1'){ $mem = $mem + 1; } } echo round(intval($mem) * 100/intval($say)); ?> %</td> </tr> <?php } ?>
我建議您檢查條件之前或內(nèi)部的值不為空,并且能夠以特定格式創(chuàng)建 dateTime 對(duì)象
date_create_from_format(string $format, string $datetime, ?DateTimeZone $timezone = null): DateTime|false
如果日期時(shí)間字符串能夠創(chuàng)建日期時(shí)間對(duì)象,上面將返回日期時(shí)間對(duì)象,否則將返回 FALSE 布爾值。這樣你就可以執(zhí)行你的 else 部分。