You are here:  » Repetitive data


Repetitive data

Submitted by Roberto on Fri, 2009-08-14 18:21 in

Hello.
I use this code to save an XML data in my DB.

function myRecordHandler($record)
  {
    $one = mysql_real_escape_string($record["DATES/FIELDS"]);
    $two = mysql_real_escape_string($record["DATES/FIELDS-ID"]);
    $three = mysql_real_escape_string($record["DATES/FIELDS@1"]);
    $four = mysql_real_escape_string($record["DATES/FIELDS@1-ID"]);
    $query = "INSERT INTO `table` ( `fields` ,`id` ) VALUES ('".$one."','".$two."')";
    MYSQL_QUERY($query);
    $query = "INSERT INTO `table` ( `fields` ,`id` ) VALUES ('".$three."','".$four."')";
    MYSQL_QUERY($query);
    }
  MagicParser_parse("{link saved}","myRecordHandler","xml|CONFIG/");

Where are the code data is small, but my XML contains more than 300 "fields" and "fields-id".

Example:
$ record ["FIELDS"]
$ record ["FIELDS-ID"]
$ record ["FIELDS@1"]
$ record ["FIELDS@1-ID"]
....
$ record ["FIELDS@299"]
$ record ["FIELDS@299-ID"]
$ record ["FIELDS@300"]
$ record ["FIELDS@300-ID"]

how can I change my code to avoid having to write 300 $query ?
under "DATES", are more tags that fields...

XML sample:

<CONFIG>
<DATES>
<FIELDS ID="001">Text1</FIELDS>
....
<FIELDS ID="300">Text300</FIELDS>
</DATES>
<NAMES>
<NAME ID="001">Jhon</NAME>
....
<NAME ID="028">Bob</NAME>
</NAMES>
</CONFIG>

Thanks in advance, and sorry for my english (google translator).

Submitted by support on Fri, 2009-08-14 18:38

Hello,

The best way is to use a counter. Try something like this:

function myRecordHandler($record)
  {
    $i=0;
    foreach($record as $k => $v)
    {
      // create @1, @2 etc.
      if ($i) $postfix = "@".$i;
      // check if the field exists
      if (!isset($record["DATES/FIELDS".$postfix])) break;
      // get the fields and id values
      $fields = mysql_real_escape_string($record["DATES/FIELDS".$postfix]);
      $id = mysql_real_escape_string($record["DATES/FIELDS".$postfix."-ID"]);
      $query = "INSERT INTO `table` ( `fields` ,`id` ) VALUES ('".$fields."','".$id."')";
      mysql_query($query);
      $i++;
    }
  }
MagicParser_parse("{link saved}","myRecordHandler","xml|CONFIG/");

Hope this helps!
Cheers,
David.

Submitted by Roberto on Sat, 2009-08-15 20:34

Thanks for teaching me the way david.
The script works very well.

I reached a point in my XML that is not followed.
and do not like for this example:

<field label="Friends">
<enums class="1">
<enum value="A">Jhon</enum>
<enum value="B">Don</enum>
<enum value="C">Peter</enum>
</enums>
<enums class="2">
<enum value="D">Mary</enum>
<enum value="E">Anna</enum>
<enum value="F">Rose</enum>
</enums>
</field>

I want to keep the three data like this:
Table: Friends
Row1: 1/A/Jhon Row2: 1/B/Don Row3: 1/C/Peter Row4: 2/D/Mary Row5: 2/E/Anna Row6: 2/F/Rose

But I was only with the script you wrote me this:
Row1: A/Jhon Row2: B/Don Row3: C/Peter Row4: D/Mary Row5: E/Anna Row6: F/Rose

I appreciate any help in this.
Apologize, but I am new to this XML & Magic_Parser

Thanks in advance

Submitted by support on Sun, 2009-08-16 11:26

Hello Roberto,

For that style of XML, a different structure would be better; for example:

    $enumsClass = "";
    $enumValue = "";
    $enum = "";
    foreach($record as $k => $v)
    {
      if (strpos($k,"ENUMS"))
      {
        if (strpos($k,"CLASS")) $enumsClass = $v;
        elseif (strpos($k,"VALUE")) $enumValue = $v;
        elseif (strpos($k,"ENUMS/ENUM")) $enum = $v;
      }
      if ($enumsClass && $enumValue && $enum)
      {
         // insert into database here $enumsClass (e.g. 1) $enumValue (e.g. A) $enum (e.g. John)
         // ....
         // reset for next one
         $enumsClass = "";
         $enumValue = "";
         $enum = "";
      }
    }

Hope this helps!
Cheers,
David.