简体   繁体   English

如何在视图中将变量从控制器传递给ajax请求,然后在php脚本中检索它(codeigniter)

[英]How to pass variable from controller to ajax request in view and then retrieve it in php script (codeigniter)

Hello I want to pass this variable: ID_person to view and then send it to php script to get requested data from database. 您好我想传递此变量: ID_person查看然后将其发送到PHP脚本以从数据库获取请求的数据。

Controller: 控制器:

 public function view($id){
        $data = array();

        if(!empty($id)){
            $data['zakaznici'] = $this->Zakaznici_model->getRows($id);              //$data['temperatures'] = $this->Temperatures_model->getRows($id); 
              $data['title'] = 'Údaje o zákazníkovi';
              $data['ID_person'] = $id;


            $this->load->view('zakazniciview', $data);

        }else{
            redirect('/zakaznici');
        }
    }

So far I'm using this request in my view: 到目前为止,我在我的视图中使用此请求:

<script type="text/javascript">


$(function() {

  $.ajax({
    url: "http://localhost/skolaa/chart_vypozicky.php",
    type: "GET",
    success: function(data) {
      chartData = data;
      var chartProperties = {
        caption: "Celková suma za prenájmy počas jednotlivých rokov",
        xAxisName: "Rok",
        yAxisName: "Suma ",
        rotatevalues: "0",
        useDataPlotColorForLabels: "1",
        theme: "fusion"

      };
      apiChart = new FusionCharts({
        type: "column2d",
        renderAt: "chart-container",
        width: "550",
        height: "350",
        dataFormat: "json",
        dataSource: {
          chart: chartProperties,
          data: chartData
        }
      });
      apiChart.render();
    }
  });
});
</script>

It is working but I need to get somehow that ID_person variable from controller and sent it to chart_vypozicky.php script and then retrieve it in query in this script. 它正在工作,但我需要从控制器以某种方式得到ID_person变量并将其发送到chart_vypozicky.php脚本,然后在此脚本中的查询中检索它。

Php script: PHP脚本:

<?php
    //address of the server where db is installed
    $servername = "localhost";
    //username to connect to the db
    //the default value is root
    $username = "root";
    //password to connect to the db
    //this is the value you would have specified during installation of WAMP stack
    $password = "";
    //name of the db under which the table is created
    $dbName = "prenajom_sportovisk";
    //establishing the connection to the db.
    $conn = new mysqli($servername, $username, $password, $dbName);
    //checking if there were any error during the last connection attempt
    if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
    }
    //the SQL query to be executed
    $query = "SELECT SUM(cena) as price, YEAR(DATUM) as rok FROM sportoviska_zakaznici where ID= "; //I need to retrieve that id variable here
    //storing the result of the executed query
    $result = $conn->query($query);
    //initialize the array to store the processed data
    $jsonArray = array();
    //check if there is any data returned by the SQL Query
    if ($result->num_rows > 0) {
      //Converting the results into an associative array
      while($row = $result->fetch_assoc()) {
        $jsonArrayItem = array();
        $jsonArrayItem['label'] = $row['rok'];
        $jsonArrayItem['value'] = $row['price'];
        //append the above created object into the main array.
        array_push($jsonArray, $jsonArrayItem);
      }
    }
    //Closing the connection to DB
    $conn->close();
    //set the response content type as JSON
    header('Content-type: application/json');
    //output the return value of json encode using the echo function.
    echo json_encode($jsonArray);
?>

Is it somehow possible to get this done? 是否有可能完成这项工作? I will be really thankful for all suggestions. 我将非常感谢所有的建议。

In your view pass ID_person and then Use $_GET['ID_person'] in chart_vypozicky.php page 在您的视图中传递ID_person,然后在chart_vypozicky.php页面中使用$ _GET ['ID_person']

<script type="text/javascript">


$(function() {

  $.ajax({
    url: "http://localhost/skolaa/chart_vypozicky.php",
    type: "GET",
    data:{ID_person:'<?php echo $ID_person; ?>'},
    success: function(data) {
      chartData = data;
      var chartProperties = {
        caption: "Celková suma za prenájmy počas jednotlivých rokov",
        xAxisName: "Rok",
        yAxisName: "Suma ",
        rotatevalues: "0",
        useDataPlotColorForLabels: "1",
        theme: "fusion"

      };
      apiChart = new FusionCharts({
        type: "column2d",
        renderAt: "chart-container",
        width: "550",
        height: "350",
        dataFormat: "json",
        dataSource: {
          chart: chartProperties,
          data: chartData
        }
      });
      apiChart.render();
    }
  });
});
</script>


<?php
    //address of the server where db is installed
    $servername = "localhost";
    //username to connect to the db
    //the default value is root
    $username = "root";
    //password to connect to the db
    //this is the value you would have specified during installation of WAMP stack
    $password = "";
    //name of the db under which the table is created
    $dbName = "prenajom_sportovisk";
    //establishing the connection to the db.
    $conn = new mysqli($servername, $username, $password, $dbName);
    //checking if there were any error during the last connection attempt
    if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
    }
    //the SQL query to be executed
    $person_ID=$_GET['ID_person']
    $query = "SELECT SUM(cena) as price, YEAR(DATUM) as rok FROM sportoviska_zakaznici where ID='".$person_ID."'"; //I need to retrieve that id variable here
    //storing the result of the executed query
    $result = $conn->query($query);
    //initialize the array to store the processed data
    $jsonArray = array();
    //check if there is any data returned by the SQL Query
    if ($result->num_rows > 0) {
      //Converting the results into an associative array
      while($row = $result->fetch_assoc()) {
        $jsonArrayItem = array();
        $jsonArrayItem['label'] = $row['rok'];
        $jsonArrayItem['value'] = $row['price'];
        //append the above created object into the main array.
        array_push($jsonArray, $jsonArrayItem);
      }
    }
    //Closing the connection to DB
    $conn->close();
    //set the response content type as JSON
    header('Content-type: application/json');
    //output the return value of json encode using the echo function.
    echo json_encode($jsonArray);
?>

Use this updated code 使用此更新的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM