Title rewritten as: PHP fails to process when there is no data returned for the date
P粉038856725
P粉038856725 2023-09-10 11:49:01
0
1
527

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粉038856725
P粉038856725

reply all(1)
P粉285587590

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!