2016-08-31 12 views
1

array data这样从.js文件JSON拍摄,而旁边PHP如何使阵列数据foreach循环给

Ok: Array 
(
[MyData] => Array 
    (
     [0] => Product 1 
     [1] => Attr 1 
     [2] => Quantity 1 
     [3] => Price 1 
     [4] => Product 2 
     [5] => Attr 2 
     [6] => Quantity 2 
     [7] => Price 2 
     [8] => Product 3 
     [9] => Attr 3 
     [10] => Quantity 3 
     [11] => Price 3 
     [12] => Product 4 
     [13] => Attr 4 
     [14] => Quantity 4 
     [15] => Price 4 
     [16] => Product 5 
     [17] => Attr 5 
     [18] => Quantity 5 
     [19] => Price 5 
    ) 
) 

在PHP我有这样的事情

$data = $_POST['MyData']; 

MyData数据是通过$ data [0] .... [39]得到的。变量电子邮件模板等 喜欢这里:

$message = '<!DOCTYPE HTML>'. 
(...) 
    '<table cellpadding="15">'. 
     '<tr style="background-color: #ffffff;">'. 
      '<td><p>'.$data[0].'</p></td>'. 
      '<td><p>'.$data[1].'</p></td>'. 
      '<td><p>'.$data[2].'</p></td>'. 
      '<td><p>'.$data[3].'</p></td>'. 
     '</tr>'. 
     '<tr style="background-color: #ffffff;">'. 
      '<td><p>'.$data[4].'</p></td>'. 
      '<td><p>'.$data[5].'</p></td>'. 
      '<td><p>'.$data[6].'</p></td>'. 
      '<td><p>'.$data[7].'</p></td>'. 
     '</tr>'. 
    '</table>'. 
(...) 
; 

如何使一个循环处理该表TR排辈(foreach?)? 问题的存在是因为数组有时只有1个产品(Array 0 to 3),有时可能有40个产品...您更确切地知道问题出在哪里 - 我不想制作,带有数百个数组值的大型HTML/PHP电子邮件模板ID的;) 我在学习js, JSON, ajax and PHP所以请耐心等待我的新手问题。

回答

2

你可以尝试这样的:

$i = 0; 
$output = '<table cellpadding="15">'. 
       '<tr style="background-color: #ffffff;">'.; 

foreach($data as $row) 
{ 
    if($i % 4 === 0) 
    { 
     $output .= '</tr>'. 
        '<tr style="background-color: #ffffff;">'; 
    } 

    $output .= '<td><p>'.$row.'</p></td>'; 
    $i++; 
} 

$output .= '</tr>'. 
     '</table>'; 
+0

谢谢Marco。你的一段代码几乎和我需要的一样工作!几乎因为当我选择3个产品时,我有如下架构的表:产品1,属性1,数量1,(空td)在第一行。 Price 1,Product 2,Attr 2,Quantity 2 in second row。和价格2,产品3,属性3,数量3三分之一。最后第四行,价格只有一个td。单元格中的值已经发生了变化。有任何想法吗? – PipBoy2000

+0

对不起!我有$ i = 1;当我改变它为$ i = 0;就像你最初做的那样完美! +1和顶部答案马克! – PipBoy2000

0

的foreach会出现类似这样的,如果你在PHP中有值的数组中的变量:

<table> 
    <thead> 
     <th>S.No</th> 
     <th>Name</th> 
    </thead> 
    <tbody> 
<?php 
$data = $_POST['MyData']; 
$i=1;// Auto Increment 
foreach($data as $single_value) 
{ 
?> 
<tr> 
    <td><?php echo $i; ?></td> 
    <td><?php echo $single_value; // This will display the value ?></td> 
</tr> 

<?php 
$i++; 
} 
?> 
</tbody> 
</table> 

在此的foreach的<tr>将被重复,直到你有多少值在数组中。

+1

否确定它解决了他的问题,但... –

1

即使是艰难的,我喜欢马文回答上述,我建议从

[0] => Product 1 
    [1] => Attr 1 
    [2] => Quantity 1 
    [3] => Price 1 
    [4] => Product 2 
    [5] => Attr 2 
    [6] => Quantity 2 
    [7] => Price 2 
    [8] => Product 3 
    [9] => Attr 3 
    [10] => Quantity 3 
    [11] => Price 3 
    [12] => Product 4 
    [13] => Attr 4 
    [14] => Quantity 4 
    [15] => Price 4 
    [16] => Product 5 
    [17] => Attr 5 
    [18] => Quantity 5 
    [19] => Price 5 

[0] => Product 1 
    [0] => Attr 1 
    [1] => Quantity 1 
    [2] => Price 1 
[1] => Product 2 
    [0] => Attr 2 
    [1] => Quantity 2 
    [2] => Price 2 
[2] => Product 3 
    [0] => Attr 3 
    [1] => Quantity 3 
    [2] => Price 3 
[3] => Product 4 
    [0] => Attr 4 
    [1] => Quantity 4 
    [2] => Price 4 
[4] => Product 5 
    [0] => Attr 5 
    [1] => Quantity 5 
    [2] => Price 5 

重建阵列不仅它解决了您的问题,它还允许您轻松处理不同产品的相同数据。在我的体验中,我总是有一些计算要在总价和其他事情上做。另外,如果你做了一次像这样的一些resort()函数,你可以在任何其他地方使用它作为一个衬垫。

更新#1 而通过这种类型的再整理的可以通过简单的做的方式(这是比快的foreach)

我会去

function resort($arr, $countOfElementsPerProduct) { 
    $ret = array(); 
    for($i = 0; $i < count($arr); $i++) { 
     if($i % $countOfElementsPerProduct == 0) { 
      if(count($product)) { 
       $ret[] = $product; 
      } 
      $product = array(); 
     } else { 
      $product[] = $arr[$i]; 
     } 
    } 
    return $ret; 
} 

更新# 2 在你的情况,我会去这样的事情:

$('.ok').on('click', function(e){ 
     var selectedProducts = []; 
     var productsVars = []; 
    }); 
    $("#table tr.selected").each(function(){ 
     productsVars.push($('td:nth-child(‌​5)', this).html()); //adding attributes in productVars 
     productsVars.push($('td:nth-child(6)', this).html()); 
     productsVars.push($('td:nth-child(8)', this).html()); 
     productsVars.push($('td:nth-child(10)', this).html()); 
     selectedProducts.push(productsVars); //variables for each product, separately 
     productsVars = []; //empty your variables for next product 
    }); 
    var myJsonData=selectedProducts; 
    $.ajax({data:{MyData:myJsonData}}); 
+0

是弗拉基米尔。这是宝贵的意见!你也有完全正确的关于我的意图总价格,总量,但即时通讯几乎全新...我从js脚本获得产品从选择的表行像这样$('。ok')。on('click',函数(e){var selected = []; $(“#table tr.selected”)。each(function(){selected.push($('td:nth-​​child(5)',this).html( )); selected.push($('td:nth-​​child(6)',this).html()); selected.push($('td:nth-​​child(8)',this).html( )); selected.push($('td:nth-​​child(10)',this).html());}); var myJsonData = selected; $。ajax({data:{MyData:myJsonData}} ) – PipBoy2000

+0

到目前为止它的工作原理但是数组“方案”是非常薄弱的​​,正如你上面提到的,我没有任何知识如何正确重构 – PipBoy2000

+1

检出我的答案的更新版本!这将创建二维Json。需要几个编辑你的确切情况.. –