簡體   English   中英

更改PHP中的變量,但JavaScript中的相同變量不變

[英]Changing variable in php but same variable in javascript not changing

我正在嘗試創建一個Google圖表來顯示流程在不同時間段內的資源使用情況。 我將不同的csv文件導入到服務器,然后使用php讀取它,這是php文件:

   //PHPVar.php
    <?php
    class chartData
    {
        var $length;
        var $catagories;
        var $elements = array();
        var $numFiles;
        var $allFiles;       
        function startUp($input = 5)
        {
            $dir = "CSV";
            $this->allFiles = scandir($dir,1);
            $this->numFiles = count($this->allFiles) - 2;               

            $currentDir = $input;           
            $filePath = $dir;
            $filePath .= "/";
            $filePath .= $this->allFiles[intval($currentDir)];

            $file = fopen($filePath,"r");
            $this->elements=array(fgetcsv($file));

            while(! feof($file)
                $this->elements[]=fgetcsv($file);                   

            $this->length=count($this->elements);               
            $this->catagories=count($this->elements[0]);                
        }
    }
    $data = new chartData;
    //If there's a parameter input from AJAX then call startup to
    //refill the elements list
    if(isset($_POST['fileNumber']))
        $data->startUp($_POST['fileNumber']);
    //Else call startup with default parameter
    else
        $data->startUp();
    ?>

因此,我存儲所有csv文件的目錄稱為CSV,因此,如果沒有對startup()的輸入,則默認情況下它將是一個指定的文件,並將文件中的所有值存儲到名為elements []的數組中[]。 我首先創建一個chartData對象,並檢查是否有輸入,如果沒有輸入,則意味着這是第一個調用。 在我的網頁中,我有一個下拉菜單,該菜單顯示CSV目錄中的所有文件,並且我希望它使圖形以我選擇的csv文件中的值刷新。 這是我用來將數據(我選擇的文件)從javascript傳輸到php的代碼:

    function changeFile(str) {
                $.ajax({
                    url: 'PHPVar.php',
                    type: 'POST',
                    data: {fileNumber:str},
                    success: function(data) {console.log(data);}
                    });                    
                chart.clearChart();
                drawChart();
            }

因此,最終下拉菜單中的每個條目都會調用此函數,該函數應再次運行PHPVar.php以刷新值。 生成的第一個圖很好,問題是當我嘗試更改csv文件以進行繪制時。 看來我在javascript中看到的php變量沒有改變,而PHPVar.php中的php變量卻沒有改變。 我已經通過將一些變量的值輸出到控制台來驗證了這一點,並且PHPVar.php的變量確實在下拉菜單中的每次調用中都在變化,但是無論我在javascript中使用哪個變量記錄同一個變量,該值始終保持不變。與我第一次啟動網站時相同,無論我從下拉菜單中用不同的值調用該函數多少次。 在我繪制的圖形文件中

    <?php include 'PHPVar.php' ?>

可能是include僅制作了一個PHPVar.php的副本,所以當我去更改變量時,我實際上是在PHPVar中對其進行了更改,而包含include創建的同名變量保持不變? 任何答案/建議表示贊賞。 這是js函數:

            google.load("visualization", "1", {packages:["corechart"]});
            google.setOnLoadCallback(drawChart);
            var data;
            var options;
            var time;
            var row;
            var selected = 1;
            var average = 0;
            var max = 0;
            var index;
            var size;
            var chart;

            function drawChart() {
                average = 0;
                max = 0;
                data = new google.visualization.DataTable();
                var testArray = null;
                testArray = [];            
                <?php   
                //Export data from php server into javascript array *VERY SENSITIVE, make backup before changing*

                for($i=0;$i<$data->length-1;$i++)
                {
                ?>            
                    testArray[<?php echo $i; ?>] = [];
                    <?php

                    for($j=0;$j<$data->catagories;$j++)
                    {
                    ?>
                        <?php
                        if($j == 0 && $i != 0)
                        {
                            $timeStamp = array();
                            $time = preg_split('/-/', $data->elements[$i][0]);

                            $timeStamp = array(2015,(int)$time[0],(int)$time[1],(int)$time[2],(int)$time[3],(int)$time[4]);

                            ?>

                            testArray[<?php echo $i; ?>][<?php echo $j; ?>] = <?php echo json_encode($timeStamp,JSON_NUMERIC_CHECK); ?>;

                        <?php
                        } else if($i == 0)
                        {
                        ?>
                            testArray[<?php echo $i; ?>][<?php echo $j; ?>] = '<?php echo $data->elements[$i][$j]; ?>'; 
                        <?php
                        } else
                        {
                        ?>
                            testArray[<?php echo $i; ?>][<?php echo $j; ?>] = <?php echo (int)$data->elements[$i][$j]; ?>; 
                        <?php
                        }
                    }
                }
                ?> 
                // Declare columns
                row  = testArray[0][selected];
                time = testArray[0][0];
                data.addColumn('datetime', time);
                data.addColumn('number', row);
                //alert(String(testArray[1][0][1]));
                for(index=1;index<<?php echo $data->length-1; ?>;index++)
                {
                    data.addRow([new Date(testArray[index][0][0],
                        testArray[index][0][1],
                        testArray[index][0][2],
                        testArray[index][0][3],
                        testArray[index][0][4],
                        testArray[index][0][5]), testArray[index][selected]]);
                    average += testArray[index][selected];
                    if(testArray[index][selected] > max)
                        max = testArray[index][selected];
                }
                average = average/<?php echo $data->length-1; ?>;
                var options = {
                    width: 900,
                    height: 500,
                    title: row,
                    hAxis: {
                      title: 'Time',
                      viewWindow: {
                      min: new Date(testArray[1][0][0],
                      testArray[1][0][1],
                      testArray[1][0][2],
                      testArray[1][0][3],
                      testArray[1][0][4],
                      testArray[1][0][5]),//testArray[1][0]),
                      max: new Date(testArray[<?php echo $data->length-2; ?>][0][0], 
                      testArray[<?php echo $data->length-2; ?>][0][1], 
                      testArray[<?php echo $data->length-2; ?>][0][2], 
                      testArray[<?php echo $data->length-2; ?>][0][3], 
                      testArray[<?php echo $data->length-2; ?>][0][4], 
                      testArray[<?php echo $data->length-2; ?>][0][5])
                      },
                      gridlines: 
                      {
                          count: -1,
                          units:
                          {
                              days: {format: ['MMM dd']},
                              hours: {format: ['HH:mm', 'ha']}
                          }
                      },
                      minorGridlines:
                      {
                          units: 
                          {
                            hours: {format: ['hh:mm:ss a', 'ha']},
                            minutes: {format: ['HH:mm a Z', ':mm']}
                          }
                      }
                    },
                    vAxis: {
                      title: 'Resource'
                    },
                    backgroundColor: '#f1f8e9'
                };

                chart = new google.visualization.LineChart(document.getElementById('curve_chart'));

                chart.draw(data, options);
                document.getElementById('Average').innerHTML = "Mean Resource Usage: "+ average;
                document.getElementById('Max').innerHTML = "Max Resource Usage: "+ max;

            }

我認為問題可能出在您的jQuery ajax調用上:

$.ajax({
  url: 'PHPVar.php',
  type: 'POST',
  data: {fileNumber:str},
  success: function(data) {console.log(data);}
});       

默認情況下,jQuery .ajax()是緩存結果。

因此,您可能希望通過執行以下操作來默認禁用緩存:

$.ajaxSetup({ cache: false });

在初始化JS代碼之前的某個地方。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM