You are here:  » Insert data based on record exists

Support Forum



Insert data based on record exists

Submitted by globologic on Sat, 2015-10-03 09:43 in

Hi,

I have a PHP script that uses MagicParser to extra data from a XML feed into a MYSQL DB.

At the moment it insert all records from the XML file however I would like to be able to insert only certains records based on an IF EXISTS. Is this possible?

Basically want to be able to say: IF "event-eventname" contains "xyz" then parse only following records into db.

Below is my PHP file, happy to share XML path Dave if you need it or not.

{code saved}

Submitted by support on Sun, 2015-10-04 09:50

Hi,

Sure - all you need to do is return; from the myRecordHandler function if a test decides the record should not be inserted. For example, to import only records where $jobslist["EVENT-EVENTNAME"] contains "xyz", look for where you have the following code at line 13:

  global $counter;

...and REPLACE with:

  global $counter;
  if (strpos($jobslist["EVENT-EVENTNAME"],"xyz")===FALSE) return;

(on a separate note, I noticed a call to mysql_query($sql); at line 16 that I think may be superfluous and could result in double insertions...)

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

Submitted by globologic on Sun, 2015-11-01 12:02

Thanks Dave,

That works great, is there a way to do it on a wild card so find anything like "%test%"

Rgds

Jason

Submitted by support on Sun, 2015-11-01 14:11

Hi Jason,

Using strpos as above should function as a %xyz% where % is wildcard as it is using strpos (i.e. does needle occur anywhere in haystack) and the === operator for comparison ensuring that 0 (needle occurs at offset zero) and FALSE (doesn't occur anywhere) are differentiated.

What you might want to consider is using stripos() to make it case insensitive also, e.g.

   if (stripos($jobslist["EVENT-EVENTNAME"],"test")===FALSE) return;

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