You are here:  » How to generate 'n' element per page?


How to generate 'n' element per page?

Submitted by trypode8 on Wed, 2009-02-18 15:45 in

Hi,

I'm a new user of Magic Parser and I'm french (I need to precise it if you don't understand my English).
I'm actually working on a method to create php pages with some info on it.

My XML file is here:

http://www.trypode.net/xmltest/xml/xml_dedietrich.xml

With elements:

<item>
<type_appareil></type_appareil>
<reference_commercial></reference_commercial>
<image></image>
<link></link>
<titre_argu_1></titre_argu_1>
<argu_1></argu_1>
<titre_argu_2>Commandes électroniques</titre_argu_2>
<argu_2></argu_2>
<titre_argu_3></titre_argu_3>
<argu_3></argu_3>
</item>

Automatic generating PHP code using your tool gave me this:

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($record)
  {
    
// This is where you write your code to process each record, such as loading a database
    // You can display the record contents using PHP's internal print_r() function:
    
print_r($record);
    
// The following code will print out each field in your sample data:
    
print $record["ITEM"];
    print 
$record["TYPE_APPAREIL"];
    print 
$record["REFERENCE_COMMERCIAL"];
    print 
$record["IMAGE"];
    print 
$record["LINK"];
    print 
$record["TITRE_ARGU_1"];
    print 
$record["ARGU_1"];
    print 
$record["TITRE_ARGU_2"];
    print 
$record["ARGU_2"];
    print 
$record["TITRE_ARGU_3"];
    print 
$record["ARGU_3"];
  }
  
MagicParser_parse("http://www.trypode.net/xmltest/xml/xml_dedietrich.xml","myRecordHandler","xml|RSS/CHANNEL/ITEM/");
?>

It's working. But before generating something like this (using CSS and ):

http://www.trypode.net/xmltest/index_dedietrich.php

I would like to know how I can insert a code to divide my php page with 'n' element into 'n' pages.
For exemple:

/index_dedietrich.php?p=1 with 5 and 'next' (then 'previous' button) for p=2 with the 5 next until the end of the xml file.

Thank you,

Jean-Nicolas.

Submitted by support on Wed, 2009-02-18 16:09

Hello Jean-Nicolas,

It is quite straight forward to do, but probably easiest to show you the code rather than describe it! Here is an example with your RSS, just displaying the print_($record) and a <hr /> which you can replace with your code to generate the HTML.

<?php
  header
("Content-Type: text/html; charset=utf-8");
  require(
"MagicParser.php");
  
$p $_GET["p"];
  if (!
$p$p 1;
  
$n 5// number on each page
  
$c 0// current item count
  
function myRecordHandler($record)
  {
    global 
$p,$n,$c;
    
$c++;
    if (
$c <= (($p-1)*$n)) return;
    
print_r($record);
    print 
"<hr />";
    if (
$c >= ($p*$n)) return TRUE;
  }
  
MagicParser_parse("http://www.trypode.net/xmltest/xml/xml_dedietrich.xml","myRecordHandler","xml|RSS/CHANNEL/ITEM/");
  print 
"<p>";
  if (
$p 1)
  {
    print 
"<a href='?p=".($p-1)."'>Previous</a>";
  }
  else
  {
    print 
"<font color='#999999'>Previous</font>";
  }
  print 
" | ";
  print 
"<a href='?p=".($p+1)."'>Next</a>";
  print 
"</p>";
?>

Hope this helps!
Cheers,
David.

Submitted by trypode8 on Wed, 2009-02-18 16:21

Hi David,

First: thank you for your kick answer, you're so fast!
Second: It seems to work very well, thank you! I'll update my code now and be back if I have few more questions.

Regards,

Jean-Nicolas.

Submitted by trypode8 on Wed, 2009-02-18 17:05

Hi again David,

It seems that the code will generate infinite 'next' pages if it has finished to read the xml file.
After the last item, It will continue to produce 'next' blank pages.

It is possible to disable the 'next' button after it reads the last entry?

And my second question is:

How to easily change the aspect of the page?

For the moment it displays:

Array ( [ITEM] => [TYPE_APPAREIL] => ... [REFERENCE_COMMERCIAL] => ... [IMAGE] => ... [LINK] => ... [TITRE_ARGU_1] => ... [ARGU_1] => .... [TITRE_ARGU_2] => ... [ARGU_2] => ... [TITRE_ARGU_3] => ... [ARGU_3] => ... )

I want to hide all the 'Array ( [ITEM] => [TYPE_APPAREIL] =>' and display only the information of my xml (the) '...' and print in on a table for example, with html code (to display one picture [IMAGE] for example).

Thank you again,

Jean-Nicolas.

Submitted by support on Wed, 2009-02-18 17:18

Hi Jean-Nicolas,

Sure - this is made slightly more complicated because to stop Next showing for the last page you have to know how many records there are; so this means parsing the file twice. To make sure it doesn't contact the server twice, I have loaded the RSS into a string in this version with file_get_contents(), and then the string is parsed twice. This should work on most installations of PHP.

All of the fields being printed out is just the result of print_r($record);, but in your main application you would generate HTML from the individual fields. So, to just show the image, you would generate the image HTML using, for example $record["IMAGE"]. I have done both of these in this example...

<?php
  header
("Content-Type: text/html; charset=utf-8");
  require(
"MagicParser.php");
  
$p $_GET["p"];
  if (!
$p$p 1;
  
$n 5// number on each page
  
$c 0// current item count
  
$t 0// total item count
  
function myRecordCountHandler($record)
  {
    global 
$t;
    
$t++;
  }
  function 
myRecordHandler($record)
  {
    global 
$p,$n,$c;
    
$c++;
    if (
$c <= (($p-1)*$n)) return;
    print 
"<img src='".$record["IMAGE"]."' />";
    print 
"<hr />";
    if (
$c >= ($p*$n)) return TRUE;
  }
  
$xml file_get_contents("http://www.trypode.net/xmltest/xml/xml_dedietrich.xml");
  
MagicParser_parse("string://".$xml,"myRecordCountHandler","xml|RSS/CHANNEL/ITEM/");
  
MagicParser_parse("string://".$xml,"myRecordHandler","xml|RSS/CHANNEL/ITEM/");
  print 
"<p>";
  if (
$p 1)
  {
    print 
"<a href='?p=".($p-1)."'>Previous</a>";
  }
  else
  {
    print 
"<font color='#999999'>Previous</font>";
  }
  print 
" | ";
  if ((
$p*$n) < $t)
  {
    print 
"<a href='?p=".($p+1)."'>Next</a>";
  }
  else
  {
    print 
"<font color='#999999'>Next</font>";
  }
  print 
"</p>";
?>

...but I noticed (and then checked the code but it looks OK!) it seems every 5 records are the same in this file..!

Hope this helps!
Cheers,
David.

Submitted by trypode8 on Thu, 2009-02-19 09:08

Hi David,

That's perfect, thank you again!

Yes, for my test, it's 5 records loop on the xml, that's why each page is similar...

Regards,
Jean-Nicolas.

Submitted by trypode8 on Wed, 2009-03-04 11:06

Dear David,

My pages are almost done, nevertheless, I have one more question...

First, the last version of my code:

<?php
  require("MagicParser.php");
  $p = $_GET["p"];
  if (!$p) $p = 1;
  $n = 5; // number on each page
  $c = 0; // current item count
  $t = 0; // total item count
  function myRecordCountHandler($record)
  {
    global $t;
    $t++;
  }
  function myRecordHandler($record)
  {
global $p,$n,$c;
    $c++;
    if ($c <= (($p-1)*$n)) return;
if ($record["MARQUE"] == "DE DIETRICH" && $record["TYPE_APPAREIL"] == "H_B") {
print "<table border=\"0\"><tr><td rowspan=\"2\" align=\"center\" valign=\"middle\" width=\"220\"><a href=\"http://www.dedietrich-electromenager.fr/\" /><img border=\"0\" alt=".$record["TYPE_APPAREIL"].'&nbsp;'.$record["REFERENCE_COMMERCIAL"]." src='http://www.catalogue-fb.com/CatalogOnline/img/catalog/".$record["IMAGE"]."'/></a></td>
    <td width=\"480\"><div align=\"left\"><b><a href=\"http://www.dedietrich-electromenager.fr/\" />".$record["TYPE_APPAREIL"]." ".$record["REFERENCE_COMMERCIAL"]."</a></b></div></td>
  </tr>
  <tr>
    <td align=\"left\" valign=\"top\"><div align=\"left\"><p><b>".$record["TITRE_ARGU_1"]."</b><br>".$record["ARGU_1"]."<br><br><b>".$record["TITRE_ARGU_2"]."</b><br>".$record["ARGU_2"]."<br><br><b>".$record["TITRE_ARGU_3"]."</b><br>".$record["ARGU_3"]."</p></div></td></tr></table><br>";
print "<hr />";
}
    if ($c >= ($p*$n)) return TRUE;
  }
  $xml = file_get_contents("http://www.trypode.net/xmltest/xml/xml_fagorbrandt_france.xml");
  MagicParser_parse("string://".$xml,"myRecordCountHandler","xml|ITEMS/ITEM/");
  MagicParser_parse("string://".$xml,"myRecordHandler","xml|ITEMS/ITEM/");
  print "<p align='center'>";
  if ($p > 1)
  {
    print "<a href='?p=".($p-1)."'><< Page précédente</a>";
  }
  else
  {
    print "<font color='#999999'><< Page précédente</font>";
  }
  print " | ";
  if (($p*$n) < $t)
  {
    print "<a href='?p=".($p+1)."'>Page suivante >></a>";
  }
  else
  {
    print "<font color='#999999'>Page suivante >></font>";
  }
  print "</p>";
?>

I am working on a huge single XML file {link saved} with some elements and I would like to cut that in some pages.

For example only products from the brand ("MARQUE") "DE DIETRICH" and a type of products ("TYPE_APPAREIL") "H_B" like this:

if ($record["MARQUE"] == "DE DIETRICH" && $record["TYPE_APPAREIL"] == "H_B") {
print "

It's working but there are 2 problems:

- First, the 'Next page >>' (Page suivante >>) loops infinite.

- Second, if I want to see only the H_B products, it will appear first on page 18. It's like it cuts the whole XML in n pages of 5 elements but never put the hidden elements away.

I don't know if I'm very clear, you can see the result here:

{link saved}

Thank you,

Best regards,

Jean-Nicolas.

Submitted by support on Wed, 2009-03-04 11:15

Hi Jean-Nicolas,

It should be straight forward to line them up, all you need to do is add the same condition to the record counter, and then only increase the counter in the main record handler when you are displaying a record you want. Have a go with this:

<?php
  
require("MagicParser.php");
  
$p $_GET["p"];
  if (!
$p$p 1;
  
$n 5// number on each page
  
$c 0// current item count
  
$t 0// total item count
  
function myRecordCountHandler($record)
  {
    global 
$t;
    if (
$record["MARQUE"] == "DE DIETRICH" && $record["TYPE_APPAREIL"] == "H_B")
    {
      
$t++;
    }
  }
  function 
myRecordHandler($record)
  {
    global 
$p,$n,$c;
    if (
$record["MARQUE"] == "DE DIETRICH" && $record["TYPE_APPAREIL"] == "H_B")
    {
      
$c++;
      if (
$c <= (($p-1)*$n)) return;
      print 
"<table border=\"0\"><tr><td rowspan=\"2\" align=\"center\" valign=\"middle\" width=\"220\"><a href=\"http://www.dedietrich-electromenager.fr/\" /><img border=\"0\" alt=".$record["TYPE_APPAREIL"].'&nbsp;'.$record["REFERENCE_COMMERCIAL"]." src='http://www.catalogue-fb.com/CatalogOnline/img/catalog/".$record["IMAGE"]."'/></a></td>
      <td width=\"480\"><div align=\"left\"><b><a href=\"http://www.dedietrich-electromenager.fr/\" />"
.$record["TYPE_APPAREIL"]." ".$record["REFERENCE_COMMERCIAL"]."</a></b></div></td>
      </tr>
      <tr>
      <td align=\"left\" valign=\"top\"><div align=\"left\"><p><b>"
.$record["TITRE_ARGU_1"]."</b><br>".$record["ARGU_1"]."<br><br><b>".$record["TITRE_ARGU_2"]."</b><br>".$record["ARGU_2"]."<br><br><b>".$record["TITRE_ARGU_3"]."</b><br>".$record["ARGU_3"]."</p></div></td></tr></table><br>";
      print 
"<hr />";
      if (
$c >= ($p*$n)) return TRUE;
    }
  }
  
$xml file_get_contents("http://www.trypode.net/xmltest/xml/xml_fagorbrandt_france.xml");
  
MagicParser_parse("string://".$xml,"myRecordCountHandler","xml|ITEMS/ITEM/");
  
MagicParser_parse("string://".$xml,"myRecordHandler","xml|ITEMS/ITEM/");
  print 
"<p align='center'>";
  if (
$p 1)
  {
    print 
"<a href='?p=".($p-1)."'><< Page précédente</a>";
  }
  else
  {
    print 
"<font color='#999999'><< Page précédente</font>";
  }
  print 
" | ";
  if ((
$p*$n) < $t)
  {
    print 
"<a href='?p=".($p+1)."'>Page suivante >></a>";
  }
  else
  {
    print 
"<font color='#999999'>Page suivante >></font>";
  }
  print 
"</p>";
?>

If this doesn't make the "Page suivante" work properly, let me know and I'll look into that further...

Cheers!
David.

Submitted by trypode8 on Wed, 2009-03-04 12:47

Hi David,

It's fully working now!
A real great big thank you for your support, so fast and clear!

Jean-Nicolas.