0

Output This is the output

I want to display all data in tables according to date. I store data from database in array to be displayed. But I got error warning:undefined array key. Below is my code to store data in array.

 include("db.php");

                            if(isset($_GET['from_date']) && isset($_GET['to_date'])){
                                
                                global $conn;
                                $from_date = $_GET['from_date'];
                                $to_date = $_GET['to_date'];

                                $stmt = $connection->prepare("
                                SELECT 
                                    date(datetime_entry_queue) AS Date,
                                    duration_wait
                                    FROM call_entry
                                    WHERE status='abandonada'
                                    AND date(datetime_entry_queue) BETWEEN '$from_date' AND '$to_date'
                                ");

                                
                                $stmt->execute();
                                $result = $stmt->get_result();

                                $Data = null;

                                while($row = $result->fetch_assoc()) {


                                    if($row['duration_wait'] >= 0 AND ($row['duration_wait'] <= 30))
                                    {
                                        $Data[$row['Date']]['0-30'] += 1;

                                    }
                                    else if($row['duration_wait'] >= 31 AND ($row['duration_wait'] <= 60))
                                    {
                                        $Data[$row['Date']]['31-60'] += 1;
                                    }
                                    else if($row['duration_wait'] >= 61 AND ($row['duration_wait'] <= 120))
                                    {
                                        $Data[$row['Date']]['61-120'] += 1;
                                    }
                                    else
                                    {
                                        $Data[$row['Date']]['>120'] += 1;
                                    }
                                }

                                echo "<pre>";
                                print_r($Data);
                                echo "</pre>";

                                $stmt->close();

                               
                            }

This how I return the data in the table on my page:

<?php
                            
                            foreach($Data AS $Date => $Total)
                            {

                            ?>
                                <tr>
                                    <td><?php echo $Date;?></td>
                                    <td><?php echo $Total["0-30"];?></td>
                                    <td><?php echo $Total['31-60'];?></td>
                                    <td><?php echo $Total['61-120'];?></td>
                                    <td><?php echo $Total['>120'];?></td>
                                    
                                   
                                </tr>
                            <?php
                            }
                            
                            ?>

I already run my query and it shows all the data I needed.

Yamin
  • 9
  • 1
  • 5
  • Do you know on what line the warning occurs? That would help us to help you :) – Gowire Nov 20 '22 at 14:04
  • You can use the `??` operator when an array element is missing. For example, write `` instead. This will return "None" if the array element isn't found. I guess that your code won't populate the Data array with all possible keys... – Gowire Nov 20 '22 at 14:09
  • @Gowire thank you for the response I already edited my post, you can see the output – Yamin Nov 20 '22 at 14:10
  • @Gowire Why the element isnt found when I already defined the key in if statement and why it wont populate the data array? Im sorry im still on process of learning array so I would appreciate it if you can explain the details :) – Yamin Nov 20 '22 at 14:21

1 Answers1

1

The problem here should be using the "+=" statement.

Its fine to assign a value to none-existing array elements, but the "+=" first reads the element (before incrementing).

Reading non existing elements will result in warnings.

  • @Yamin: It would be helpful then you would edit your question to just ask about that and create a minimal example demonstrating what and what not works for you (and what the desired outcome would be). Compare https://stackoverflow.com/help/minimal-reproducible-example – hakre Nov 20 '22 at 16:05