如何插入JSON数据表? [英] How to insert json data in to table?

查看:127
本文介绍了如何插入JSON数据表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了PHP的code生成JSON数据,现在我们要显示该JSON数据到表中。我的PHP code是能够产生数据的,但不能能够插入到表,请帮助我在此。

我的PHP code生成JSON数据booking.php

 < PHP
标题(访问控制 - 允许 - 产地:*'); //如果在跨Domaim AJAX调用请求工作
的mysql_connect(localhost的根,2180);
mysql_select_db(服务);

$查询=SELECT * FROM客户那里服务='2';
$结果= mysql_query($查询);

如果($结果=== FALSE){
  死亡(可以\'吨做到这一点:mysql_error());
}

$ retVal的=阵列();
// MYSQL_ASSOC删除键=字段标识
而($行= mysql_fetch_array($结果,MYSQL_ASSOC)){
  $ retVal的[] = $行;
}
回声json_en code($ retVal的);
 

我的JavaScript JSON数据打印到表

 <脚本>
            功能fetchData1(){
                $(数据contacts1-JS TBODY)空()。
                $获得(HTTP://localhost/service/newJobs.php功能(数据){
                    $每个(数据,功能(我,联系方式){
                        $(数据contacts1-JS)。追加(

                            &其中; TD>中+ contact.cust_name +&所述; / TD>中+
                            &其中; TD>中+ contact.cust_mobile +&所述; / TD>中+
                            &其中; TD>中+ contact.cust_email +&所述; / TD>中+
                            &其中; TD>中+ contact.cust_address +&所述; / TD>中+

                            );
                    });
                });
            }

             $(文件)。就绪(函数(){
                  $(数据contacts1-JS TBODY)空()。
                $('#fetchContacts1)。点击(函数(){
                     fetchData1();
                });
            });

        < / SCRIPT>
 

JSON格式是由上面的PHP code产生

  [
    {
        CID:65,
        地址1:PWD路,B Narayanapura,班加罗尔,印度卡纳塔克邦
        地址2:JSS布局,迈索尔,印度卡纳塔克邦
        城市:班加罗尔,
        意见:DS,
        电子邮件:you@gmail.com
        地标:PWD路,B Narayanapura,班加罗尔,印度卡纳塔克邦
        scheduledDate:13月 -  2015年,
        scheduledTime:10:30,
        服务:2,
        userContactNumber:12.2亿,
        username的:加金德拉
    }
]
 

HTML $ C $下表

 < D​​IV CLASS =排液>
                        <! - 块 - >
                        < D​​IV CLASS =块>
                            < D​​IV CLASS =导航栏导航栏,内部块标头>
                                < D​​IV CLASS =静音拉左>木匠服务< / DIV>
                            < / DIV>
                            <&GT DIV CLASS =块内容崩溃;
                                < D​​IV CLASS =span12>
                                     <表类=数据contacts1-JS表的表条纹>
                                          < THEAD>
                                            &其中; TR>
                                                  百分位> ID< /第i个
                                                  <第i个客户名称和LT; /第i个
                                                  百分位>客户移动和LT; /第i个
                                                  <第i个客户的电子邮件和LT; /第i个
                                                  百分位>地址< /第i个
                                                  百分位>日期和LT; /第i个
                                                  百分位>时间< /第i个
                                                  <第i个状态和LT; /第i个
                                            < / TR>
                                          < / THEAD>
                                      < TBODY>

                                      < / TBODY>
                                    < /表>
                                < / DIV>
                                <按钮ID =fetchContacts1级=BTN BTN-默认的类型=提交>刷新< /按钮>
                            < / DIV>
                        < / DIV>
                        <! -  /块 - >
                    < / DIV>
 

解决方案

这是一种用JavaScript这种工作

 函数fetchData1(){
                    $(数据contacts1-JS TBODY)空()。
                    $获得(HTTP://localhost/service/newJobs.php功能(数据){
                    数据= JSON.parse(数据); //如果服务器返回的文本,而不是JSON对象
                     对于(数据变种I){
                         VAR TR = $(< TR>< / TR>中);
                         tr.append(&其中; TD>中的数据+ [I] .cust_name +&所述; / TD>中+
                                &其中; TD>中+数据[I] .cust_mobile +< / TD>中+
                                &其中; TD>中+数据[I] .cust_email +< / TD>中+
                                &其中; TD>中+数据[I] .cust_address +< / TD>中);
                         $(数据contacts1-JS TBODY)追加(TR)。
                       }
                    });
                }

                 $(文件)。就绪(函数(){
                      $(数据contacts1-JS TBODY)空()。
                    $('#fetchContacts1)。点击(函数(){
                         fetchData1();
                    });
                });
 

在服务器脚本(PHP)添加在第一行

 标题(内容类型:应用程序/ JSON);
 

I have written php code to generate json data, Now We have to display this json data into table. My php code is able to generate data but not able able to insert into table, please help me on this.

My php code to generate json data 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 );

My javascript to print json data to table

<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>

Format of JSON which is generated by above php code

[
    {
        "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 code for table

<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>

解决方案

This is a way to work this with 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();
                    });
                });

On server script (php) add in first row

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

这篇关于如何插入JSON数据表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆