【问题标题】:How to make HTML table use PHP array如何使 HTML 表格使用 PHP 数组
【发布时间】:2018-01-23 12:38:57
【问题描述】:

我想让 HTML 表格使用 PHP 数组。

这是 PHP 端代码

function get_location_list()
{
    global $connect;

    $query = "select location
                  , SUBSTRING_INDEX(SUBSTRING_INDEX(location,'-',1),'-',-1) loc_a
                  , SUBSTRING_INDEX(SUBSTRING_INDEX(location,'-',2),'-',-1) loc_b
                  , SUBSTRING_INDEX(SUBSTRING_INDEX(location,'-',3),'-',-1) loc_c
               from stock_location
               where location> ''
           order by loc_a, loc_b * 1, loc_c * 1 limit 100";
    $result = mysql_query($query, $connect);
    $arr_result = array();

    while( $data = mysql_fetch_array( $result ) )
    {
        $arr_result[$data[loc_a]][] = $data[location];
    }

    echo json_encode( $arr_result );
}

此代码将数组发送到 HTML 端。数组值是这样的

"A1":["A1-1","A1-1-1","A1-1-2","A1-1-3","A1-1-4","A1-2-1","A1-2","A1-2-
2","A1-2-3","A1-2-4","A1-3-1","A1-3-2","A1-3","A1-3-3","A1-3-4","A1-4-
1","A1-4-2","A1-4-3","A1-4-4","A1-4","A1-5-1","A1-5-2","A1-5-3","A1-5-
4","A1-5"],
"A2":["A2-1","A2-1-1","A2-1-2","A2-1-3","A2-1-4","A2-2-1","A2-
2","A2-2-2","A2-2-3","A2-2-4","A2-3-1","A2-3-2","A2-3-3","A2-3","A2-3-
4","A2-4-1","A2-4-2","A2-4-3","A2-4","A2-4-4","A2-5-1","A2-5-2","A2-5-
3","A2-5-4","A2-5"],
"A3":["A3-1","A3-1-1","A3-1-2","A3-1-3","A3-1-4","A3-2-
1","A3-2","A3-2-2","A3-2-3","A3-2-4","A3-3-1","A3-3-2","A3-3-3","A3-3","A3-
3-4","A3-4-1","A3-4-2","A3-4-3","A3-4","A3-4-4","A3-5-1","A3-5-2","A3-5-
3","A3-5-4","A3-5"],
"A4":["A4-1","A4-1-1","A4-1-2","A4-1-3","A4-1-4","A4-2-
1","A4-2","A4-2-2","A4-2-3","A4-2-4","A4-3-1","A4-3-2","A4-3-3","A4-3","A4-
3-4","A4-4-1","A4-4-2","A4-4-3","A4-4","A4-4-4","A4-5-1","A4-5-2","A4-5-
3","A4-5-4","A4-5"]

这是 HTML 端。不同的文件,与 PHP 不同的文件

  <table class="table table-bordered">
      <thead>
        <tr>
            <th class="header_center" width=100>location</th>
            <th class="header_center" width=100>product</th>
            <th class="header_center" width=100>qty</th>
        </tr>
      </thead>

      <tbody id="table_contents">
      </tbody>
    </table>

这是javascript。与 HTML 在同一文件中的 javascript 代码

    $.post("function.htm", { 
        template   : "IU00", 
        action     : "get_location_list",
    },

    function( response ){ 

         $("#table_contents").empty();

            obj = eval( "(" + response + ")" )

            for( var i=0; i < obj.length; i++ )
            {
                str = "<tr>";
                str += "<td>" + obj[i].location + "</td>";
                str += "<td>" + "</td>";
                str += "<td>" + "</td>";
                str += "</tr>";

                $("#table_contents").append(str);
            } 


    });

我想显示这样的 HTML 表格。

  __________ __________ __________ __________
 |location 1|location 2|location 3|location 4|
 |  A1-1-1  |  A2-1-1  |  A3-1-1  |  A4-1-1  |
 |  A1-1-2  |  A2-1-2  |  A3-1-2  |  A4-1-2  |
 |  A1-1-3  |  A2-1-3  |  A3-1-3  |  A4-1-3  |
 |  A1-1-4  |  A2-1-4  |  A3-1-4  |  A4-1-4  |
 |  A1-2-1  |  A2-2-1  |  A3-2-1  |  A4-2-1  |
 |  A1-2-2  |  A2-2-2  |  A3-2-2  |  A4-2-2  |
       .         .           .          .
       .         .           .          .
 |  A1-5-5  |  A2-5-5  |  A3-5-5  |  A4-5-5  |
 ---------------------------------------------

我不确定问题出在 Javascript 还是 HTML 代码上。需要进行哪些更改才能以上述格式显示表格?

【问题讨论】:

  • 为什么需要发送 json?为什么不直接从 PHP 发送 HTML 输出而不是在 JavaScript 中解析它?只是想了解一下,因为您没有使用任何 JS 框架并手动执行,所以 PHP 将有更好的控制。
  • 丹麦哈基姆汗提出了一个很好的建议,这里真的不需要JS/JQuery。此外,您正在使用已弃用的 mysql 数据库命令。使用 mysqli php.net/manual/en/book.mysqli.php(或 PDO)。

标签: javascript php html arrays html-table


【解决方案1】:

// Code goes here
 /**
  * https://stackoverflow.com/questions/48401981/how-to-make-html-table-use-php-array#
  */ 
var response ="";
var table_header = $('#table_header');
var table_body = $('#table_body');

function processResponse(resp){
  resp = {'A1':['A1-1','A1-2','A1-3'], 'A2':['A2-1','A2-2','A2-3','A2-4']};
 
 // work on the table header
  var columns ="";
  var columnNames = Object.keys(resp);
  var maxRow = 0; // so we know which key has the longest row
  for(var col=0; col < columnNames.length; col++){
    columns += '<th class="header_center" width=100>'+columnNames[col]+'</th>';
    
    if(maxRow < resp[columnNames[col]].length ){
      maxRow = resp[columnNames[col]].length;
    }
  } 
  table_header.html(columns);
  
  // lets work on the table row based on the longest row
  var rows = [];
  for(var i=0; i< maxRow; i++){
    var single_row = [];
    // loop through the columns
    for (var col=0; col < columnNames.length; col++){
      var value = resp[columnNames[col]];
      single_row.push(value[i]);
    }
    rows.push(single_row); // row created
  }

// now update our table with it 
table_body.html("");
  for(var row=0; row<rows.length; row++){
    var tr = "<tr>";
    // now td
    for(var col=0; col<rows[row].length; col++){
      tr +="<td>"+rows[row][col]+"</td>";
    }
    
    tr +="</tr>";
    
    table_body.append(tr);
  } 
  
}


$("#process").click (function(e){
  processResponse();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

  <table class="table table-bordered">
      <thead>
        <tr id="table_header">
            
        </tr>
      </thead>

      <tbody id="table_body">
      </tbody>
    </table>
    
    <button id="process">Process record</button>

您可以更新响应以跟随服务器返回的响应。 您的服务器可以执行大部分操作。如果您检查 rows 是如何生成的,您可以将该进程移至您的服务器,然后对行进行两次迭代到列

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-27
    • 2016-04-12
    • 2012-01-16
    • 2019-11-05
    • 1970-01-01
    • 2012-02-07
    • 2020-06-30
    • 2014-01-25
    相关资源
    最近更新 更多