1630082
1630082

Reputation: 49

PHP Display an array in table format

I have an array like below, I need to display that array into table. I have tried the function using PHP but it getting rows and cells value, I need three type of values:

  1. Rows name
  2. Columns name
  3. name for the corresponding row and column

Note : I have convert the below array1 into array 2

Array 1 :

Array
(
    [0] => Array ([id] => 1[rows] => R1[columns] => c1[name] => 1)
    [1] => Array([id] => 2[rows] => R1[columns] => c2[name] => 2)
    [2] => Array([id] => 3[rows] => R1[columns] => c3[name] => 3)
    [3] => Array([id] => 4[rows] => R2[columns] => c1 [name] => 1)
    [4] => Array([id] => 5[rows] => R2[columns] => c2[name] => 2)
    [5] => Array([id] => 6[rows] => R2[columns] => c3[name] => 3)
)

Array 2 :

 Array
        (    
          [0] => Array([0] => R1[1] => c1[2] => 1)
          [1] => Array([0] => R1[1] => c2[2] => 2)
          [2] => Array([0] => R1[1] => c3[2] => 3)
          [3] => Array([0] => R2[1] => c1[2] => 1)
          [4] => Array([0] => R2[1] => c2[2] => 2)
          [5] => Array([0] => R2[1] => c3[2] => 3)
        )

Table :

    id | row | column | value
    --------------------------
    1  | r1  |  c1    |  1
    2  | r1  |  c2    |  2
    3  | r1  |  c3    |  3
    4  | r2  |  c1    |  1
    5  | r2  |  c2    |  2
    6  | r2  |  c3    |  3

Function : This function convert the array2 into table

function convetTable($array) {
      $rows = array(); 
      $rows[0] = array(); 
      foreach ($array as $column) { 
        if (!in_array($column[1],$rows[0])) {
            $rows[0][] = $column[1]; 
        }
      }
      $count = count($rows[0]);
      foreach ($array as $row) {
        $rowTtl = $row[0];
        $rowQty = $row[1];
        $rowVal = $row[2];
        if (!isset($rows[$rowTtl])) $rows[$rowTtl] = array_pad(array(),$count,0);
                $rows[$rowTtl][array_search($rowQty,$rows[0])] = $rowVal;
       }
    return $rows;
 }

$table = convetTable($array2);

HTML :

{foreach from=$table item=key key=foo}
                        <tr class="prototype" id="first_row">
                        <td><a href="javascript:void(0);" class="remove">Remove Row</a>
                        <td><input type="text" name="rows[]" value="{$foo}" class="number" /></td>
                        {foreach from=$key item=cell}
                            <td align="center"><input type="text" name="cols[]" value="{$cell}" class="id number" /></td>
                         {/foreach}
                        </tr>
                   {/foreach}

Expected OutPut

0   C1 C2 C3

R1  1  2  3

R2  1  2  3

PS : here c1,c2,c3,1,2,3 are $cell value.

What I try to display $row,$col,$name, but here I'm getting $row and $cell only.

I need another value name for the corresponding row and column.

Upvotes: 0

Views: 4226

Answers (1)

Nishant Solanki
Nishant Solanki

Reputation: 2128

try this..

$count = count($array); //this will give you the count of elements of your array...
echo "<table border="1"><tr><th>ID</th><th>ROWS</th><th>COLUMNS</th><th>NAMES</th></tr>";
for($i=1;$i<=$count;$i++) //loop through the array..
{
    echo "<tr><td>$i</td><td>".$array[$i][0]."</td><td>".$array[$i][1]."</td><td>".$array[$i][2]."</td></tr>";
}

echo "</table>";

Please note this code is to show you how to simplify your massiave code... may be you will need to edit this code to get the desired output...

Upvotes: -1

Related Questions