PAre you good with php arrays and html tables

matthew1471

New member
May 13, 2007
242
0
0
Bournemouth, UK.
If there's someone here who's great with php and putting the data from arrays into html tables, I've got [what I'm sure will be] a quick bit of work for you.

The array contains three sets of daily data which need to be columnised. There may be more rows of data for one date / column than another: in this case, the table needs to render a blank <td></td> in the columns which have no data.

date | data1 | data2 | data3
1235 | a1 | b2 | c
1235 | | b3 | c
1235 | | b4 | c
1235 | | b5 |

1234 | a1 | b2 | c1
1234 | a3 | b4 | c3
1234 | a6 | | c4
1234 | | | c9

(I'm aware the layout above is broken, but I hope it gives you and idea - I'll send you a screen shot of what I need if it'll help).

I've been trying to figure it out, but I'm beaten... I'd rather not spend any more time on it. I'm sure a fresh pair of eyes will have it sorted. If you're interested, pm me. I'm happy to slip you some $ or a one off drip feed blast or something.
 


// $DATA = input data

echo '<table>';

$l = explode("\n", $DATA);
foreach($l as $r) {
$a = explode('|', $r);
echo '<tr><td>'.implode('</td><td>', $a).'</td></tr>';
}
echo '</table>';
 
Cheers for replying both of you.

I think I've mislead you with the dodgy layout thing. The data is already in an array.

The dodgy layout thing was how the finished table needs to be formatted... it shows the rows of the table with some blank cells.
 
If you already know the field names, you can start with:

Code:
echo '<table><tr><th>data1</th><th>data3</th><th>data2</th></tr>';

for the first row.

The issue is that the arrays may be different sizes, and trying to access a non-existent index will give you problems, so you will need to gather some information first and then, you can build a new row from the sets of data. Also, if there is nothing in that particular element, you can still use it in an echo or string concat, as long as you are still in bounds of the array:

Code:
$a=count($data1);
$b=count($data2);
$c=count($data3);

$largest = -1;
//find the largest array
if($a>$b && $a>$c)
   $largest = $a;
else if($c>$a && $c>$b)
   $largest = $c
else
   $largest = $b;

   $rows = array(); // the new array of rows


for($i=0;$i<$largest;$i++)
{
   $rows[$i] = '<tr>';

   //this checks for bounds, if the element exists, but is empty, $i<$a will be true, but will print ''
   if($i<$a)
      $rows[$i]+= '<td>' . $data1[$i].'</td>';
   else //trying to print $data[$i] here will cause error
      $rows[$i] += '<td></td>';

   if($i<$b)
      $rows[$i] += '<td>' . $data2[$i].'</td>';
   else
      $rows[$i] += '<td></td>';

   if($i<$c)
      $rows[$i] += '<td>' . $data3[$i].'</td>';
   else
      $rows[$i] += '<td></td>';


   $rows[$i] += '</tr>';
}

//print the new set of rows
foreach($rows as $row)
{
   echo $row;
}

echo '</table>';

Another solution would be to get the size of the largest array, then pad the ends of the smaller ones with null, then you can avoid the for() loop altogether, but it's not always the best idea to try to alter existing data.
 
That's pretty good code, but I've output tons and tons of tables in PHP and never had to worry about anything like that. If you've got arrays within your array that have different numbers of elements there's some kind of serious logic error somewhere.