【问题标题】:Achieve this exact PHP foreach functionality in Ajax?在 Ajax 中实现这个精确的 PHP foreach 功能?
【发布时间】:2019-11-16 07:50:03
【问题描述】:

我得到了这样的令人满意的输出:

从这个 php foreach 循环(关于模型和控制器的视图和 gettinf 详细信息),我根据以下条件从 mysql 数据库表中获取数据:

   <tfoot>
      <?php foreach ($orders as $k => $v) {     
         $order_item = $this->vendor_model->getOrdersItemData($v['o_id']); ?>
      <tr>
         <td><?php echo $v['o_id']?></td>
         <td><?php echo $v['t_id']?></td>
         <td><?php echo $v['grand_total']?></td>
         <td><?php echo $v['order_status']?></td>
         <!--Datavariable for orders-->
         <td>
            <table>
               <thead>
                  <tr>
                     <td>Product Name</td>
                     <td>Quantity</td>
                     <td>Price</td>
                  </tr>
               </thead>
               <tbody>
                  <?php foreach ($order_item as $k => $p) {
                     $product_item = $this->vendor_model->getProductData($p['product_id']); ?>
                  <tr>
                     <td><?php echo $product_item['product_name']?></td>
                     <td><?php echo $p['quantity']?></td>
                     <td><?php echo $product_item['product_price']?></td>
                     <!--data variable for order items-->       
                  </tr>
                  <?php }?>
               </tbody>
            </table>
         </td>
      </tr>
      <?php }?>
   </tfoot>
</table>

我正在尝试像这样在 ajax 中执行此操作:

$.each(data, function(k, v) {
    var product_item = < ? = json_encode($this - > vendor_model - > getOrdersItemData("19", JSON_HEX_TAG)) ? > ;<!--Is there any way to get this "19 from data by loop"-->
    html += '<tr>' +
        '<td>' + v.o_id + '</td>' +
        '<td>' + v.u_id + '</td>' +
        '<td>' + v.grand_total + '</td>' +
        '<td>' + v.order_status + '</td>' +
        //'<td>'+k + ": " + v.t_id+'</td>'+
        //'<td>'+'<pre>'+JSON.stringify(data)+'</td>'+
        '<td>' + '<pre>' + product_item.oi_id + '</td>' +
        '<td>' +
        $.each(product_item, function(k, v) {
            html += '<table>' +
                '<tr>' +
                '<td>' + k + ": " + v.oi_id + '</td>' +
                '<td>' + '<pre>' + JSON.stringify(product_item) + '</td>' +
                '</tr>' +
                '</table>';
        }) +
        '</td>' +
        '<td>' +
        '<a href="javascript:;" class="btn btn-success item-view" data="' + this.o_id + '">View</a>' +
        '<a href="javascript:;" class="btn btn-info item-edit" data="' + this.o_id + '">Edit</a>' +
        '<a href="javascript:;" class="btn btn-danger item-delete" data="' + this.o_id + '">Delete</a>' +

        '</td>' +
        '</tr>';
});

我刚开始使用 Ajax,有什么方法可以在 ajax 中做到这一点,我知道我的 ajax 中也有很多错误。

我想从 ajax 做那个 php forloop 输出。

【问题讨论】:

  • 您可以在 Ajax 回调函数中执行此操作,但您需要发回原始数据(json、xml)并自己构建输出。这两种方法各有利弊。
  • 我认为您对 ajax 的含义有些困惑。 Ajax 表示异步 JavaScript 和 XML(基本上是发送异步请求)。除此之外,根据您的需求,您的问题有 2 个可能的答案。

标签: javascript php ajax


【解决方案1】:

所以...如果您的问题是“如何在发出异步请求时获得相同的结果?” 您可以这样做(我假设您有 jquery 或其他东西,如果没有的话你可以在这里找到一个香草 js 实现 Insert external page html into a page html - 也许不是最好的,我几年前写的,但应该可以解决问题):

假设您在路由“/myCurrentPhpEpicOutput”上有当前版本(在 php 中)。

您的 html + js 可能如下所示:

<body>
    <div id="loadStuffHere"></div>

    <script>
         $(function() {
             $.ajax({
                 url: '/myCurrentPhpEpicOutput',
                 method: 'get',
                 success: function(result) {
                     $('loadStuffHere').html(result);
                 }
             });
         });
    </script>
</body>

此选项基本上将 html 生成保留在服务器端(您满意的那个),然后将其加载到您需要的地方。

如果您的问题是“我如何使用 javascript 和 ajax 请求编写相同的格式来获取原始数据?”

答案与一些调整相似:

让我们再次假设您的端点在路由“/myCurrentPhpEpicData”上。

你的 php 看起来像这样:

<?php
     // ..... bla bla get orders from somewhere
     echo json_encode($orders);
     die();

     // Actually I see you have codeigniter there so maybe return JsonResponse or something instead of echo + die (I haven't work on CI since 2011ish so... can't really remember its functions names but should have one that does that).

那么你的 html + js 会是这样的:

<body>
    <div id="loadStuffHere"></div>

    <script>
         $(function() {
             // This is the "load" you asked about in your question
             $.ajax({
                 url: '/myCurrentPhpEpicData',
                 method: 'get',
                 dataType: 'json',
                 success: function(data) {
                     // There are tons of variations on this... go wild
                     var computedHtml = '';
                     for (var i in data) {
                         var order = data[i];
                         // Here make your form html as a string (you did most of it in your question so you can copy paste and adapt that)
                     }
                     $('loadStuffHere').html(computedHtml);
                 }
             });
         });
    </script>
</body>

【讨论】:

  • 非常感谢您宝贵的时间和精力,但问题是我想根据 多个数组 来执行此操作,例如 order_table 其中有 o_id 和一个 order_items 表,它有 quantityproduct_id 然后是 Products 表获取产品的详细信息到 order_items,然后从订单项到 orders
猜你喜欢
  • 2011-01-09
  • 2016-12-07
  • 1970-01-01
  • 2021-11-23
  • 2022-07-28
  • 1970-01-01
  • 2011-10-21
  • 2012-09-26
  • 2016-11-26
相关资源
最近更新 更多