Viewed   785 times

I would like to display data, two columns per row during my foreach. I would like my result to look like the following:

 <table>
 <tr><td>VALUE1</td><td>VALUE2</td></tr>
 <tr><td>VALUE3</td><td>VALUE4</td></tr>
 <tr><td>VALUE5</td><td>VALUE6</td></tr>
 </table>

Any help would be greatly appreciated.

 Answers

2
$i=0;
foreach ($x as $key=>$value)
  {
  if (fmod($i,2)) echo '<tr>';
  echo '<td>',$value,'</td>';
  if (fmod($i,2)) echo '</tr>';
  $i++;
  }

this will output TR (row) each second time

ps: i haven't tested the code, so maybe you will need to add ! sign before fmod, if it doesn't output TR on first iteration, but on second iteration in the beginning...

Tuesday, August 9, 2022
 
blend
 
2

Maybe try putting each row into an array and then each cell into another array. Hopefully, that will do what you want.

require('simple_html_dom.php');
$html = file_get_html('http://flightplan.romatsa.ro/init/fpl/flightslr/LRCL/');

$table = $html->find('table', 3);
$rowData = array();

foreach($table->find('tr') as $row) {
    // initialize array to store the cell data from each row
    $flight = array();
    foreach($row->find('td') as $cell) {
        // push the cell's text to the array
        $flight[] = $cell->plaintext;
    }
    $rowData[] = $flight;
}

echo '<table>';
foreach ($rowData as $row => $tr) {
    echo '<tr>'; 
    foreach ($tr as $td)
        echo '<td>' . $td .'</td>';
    echo '</tr>';
}
echo '</table>';

Note: this solution requires the simple_html_dom.php library. Get it here!

Wednesday, November 16, 2022
 
1

The only possible way I am aware of for the above scenario is to have this type of php code in your index.php:

<?php
if (pageNotInDatabase) {
   header('Location: ' . $_SERVER["REQUEST_URI"] . '?notFound=1');
   exit;
}

And then slightly modify your .htaccess like this:

Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{QUERY_STRING} !notFound=1 [NC]
RewriteRule ^(.*)$ index.php?p=$1 [NC,L,QSA]

That way Apache will show default 404 page for this special case because of extra query parameter ?notFound=1 added from php code and with the negative check for the same in .htaccess page it will not be forwarded to index.php next time.

PS: A URI like /foo, if not found in database will become /foo?notFound=1 in the browser.

Tuesday, October 11, 2022
 
1

https://plnkr.co/edit/umL80bh0WKr8aPEueCxZ?p=preview

You can create pipe to get pairs values:

@Pipe({ name: 'pairs' })
export class PairsPipe implements PipeTransform {
  transform(array) {
    return array.reduce((result, item, index) => (
      index % 2 ? result : [...result, [item, array[index + 1]]]
    ), []);
  }
}

add pipe to Module:

@NgModule({
  imports: [ BrowserModule ],
  declarations: [ App , PairsPipe],
  bootstrap: [ App ]
})

and use in *ngFor:

 <tr *ngFor="let item of data | pairs">
Tuesday, December 6, 2022
 
1

I use the 'read ahead' technique for processing nested loops. It does mean that 'foreach' loops cannot be used as the next record must be read as soon as the current one has been processed. Basically, the last action you do in the loop is read the next record as you are setting it up for the next iteration. Note, you never test when to print a record as that is decided by the structure of the groups. The code loops are the same as the structure of the groups in the data

A 'group' is all the records with the same id.

I assume that the 'content' and 'act' are identical for each entry in the group.

Edited to add 'rowspan' attributes to the appropriate 'td' tags. I suspect css may be easier at this point.

The issue is that the group cannot be displayed until it is known how many entries are in it.

So, i 'buffer' all the records belonging to a group in an array. at the end of the group, it is displayed with the appropriate 'rowspan' attributes in the html.

It is tested on PHP 5.3.18. It includes test data.

<?php /* Q24028866 */
$testData = array(array('id' => 2, 'date' => '05/13/2014', 'content' => 'some contents 2', 'act' => 'act1 act2 act3'),
                  array('id' => 2, 'date' => '05/28/2014', 'content' => 'some contents 2',  'act' => 'act1 act2 act3'),
                  array('id' => 7, 'date' => '06/04/2014', 'content' => 'some contents 7',  'act' => 'act1 act2 act3'),
                  array('id' => 8, 'date' => '06/08/2014', 'content' => 'some contents 8',  'act' => 'act1 act2 act3'),
                  array('id' => 8, 'date' => '06/09/2014', 'content' => 'some contents 8',  'act' => 'act1 act2 act3'));
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<table border='1'>
<thead><th>Date</th><th>Content</th><th>Act</th></thead>
<?php
// use 'read ahead' so there is always a 'previous' record to compare against...
$iterContents = new ArrayIterator($testData);
$curEntry = $iterContents->current();

while ($iterContents->valid()) { // there are entries to process

    $curId = $curEntry['id'];

    // buffer the group to find out how many entries it has...
    $buffer = array();
    $buffer[] = $curEntry;

    $iterContents->next(); // next entry - may be same or different id...
    $curEntry = $iterContents->current();

    while ($iterContents->valid() && $curEntry['id'] == $curId) {  // process the group...
        $buffer[] = $curEntry; // store all records for a group in the buffer

        $iterContents->next(); // next entry - may be same or different id...
        $curEntry = $iterContents->current();
    }

     // display the current group in the buffer...
     echo '<tr>';
     echo '<td>', $buffer[0]['date'], '</td>';
     $rowspan = count($buffer) > 1 ? ' rowspan="'. count($buffer) .'"' : '';
     echo '<td', $rowspan, '>', $buffer[0]['content'], '</td>',
           '<td', $rowspan, '>', $buffer[0]['act'], '</td>';
     echo '</tr>';
     for($i = 1; $i < count($buffer); $i++) {
          echo '<tr><td>', $buffer[$i]['date'], '</td>';
          echo '</tr>';
     }
} ?>
</table>
</body>
</html>
Sunday, October 2, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :