You are here:  » Page contents not advancing


Page contents not advancing

Submitted by ichortwo on Tue, 2011-09-27 06:45 in

Hi there.

I have "lifted" code from several forum topics and generally everything is working.

Except, when trying to post eg 5 records per page, the content for each page is the same first five elements of the .xml file. How can I make it read records for page x thus:

(page number)*(records per page)-(records per page)+1 to (page number)*(records per page) - I hope my maths is correct here!

<?php
  header("Content-Type: text/html; charset=utf-8");
  require("MagicParser.php");
  if (isset($_GET["page"]))
  {
    $page = $_GET["page"];
  }
  else
  {
    $page = 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 $page,$n,$c,$t;
    $c++;
if ($c <= (($page-1)*$n)) return;
    print $record["NAME"] . "<br/>";
print $record["COSTS"] . "<br/>" ;
print "page" . $page. "<br/>";
print "this" . $c . "<br/>"; //added for debug
print "total" . $t; //added for debug
print "<hr />";
    if ($c >= ($page*$n)) return TRUE;
  }
  $xml = file_get_contents("events2.xml");
    MagicParser_parse("events2.xml","myRecordCountHandler","xml|EVENTS/EVENT/");
    MagicParser_parse("events2.xml","myRecordHandler","xml|EVENTS/EVENT/");
  print "<p>";
  if ($page > 1)
  {
    print "<a href='?p=".($page-1)."'>Previous</a>";
  }
  else
  {
    print "<font color='#999999'>Previous</font>";
  }
  print " | ";
  if (($page*$n) < $t)
  {
    print "<a href='?p=".($page+1)."'>Next</a>";
  }
  else
  {
    print "<font color='#999999'>Next</font>";
  }
  print "</p>";
?>

The XML has up to 80 events with about 20 lines of info per event.

Schema extract:

<?xml version="1.0" encoding="UTF-8"?>
<events xsi:schemaLocation="http://app.bullseyehub.com/api/events.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<event xsi:schemaLocation="http://app.bullseyehub.com/api/event.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <id>14008</id>
  <name>Jongleurs @ Sway Early Show</name>
  <promoter>Jongleurs Comedy Live Ltd</promoter>
  <start_time>2011-09-24T16:00:00Z</start_time>
  <end_time>2011-09-24T20:00:00Z</end_time>
  <genre>Comedy</genre>
  <artists>Mandy Knight (compere), Imran Yusuf, Wayne Deakin, Kev Orkian</artists>
  <tags>Club, Fodd &amp; Drink</tags>
  <costs>Comedy Only £17, Comedy with 2 course meal £30</costs>
  <image_url>http://s3.amazonaws.com/public.attachments.app.bullseyehub.com/system/events/14008/original/Jongleurs_tv_LOGO_1_.JPG?1315235916</image_url>
  <ticket_url>http://www.jongleurs.com/show/Covent-Garden-09-24-2011-19-00-00</ticket_url>
  <web_url>http://www.jongleurs.com</web_url>
  <short_description>Jongleurs, Britain’s biggest comedy night comes to Sway. With the country's best comedians it’s a great place to celebrate any occasion from birthday’s, office parties, hen nights and stag do’s.</short_description>
  <long_description>&lt;p&gt;Jongleurs provide the rare opportunity for our guests to eat, drink, laugh and dance under one roof. With shows running every Saturday night, Jongleurs provides the very best in live entertainment. Meal tickets must arrive at 5pm, doors to comedy show open at 6pm, show starts at 7pm and the ticket allows access to Sway after the show has finished, one of London's trendiest nightclubs.&lt;/p&gt;</long_description>
  <venue>
    <id>3337</id>
    <name>Jongleurs @ Sway</name>
    <street>61-65 Great Queen Street, Covent Garden</street>
    <area/>
    <city>London</city>
    <postcode>WC2B 5BZ</postcode>
    <phone/>
    <location>
      <latitude/>
      <longitude/>
    </location>
  </venue>
</event>
</events>

Many thanks

Tim

Submitted by support on Tue, 2011-09-27 08:23

Hi Tim,

I think it's just the variable being used in the Previous / Next links - it should be page= rather than just p=, e.g.

  print "<p>";
  if ($page > 1)
  {
    print "<a href='?page=".($page-1)."'>Previous</a>";
  }
  else
  {
    print "<font color='#999999'>Previous</font>";
  }
  print " | ";
  if (($page*$n) < $t)
  {
    print "<a href='?page=".($page+1)."'>Next</a>";
  }
  else
  {
    print "<font color='#999999'>Next</font>";
  }
  print "</p>";

On a separate note, I also noticed that whilst file_get_contents() is being used to fetch the XML; the calls to MagicParser_parse() still pass the data by filename rather than string:// which you can use once you have fetched the XML to save having to make any subsequent fetch operations - alternative version using string:// would be:

    $xml = file_get_contents("events2.xml");
    MagicParser_parse("string://".$xml,"myRecordCountHandler","xml|EVENTS/EVENT/");
    MagicParser_parse("string://".$xml,"myRecordHandler","xml|EVENTS/EVENT/");

Hope this helps!
Cheers,
David.

Submitted by ichortwo on Tue, 2011-09-27 12:18

Hi David.

1. Doh! Schoolboy error. Problem solved, thanks.
2. Thanks for the tip.

Off to wrestle with stylesheets. Ho hum.

Cheers

Tim

ps. Any hints on some php to run a cron job to get the xml files from the web to the server? Or is that too cheeky?

Submitted by support on Tue, 2011-09-27 12:45

Hi Tim,

If URL wrappers are enabled on your server you can simply use PHP's copy() function to fetch the XML files from the web to your server. The only other caveat is that the destination location on your server is writeable by PHP / whatever user PHP is running as. The easiest way to handle this is simply to create a folder that is writeable by everyone - using your FTP program create a folder e.g. "files" and then right-click on the new folder in the remote window and look for Permissions... or maybe Properties... and then Permissions. Then give WRITE access to all users - Owner / Group / World.

With that in place, the following 1-liner would fetch the remote feed filename.xml to a local copy of the same name:

<?php
  copy
("http://www.example.com/path/to/filename.xml","files/filename.xml");
?>

If URL wrappers are not and cannot be easily enabled, CURL may be an alternative option and is increasing installed on even the most basic shared hosting accounts. Here's the equivalent...

<?php
  $ch 
curl_init("http://www.example.com/path/to/filename.xml");
  
$fh fopen("files/filename.xml","w");
  
curl_setopt($chCURLOPT_HEADER0);
  
curl_setopt($chCURLOPT_FILE$fh);
  
$xml curl_exec($ch);
  
curl_close($ch);
  
fclose($fh);
?>

Cheers,
David.

Submitted by ichortwo on Tue, 2011-09-27 15:15

Hi David

There were a couple a tweaks I had to do which I'd like to post here in case anyone needs/reads anything similar.

My host is 1and1 so I had to dynamite their php settings with a php.ini file. This HAS to be in the same directory as the php file reader with a line

allow_url_fopen = ON

I then had to define the root directory. Code thus:

<?php
  $ch = curl_init("http://www.example.com/hostfile/file.xml");
  $filelocation = $_SERVER['DOCUMENT_ROOT']; //tweak to above code
  $fh = fopen($filelocation."/targetdirectory/filename.xml","w"); //tweak to above code
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_FILE, $fh);
  $xml = curl_exec($ch);
  curl_close($ch);
  fclose($fh);
?>

And hey presto....

Top stuff again, and many thanks.

Tim

Submitted by ichortwo on Wed, 2011-09-28 14:31

Hi David

I'm now trying to implement the code in Joomla using Sourcerer and there seems to be a Joomla issue with Global Variables.

Is it a simple job to migtrate the code to static variables? If you could give me a pointer, I'll try and do the work and then post the results.

Many thanks

Tim

Submitted by support on Wed, 2011-09-28 14:37

Hi Tim,

When running the code within Joomla (I guess you're using a plugin like Direct PHP) the code is executed within a function as it is so all you need to do is just declare each of your variables as global at the top of your code as well as inside each function and that should do the trick...

Cheers,
David.

Submitted by ichortwo on Wed, 2011-09-28 14:48

Hi David

I wish I was as clever as you....

for those following this chain, the code now reads:

<?php
  global $eventpage,$numberonpage,$currentitemnumber,$totalitems;
  print "<table width='640px' border='0'>";
  require("bullseyehub/MagicParser.php");
  if (isset($_GET["eventpage"]))
  {
    $eventpage = $_GET["eventpage"];
  }
  else
  {
    $eventpage = 1;
  }
  $numberonpage = 5; // number on each page
  $currentitemnumber = 0; // current item count
  $totalitems = 0; // total item count
  function myRecordCountHandler($record)
  {
    global $totalitems;
    $totalitems++;
  }
  function myRecordHandler($record)
  {
    global $eventpage,$numberonpage,$currentitemnumber,$totalitems;
    $currentitemnumber++;
if ($currentitemnumber <= (($eventpage-1)*$numberonpage)) return;
    print "<tr><td>Name " . $record["NAME"] . "</td></tr>";
print "<tr><td>Ticket Price " .$record["COSTS"] . "</td></tr>";
print "<tr><td><hr/></td></tr>";
if ($currentitemnumber >= ($eventpage*$numberonpage)) return TRUE;
  }
  $xml = file_get_contents("bullseyehub/events2.xml");
    MagicParser_parse("string://".$xml,"myRecordCountHandler","xml|EVENTS/EVENT/");
    MagicParser_parse("string://".$xml,"myRecordHandler","xml|EVENTS/EVENT/");
  print "</table>";
  print "<p>";
  if ($eventpage > 1)
  {
    print "<a href='?eventpage=".($eventpage-1)."'>Previous</a>";
  }
  else
  {
    print "<font color='#999999'>Previous</font>";
  }
  print " | ";
  if (($eventpage*$numberonpage) < $totalitems)
  {
    print "<a href='?eventpage=".($eventpage+1)."'>Next</a>";
  }
  else
  {
    print "<font color='#999999'>Next</font>";
  }
  print "</p>";
?>

And is invoked in Joomla using Sourcerer with:

{source}
<?php
require_once "location/file.php";
?>
{/source}

Cheers

Tim