我想将数据库元素数组提取到表中

时间:2016-07-15 06:19:48

标签: javascript php json ajax

我正在尝试通过json对象将数据库的数组提取到表中,但是它将我的数据库第一行作为表的元素,而另一行作为最后一个数据库行。中间行未显示在输出中。这是我的代码

  <script id="source" language="javascript" type="text/javascript">

 $(function () 
 {
   $.ajax({                                      
  url: 'example.php',             
  data: "",                       
                                   //for example "id=5&parent=6"
  dataType: 'json',                //data format      
  success: function(data)          //on recieve of reply

        {   
    for(var i = 0; i < data.length; i++){
var uid = data[i];             
var firstname = data[i];       
var lastname = data[i];
var email = data[i];
var username = data[i];
var password = data[i];
    }
        $('#output').html("<b>uid: </b>"+uid+"<b> firstname:   </b>"+firstname+"<b> lastname: </b>"+lastname+"<b> email: </b>"+email+"<b> username: </b>"+username+"<b> password: </b>"+password); 


    } 
   });
   }); 
   </script>

使用example.php

<?php

// server info
 $server = 'localhost';
 $user = 'root';
 $pass = '';
 $db = 'ocean';

 $connection = mysql_connect($server, $user, $pass) or die(mysql_error());
$database = mysql_select_db($db) or die(mysql_error());


  $result = mysql_query("select * from oops");        //query

  $array = array();

 while ($row = mysql_fetch_row($result)) {
 $array[] = $row;
 }

 echo json_encode($array);



  ?>

output comes in that format 如何在适当的mannar准备输出

2 个答案:

答案 0 :(得分:2)

第一:

    DELETE FROM votes v WHERE v.id NOT IN 
    (SELECT MIN(id) FROM votes GROUP BY voter_id, votefor_id, vote)

每次都会覆盖$('#output').html("<some code>"); 的内容。所以,你只看到最后的结果。我认为你应该使用#output

append()

或者将所有行收集到变量中并使用$('#output').append("<some code>"); 一次:

html()

第二:

var html = "";
for(var i = 0; i < data.length; i++){
    var uid = data[i];             
    var firstname = data[i];       
    var lastname = data[i];
    var email = data[i];
    var username = data[i];
    var password = data[i];

    html += "<b>uid: </b>"+uid+"<b> firstname:   </b>"+firstname+"<b> lastname: </b>"+lastname+"<b> email: </b>"+email+"<b> username: </b>"+username+"<b> password: </b>"+password; 
}
$("#output").html( html );

所有这些变量都指向一个值var uid = data[i]; var firstname = data[i]; var lastname = data[i]; 。 也许是:

data[i]

<强>更新

如果要将数据显示为表格行,可以执行以下操作:

var uid = data[i].uid;             
var firstname = data[i].firstname;       
var lastname = data[i].lastname;

之后:

html += "<tr>\
    <td><b>uid: </b>"+uid+"<b></td>\
    <td><b>firstname: </b>"+firstname+"</td>\
    <td><b> lastname: </b>"+lastname+"</td>\
    <td><b> email: </b>"+email+"</td>\
    <td><b> username: </b>"+username+"</td>\
    <td><b> password: </b>"+password+"</td>\
</tr>";

答案 1 :(得分:0)

您可以替换您的ajax电话: $就({   dataType:“json”,   url:url,   数据:数据,   成功:成功 });

使用这个使用JQuery:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
$.getJSON("ajax/test.json", function(data) {
  var items = [];
  $.each(data, function(key, val) {
    items.push("<li id='" + key + "'>" + 
    "<b>uid: </b>" + value.uid + 
    "<b> firstname:   </b>" + value.firstname + 
    "<b> lastname: </b>" + value.lastname + 
    "<b> email: </b>" + value.email + 
    "<b> username: </b>" + value.username + 
    "<b> password: </b>" + value.password + "</li>");
  });

  $("<ul/>", {
    "class": "my-new-list",
    html: items.join("")
  }).appendTo("body");
});

在JavaScript中,当您使用它时:

var uid = data[i];             
var firstname = data[i];
...
var username = data[i];
var password = data[i];

您要使用相同的值data[i]设置所有这些变量。如果您想尝试使用JavaScript,请尝试以下方法:

var uid = data[i].uid;             
var firstname = data[i].firstname;       
...
var username = data[i].username;
var password = data[i].password;

因此,您的代码将如下:

$(function() {
  $.ajax({
      url: 'example.php',
      data: "",
      //for example "id=5&parent=6"
      dataType: 'json', //data format      
      success: function(data) {
        data.forEach(function(entry) {
            var uid = entry.uid;
            var firstname = entry.firstname;
            var lastname = entry.lastname;
            var email = entry.email;
            var username = entry.username;
            var password = entry.password;
          }
          $('#output').append("<b>uid: </b>" + uid +
            "<b> firstname:   </b>" + firstname +
            "<b> lastname: </b>" + lastname +
            "<b> email: </b>" + email +
            "<b> username: </b>" + username +
            "<b> password: </b>" + password);
        });
    }
  });
});