You are here:  » Magic Parser


Magic Parser

Submitted by sbedigital on Fri, 2020-06-12 00:15 in

Hi Dave,

This may not be the appropriate there please accept my sincere apologies... I understand that PriceTapestry was built on your Magic Pharse. I am in a desperate situation where your Magic Pharser will be really useful to me. Since I own the PriceTapestry, I just wondering is there anyway you can advise me how I can extract magic Pharser related files to be used with a my own application...

Many thanks

Submitted by support on Fri, 2020-06-12 06:48

Hi,

It's in the Price Tapestry distribution as includes/MagicParser.php. The only other files in the Magic Parser distribution are the test scripts but if you've been using the Magic Parser demo tool to generate example code you can just use that "as is"...

Cheers,
David.
--
PriceTapestry.com

Submitted by sbedigital on Tue, 2020-06-30 11:21

Hi Dave,

Thanks for your prompt reply as usual. I had little play around with it. On the basic setup, it works and I have managed go as far as Inserting the data into the database. Now, there few scenarios I need to cater for, I hope you provide me some advice:

1. If I have very large set of feed, how can I limit the number items processed either for display or database entry? For example, say I have a CSV feed of 1000 line, but I want to display only items from 200 to 250, or on another case I want insert 100 items at one per increment Rather than try to insert 1000 at once?

2. Is there a way return the processed array outside of the function?

I hope I have explained myself properly and apologies for taking your time.

Noor

Submitted by support on Tue, 2020-06-30 11:53

Hello Noor,

To skip a record you can just return (FALSE) from your record handler function. Returning TRUE stops the parse so for example to process records 200-250 only you could something like this;

  $count = 0;
  function myRecordHandler($record)
  {
    global $count;
    $count++;
    if ($count < 200) return FALSE;
    // process record here
    if ($count == 250) return TRUE;
  }

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by sbedigital on Tue, 2020-06-30 14:35

Hi Dave thanks again for quick reply. Your answer solve one part of the problem. Could please document how can I create a loop so that it inserts records in batches into the database. For example, I have a feed that has 1000 records, but I want the script to insert records in 10 batches, each batch containing 100 records.

After processing each batch script should pause for x amount of time and then do next batch so on so forth....

Lastly, On a different scenario entirely, after parsing the records, is there anyway to return the record set array so that I can use it outside of the myRecordHandler() function?

Many thanks once again.

Submitted by support on Tue, 2020-06-30 14:58

Hi,

The easiest way to pause after every 100 records would be to incorporate the sleep() into the record handler function; and to access the record set outside of the record handler once the parse has completed you can add the records to a global array, something like this;

  $records = array();
  $count = 0;
  function myRecordHandler($record)
  {
    global $records;
    global $count;
    $records[] = $record;
    // process $record here
    $count++;
    if (!($count % 100))
    {
      sleep(10);
    }
  }
  MagicParser_parse(...);
  // process $records here

Cheers,
David.
--
PriceTapestry.com