dqk42179 2014-11-24 10:47
浏览 45

使用JQuery JSON打印MySQL查询结果,而不知道数组键的名称

I am building a facility on a website (using Symfony2) that allows the user to create reports and display them on a screen using AJAX. The reports are effectively SQL statements that are created on the fly and are then ran on the Database.

The problem I have is that I can't fathom a way to display these results to the screen without first knowing what fields are used in the report. The queries could contain just 2 fields from a table, or 15 fields, and I'd like the code to be robust enough to handle this.

So far, this is the code I'm using:

        $.ajax({
                type: 'POST',
                url: urlLink,
                success: function (data) {
                        var Type = (data.recordType);
                        var Results = (data.results);
                        var Name = (data.name);
                        var Description = (data.description);
                        var Titles = (data.titles);

                        $('#reportName').text(Name);
                        $('#reportDescription').text(Description);

                        $('#listTable > tbody:last').empty();
                        $('#listTable > thead:last').empty();

                        $('#listTable > thead:last').append('<tr>'+Titles+'</tr>');

                        $.each(Results, function(i, item) {
                                $('#listTable > tbody:last').append('<tr><td>' + Results[i] + '</td><td>' + Results[i] + '</td><td>' + Results[i] + '</td><td>' + Results[i] + '</td><td>' + Results[i] + '</td><td>' + Results[i] + '</td></tr>');
                        });
                }
        });

The variable Titles comes from the query, as when the user is adding fields to the database these are then added to a string which I then explode using PHP in the controller.

Inside the foreach, every column comes back with [object Object]. When I remove the [i] from the code and replace it with .column-name it will then work. But this is what I'm trying to avoid. I'd like to have something similar to what I do with the Table Titles.

  • 写回答

2条回答

  • douping5226 2014-11-24 10:52
    关注

    Maybe try this, and show output of console.log(data);

    console.log(data);
    var data = $.parseJSON(data);
    
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?