简体   繁体   中英

convert data from mysql db to table using an json/php api

I'm trying to make my own api using a JSON and PHP script, which is successfully working (api.mystem.tk/product), however I want to convert that data to an html table with the columns: id, name and date.

My question, how? I've got my code included and you can watch the JSON output in the console of api.mystem.tk/product. I've deleted all the private details in the scripts and replaced them with #______.

api.php :

<?php
  $connect = mysqli_connect('#host', '#user', '#password', '#db');
  if(!isset($_GET['function'])) {
    die('Some error occurred!');
  }
  function GetProducts($db) {
    $sql = mysqli_query($db, 'SELECT * FROM php_test ORDER BY Id ASC LIMIT 
      0, 10');
    $data = array();
    if (mysqli_num_rows($sql) > 0) {
      while ($row = mysqli_fetch_array($sql)) {
        $data[] = $row["Id"];
        $data[] = $row["Name"];
        $data[] = $row["Date"];
      }
    }
    $data = json_encode($data);
    echo $_GET['jsonCallback'].'('.$data.')';
  }
  if (function_exists($_GET['function'])) {
    $_GET['function']($connect);
  }
  echo $data;
?>

function .js:

$(function(){
  var functionName = 'GetProducts';
  function LoadData() {
    $.getJSON("http://api.mystem.tk/product/api.php? 
    function="+functionName+"&jsonCallback=?", function(data) {
      var all_data = [];
      $.each(data, function(k, name){
        var array_data = '<div class="names">'+name+'</div>';
        all_data.push(array_data);
      });
      $('#data').append(all_data);
      console.log(data);
    });
  }
  LoadData();
});

index .php:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title> API TEST </title>
    <meta name="viewport" content="width=device-width,initial- 
      scale=1,maximum-scale=1,user-scalable=0">
    <script src="https://code.jquery.com/jquery-3.3.1.min.js" 
      integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" 
      crossorigin="anonymous"></script>
  </head>
  <body>
    <div id="data"></div>
    <script type="text/javascript" src="function.js"></script>
  </body>
</html>

You'll need to parse JSON data in your function.js using jQuery function. jQuery函数在function.js中解析JSON数据。 You can read brief documentation from: jQuery.parseJSON()

Added code:

<script>

var obj = JSON.stringify([
    {
        "id":'1',
        "name":'Jhon Doe',
        "date":'April 14,2018'
    },
    {
        "id":'2',
        "name":'Coder',
        "date":'April 23, 2018'
    }
]);
var jsontoparse = JSON.parse(obj);
var i = obj.length;
var j=0;
while(j<=i){
    console.log(jsontoparse[j].id);
    j = j+1;
}
</script>

so i've tried using $.parseJSON instead of $getJSON, but without succes console:

Uncaught SyntaxError: Unexpected token <
at m (jquery-3.3.1.min.js:2)
at Function.globalEval (jquery-3.3.1.min.js:2)
at text script (jquery-3.3.1.min.js:2)
at Ut (jquery-3.3.1.min.js:2)
at k (jquery-3.3.1.min.js:2)
at XMLHttpRequest.<anonymous> (jquery-3.3.1.min.js:2)

and i've changed this:

$data[] = $row["Id"];
$data[] = $row["Name"];
$data[] = $row["Date"];

to this:

$data[] = array(
  'id' => $row["Id"],
  'name'=> $row["Name"],
  'date'=> $row["Date"]
)

to be a little bit more specified...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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