Home  >  Q&A  >  body text

Title rewritten as: PHP fails to process when there is no data returned for the date

I cannot operate two databases. If there is data equal to date, I print the average in one column. If there is no data equal to the date, I get the following error. I need to type an "else" but I can't get it to run when I type it. My code is as follows;

<?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 }

                ?>

P粉038856725P粉038856725428 days ago553

reply all(1)I'll reply

  • P粉285587590

    P粉2855875902023-09-11 09:17:08

    I suggest you check that the value before or inside the condition is not empty and be able to create dateTime object in a specific format

    date_create_from_format(string $format, string $datetime, ?DateTimeZone $timezone = null): DateTime|false

    If the datetime string can create a datetime object, the above will return the datetime object, otherwise it will return a FALSE Boolean value. This way you can execute your else part.

    reply
    0
  • Cancelreply