PipBoy2000
PipBoy2000

Reputation: 440

How to make a foreach loop with array data given

I have array data like this taken from .js to JSON, and next to PHP

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
    )
)

In PHP I have something like this

$data = $_POST['MyData'];

And MyData data is get by $data[0]....[39] variable in email template etc. Like here:

$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>'.
(...)
;

How to make a loop (foreach?) handle this table tr row generation? The problem exist because array sometimes has only 1 product (Array 0 to 3), and sometimes may have 40 products... You rather know where is the problem - i don't want to make, a large HTML/PHP email template with hundreds of array value id's ;) I'm learning js, JSON, ajax and PHP so please be patient on my newbie question.

Upvotes: 1

Views: 606

Answers (3)

Naresh Kumar P
Naresh Kumar P

Reputation: 4210

Foreach will appear like this if you have array of values in a variable in 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>

In this within the foreach the <tr> will be repeating till how much values you have in array.

Upvotes: 0

Vladimir
Vladimir

Reputation: 388

Even tough I like Marco Man answer above, I would suggest to reconstruct array from

    [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

To

[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

Not only it solves your problem, it also allows You to work with same data for different products easier. In my experiance I always had some calculations to do for Total price and other things later. Also If You do something like this once as some resort() function, You could use it as one liner anywhere else.

UPDATE #1 And by the way this type of resorting can be done by simple for (which is faster than foreach)

I'd go for

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;
}

UPDATE #2 In your case, I'd go for something like this:

    $('.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}});

Upvotes: 1

SierraOscar
SierraOscar

Reputation: 17637

You could try something like:

$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>';

Upvotes: 2

Related Questions