You are here:  » Split results


Split results

Submitted by Barbabara on Tue, 2011-03-01 13:21 in

Hi,
I am using a slider script with 3 divs on the same page containing 6 results in each. I am having to call the same xml file 3 times (page 1-3), is it possible to call the xml file with all 18 results only once, and then split the results into the 3 divs containing 6 results each.

<?php
  $resultsa 
FALSE;
  function 
myRecordHandlera($record)
  { global 
$resultsa;
    print 
'#';
   
$resultsa TRUE;
  }
  
$filename "page1";
  
MagicParser_parse($filename,"myRecordHandlera","xml");
  if (!
$resultsa)
  {
    print 
'<p></p>';
  }
?>

<?php
  $resultsb 
FALSE;
  function 
myRecordHandlerb($record)
  { global 
$resultsb;
    print 
'#';
   
$resultsb TRUE;
  }
  
$filename "page2";
  
MagicParser_parse($filename,"myRecordHandlerb","xml");
  if (!
$resultsb)
  {
    print 
'<p></p>';
  }
?>

etc.

Thanks
Barbara

Submitted by support on Tue, 2011-03-01 13:30

Hi Barbara,

Sure - it should just need a counter variable, and then some conditional output of your DIV HTML as required. Consider the following;

<?php
  $counter 
0;
  function 
myRecordHandler($record)
  {
    global 
$counter;
    if (
$counter == 0) print "<div>";
    
// print $record contents here
    
$counter++;
    if (
$counter == 6)
    {
      print 
"</div>";
      
$counter 0;
    }
  }
  
MagicParser_parse($filename,"myRecordHandler","xml|FORMAT/STRING/");
?>

Hope this helsp!
Cheers,
David.

Submitted by Barbabara on Tue, 2011-03-01 14:58

Hi David,

Thank you for your swift response, your suggestion worked perfectly.
The only problem i have now, if results returned are less than 6,12 or 18 the ending will be missing. Any suggestions?

Thanks

Barbara

Submitted by support on Tue, 2011-03-01 15:17

Hi Barbara,

Add at the end of the code (after the call to MagicParser_parse) this code:

  if ($counter < 6)
  {
    print "</div>";
  }

full example:

<?php
  $counter 
0;
  function 
myRecordHandler($record)
  {
    global 
$counter;
    if (
$counter == 0) print "<div>";
    
// print $record contents here
    
$counter++;
    if (
$counter == 6)
    {
      print 
"</div>";
      
$counter 0;
    }
  }
  
MagicParser_parse($filename,"myRecordHandler","xml|FORMAT/STRING/");
  if (
$counter 6)
  {
    print 
"</div>";
  }
?>

Cheers!
David.