簡體   English   中英

如何將json數據插入表中?

[英]How to insert json data in to table?

我已經編寫了php代碼來生成json數據,現在我們必須將該json數據顯示到表中。 我的PHP代碼能夠生成數據, 但無法插入表中 ,請幫助我。

我的PHP代碼生成JSON數據booking.php

<?php
header('Access-Control-Allow-Origin: *');//Should work in Cross Domaim ajax Calling request
mysql_connect("localhost","root","2180");
mysql_select_db("service");

$query="Select * from customer where services='2'";
$result=mysql_query($query);

if ( $result === false ) {
  die("Can\'t do that: " . mysql_error());
}

$retVal = array();
//MYSQL_ASSOC remove key =field identifier
while( $row = mysql_fetch_array( $result, MYSQL_ASSOC ) ) {
  $retVal[] = $row;
}
echo json_encode( $retVal );

我的JavaScript將JSON數據打印到表

<script>    
            function fetchData1(){              
                $(".data-contacts1-js tbody").empty();
                $.get("http://localhost/service/newJobs.php", function(data) {
                    $.each(data, function(i, contact) {
                        $(".data-contacts1-js").append(

                            "<td>" + contact.cust_name + "</td>" +
                            "<td>" + contact.cust_mobile + "</td>" +
                            "<td>" + contact.cust_email + "</td>" +
                            "<td>" + contact.cust_address + "</td>" +

                            );
                    });
                });
            }  

             $(document).ready(function(){
                  $(".data-contacts1-js tbody").empty();
                $('#fetchContacts1').click(function() {
                     fetchData1();
                });
            });

        </script>

由上面的php代碼生成的JSON格式

[
    {
        "cId": "65",
        "address1": "PWD Road, B Narayanapura, Bengaluru, Karnataka, India",
        "address2": "JSS Layout, Mysore, Karnataka, India",
        "city": "Bangalore",
        "comments": "ds",
        "email": "you@gmail.com",
        "landMark": "PWD Road, B Narayanapura, Bengaluru, Karnataka, India",
        "scheduledDate": "13-Feb-2015",
        "scheduledTime": "10:30 AM",
        "services": "2",
        "userContactNumber": "1220000000",
        "userName": "Gajendra"
    }
]

表的html代碼

<div class="row-fluid">
                        <!-- block -->
                        <div class="block">
                            <div class="navbar navbar-inner block-header">
                                <div class="muted pull-left">Carpenter Services</div>
                            </div>
                            <div class="block-content collapse in">
                                <div class="span12">
                                     <table class="data-contacts1-js table table-striped" >
                                          <thead>
                                            <tr>
                                                  <th>ID</th>
                                                  <th>Customer Name</th>
                                                  <th>Customer Mobile</th>
                                                  <th>Customer Email</th>
                                                  <th>Address</th>
                                                  <th>Date</th>
                                                  <th>Time</th>
                                                  <th>Status</th>
                                            </tr>
                                          </thead>
                                      <tbody>

                                      </tbody>
                                    </table>                                    
                                </div>
                                <button id="fetchContacts1" class="btn btn-default" type="submit">Refresh</button>                          
                            </div>
                        </div>
                        <!-- /block -->
                    </div>

是不是php打印表的選項? 如果是,您可以在foreach循環中僅回顯表行,僅此而已:

while( $row = mysql_fetch_array( $result ) ) {
  $retVal[] = $row;
}

變得像

while( $row = mysql_fetch_array( $result ) ) {
  $table_row .= "
     <tr>
        <td>$row['cust_name']</td>
        <td>$row['cust_mobile']</td>
        <td>$row['cust_email']</td>
        <td>$row['cust_address']</td>
     </tr>";
}

這是使用JavaScript的一種方式

     function fetchData1(){              
                    $(".data-contacts1-js tbody").empty();
                    $.get("http://localhost/service/newJobs.php", function(data) {
                    data=JSON.parse(data); //if the server returns text instead JSON object                       
                     for(var i in data){
                         var tr=$("<tr></tr>");
                         tr.append("<td>" + data[i].cust_name + "</td>" +
                                "<td>" + data[i].cust_mobile + "</td>" +
                                "<td>" + data[i].cust_email + "</td>" +
                                "<td>" + data[i].cust_address + "</td>");
                         $(".data-contacts1-js tbody").append(tr);
                       }
                    });
                }  

                 $(document).ready(function(){
                      $(".data-contacts1-js tbody").empty();
                    $('#fetchContacts1').click(function() {
                         fetchData1();
                    });
                });

在服務器腳本(php)上添加第一行

header('Content-type: application/json');

暫無
暫無

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

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