You are here:  » Parse multiple columns


Parse multiple columns

Submitted by Tom on Fri, 2012-08-10 15:00 in

Hi David,

The script below generates a simple table, I would like to break the current table (about 100 items) in a multi-column display, eg 4 tables next to each other, floating left. In other words after counting 25 rows the table should be closed and a new one opened with the next 25 items. And so on. I guess I have to use something like if($count%25 == 0) but I am not sure how exactly to do that.

Perhaps you can help. Below is my script. Thanks for the help!

<?php
  
require("MagicParser.php");
  global 
$records;
  
$records=array();
  function 
myRecordHandler($record){
    global 
$records;
    
$records[] = $record;
  }
  
MagicParser_parse("filename.XML","myRecordHandler","xml|ALLITEMS/ITEM/");
  print 
"<table width='225' class='mp-table'>";
  foreach(
$records as $record)
  {
    print 
"<tr>
    <td><img src='/xml/"
.$record["DESTINATION"].".jpg' alt='".$record["DESTINATION"]."' title='".$record["DESTINATION"]."' height='40' width='80' /></td>
    <td>"
.$record["DESTINATION"]."</td>
    <td>"
.$record["PRICE"]."</td>
    </tr>"
;
  }
  print 
"</table>";
?>

Submitted by support on Fri, 2012-08-10 16:05

Hello Tom,

Have a go with something like this:

<?php
  
require("MagicParser.php");
  global 
$records;
  
$records=array();
  function 
myRecordHandler($record){
    global 
$records;
    
$records[] = $record;
  }
  
MagicParser_parse("filename.XML","myRecordHandler","xml|ALLITEMS/ITEM/");
  function 
startTable()
  {
    print 
"<table width='225' class='mp-table'>";
  }
  function 
endTable()
  {
    print 
"</table>";
  }
  
$totalItems count($records);
  
$itemsPerTable 25;
  
$items 0;
  
startTable();
  foreach(
$records as $record)
  {
    print 
"<tr>
    <td><img src='/xml/"
.$record["DESTINATION"].".jpg' alt='".$record["DESTINATION"]."' title='".$record["DESTINATION"]."' height='40' width='80' /></td>
    <td>"
.$record["DESTINATION"]."</td>
    <td>"
.$record["PRICE"]."</td>
    </tr>"
;
    
$items++;
    if (
$items == $itemsPerTable)
    {
      
endTable();
      if (
$items $totalItemsstartTable();
      
$items 0;
    }
  }
  if (
$totalItems $itemsPerTableendTable();
?>

Hope this helps!
Cheers,
David.
--
MagicParser.com

Submitted by Tom on Fri, 2012-08-10 17:36

Brilliant David! Thanks for the prompt and perfect help as always, it's appreciated.

Tom

Submitted by Tom on Fri, 2012-08-10 17:56

David, thanks for the superfast support :-).

It works perfect apart from one thing:

The first table is cutoff correctly at the number specified (eg 25) but the second table is not cutoff and thus longer than the first table (as long as the remaining records).
A minor thing I guess but I am not very good at PHP and can't find the fix for that.

Thanks again, Tom

Submitted by support on Fri, 2012-08-10 19:41

Hi Tom,

$items = 0; missing - corrected above...

Cheers,
David.

Submitted by Tom on Sat, 2012-08-11 07:46

Thanks, works perfect now :-)