Achmad Jaelani
Achmad Jaelani

Reputation: 11

How to Update Php Variable Every Second? Variable from mySql

First of all Thank You for read my post. I Have Code Like This.

<?php
   $con=mysqli_connect("localhost","u855423165_ardui","31039731","u855423165_ardui");
   $sql="SELECT * FROM tempLog ORDER BY timeStamp ASC";
   $result=mysqli_query($con,$sql);

   while ($data = mysqli_fetch_array($result,MYSQLI_ASSOC))
   {
     $data_1[] = $data; 
   }     
   mysqli_close($con);
?> 

I and want to make $data_1[] = $data; Update Every Second and The from Updated database or we can say $sql="SELECT * FROM tempLog ORDER BY timeStamp ASC"; re-run to get newest data.

and my second code (chart code)

 <script type="text/javascript">
                AmCharts.makeChart("chartdiv",
                    {
                        "type": "serial",
                        "categoryField": "timeStamp",
                        "dataDateFormat": "YYYY-MM-DD HH:NN:SS",
                        "maxSelectedSeries": -1,
                        "zoomOutButtonTabIndex": 0,
                        "sequencedAnimation": false,
                                            "backgroundColor": "#FFFFFF",
                        "fontFamily": "Cabin",
                        "theme": "dark",
                        "export": {
                            "enabled": true
                        },
                        "categoryAxis": {
                            "minPeriod": "ss",
                            "parseDates": true
                        },
                        "chartCursor": {
                            "enabled": true,
                            "categoryBalloonDateFormat": "JJ:NN:SS"
                        },
                        "chartScrollbar": {
                            "enabled": true,
                            "dragIconWidth": 37,
                            "graphType": "line",
                            "gridColor": "#000000",
                            "hideResizeGrips": true,
                            "minimum": 1,
                            "offset": 2,
                            "tabIndex": 3
                        },
                        "trendLines": [],
                        "graphs": [
                            {
                                "bullet": "round",
                                "id": "AmGraph-1",
                                "title": "graph 1",
                                "valueField": "timeStamp"
                            },
                            {
                                "bullet": "round",
                                "id": "AmGraph-2",
                                "lineThickness": 3,
                                "title": "graph 2",
                                "valueField": "sensor1"
                            },
                            {
                                "bullet": "round",
                                "id": "AmGraph-3",
                                "lineThickness": 3,
                                "title": "graph 3",
                                "valueField": "sensor2"
                            },
                            {
                                "bullet": "round",
                                "id": "AmGraph-4",
                                "lineThickness": 3,
                                "title": "graph 4",
                                "valueField": "sensor3"
                            },
                            {
                                "bullet": "round",
                                "id": "AmGraph-5",
                                "lineThickness": 3,
                                "title": "graph 5",
                                "valueField": "sensor4"
                            },
                            {
                                "bullet": "round",
                                "id": "AmGraph-6",
                                "lineThickness": 3,
                                "title": "graph 6",
                                "valueField": "sensor5"
                            },
                            {
                                "bullet": "round",
                                "id": "AmGraph-7",
                                "lineThickness": 3,
                                "title": "graph 7",
                                "valueField": "sensor6"
                            }
                        ],
                        "guides": [],
                        "valueAxes": [
                            {
                                "id": "ValueAxis-1",
                                "title": ""
                            }
                        ],
                        "allLabels": [],
                        "balloon": {},
                        "titles": [
                            {
                                "id": "Title-1",
                                "size": 15,
                                "text": "Grafik 6 Sensor"
                            }
                        ],
                        "dataProvider": <?php echo json_encode($data_1); ?>
                    }
                );
  </script>

i wanna insert the new variable to my second code and update the chart. this my how i insert the variable "dataProvider": <?php echo json_encode($data_1);

Upvotes: 1

Views: 259

Answers (1)

arcanine
arcanine

Reputation: 1953

The simplest way forward is to move the once per second loop to JavaScript and have it call PHP once per second since that's more typical for a PHP/JS application.

PHP can do real-time applications with libraries like ReactPHP or Swoole + web sockets, but you should be aware that asynchronous programming is an advanced concept that brings a whole series of complications

As a beginner, you may find NodeJS + socket.io handles this more idiomatically than PHP does, but you may also need to change the database to a reactive one or set up an eventing/pub sub system.

What I'm trying to say is this is a deep rabbit hole to get right, I'd advise running the checking loop in JavaScript to keep things simple

Upvotes: 1

Related Questions