Page 2 of 2

Posted: Sat Oct 14, 2006 7:55 am
by impulse()
Am I heading in the right direction with this code:

Code: Select all

function start_tag($parser, $name, $attribs) {
          $i = 0;
          $container[$i] = 0;
          return $container;
          return $i;
         }

          function end_tag($parser, $name) {
          $i++;
          return $i;
          }

          function tag_contents($parser, $data) {
          $container[$i] = $data;
          return $container;
 }
Or do I need to do a 180 degree turn?

Posted: Sat Oct 14, 2006 8:17 am
by volka
Your $is and $containers are only valid within the functions, see php.net/language.variables.scope

I don't think you're on the right track.
You should be able to describe the problem (without code) then divide it into smaller parts and start again describing each smaller part until the solution is clear. Try this with your code snippet. What does it do? Why does it do it? Which problem does it solve? Where does it fit?

My solution would probably use http://de2.php.net/manual/en/function.x ... object.php and both start_tag() and end_tag() would make use of the parameter $name. There would be no counting/incrementing of any numbers.

Posted: Wed Oct 18, 2006 9:05 am
by impulse()
I have been working on this project solidly for a week now I have come out with these 2 scripts. Could you point out which script is the more efficiant way of doing the project?

1

Code: Select all

mysql_connect("x", "x", "x");
mysql_select_db("test");

if (!($xmlparser = xml_parser_create())) {   die ("Connect create parser"); }

          function start_tag($parser, $name, $attribs) {
            global $count;
            global $container;

            $container[$count] = array();
          }

          function tag_contents($parser, $data) {
              global $count;
              global $container;

              $container[$count] = $data;
          }




          function end_tag($parser, $name) {
            global $count;

            $count++;
          }


  xml_set_element_handler($xmlparser, "start_tag", "end_tag");

  xml_set_character_data_handler($xmlparser, "tag_contents");

    $filename = "data.xml";
    if (!($fp = fopen($filename, "r"))) { die("Cannot open ". $filename); }

  while ($data = fread($fp, 4096)) {
    $data = eregi_replace(">"."[[]]+"."<","><", $data);
    if (!xml_parse($xmlparser, $data, feof($fp))) {
      $reason = xml_error_string(xml_get_error_code($xmlparser));
      $reason .= xml_get_current_line_number($xmlparser);
      die ($reason);
    }
  }

  xml_parser_free($xmlparser);



$a = 1; $b = 2; $c = 3; $d = 4; # Keeps the array in line

$i = 1; $count = count($container); # Makes the below count is 1;

while ($i <= $count) {
  mysql_query("INSERT INTO test (name, age, height, sex) VALUES ('$container[$a]', '$container[$b]',
                                                                 '$container[$c]', '$container[$d]')");
  $a += 6; $b += 6; $c += 6; $d += 6; # This had to be done to insert the data in the correct place
  $i++;
}
?>


2

Code: Select all

mysql_connect("x", "x", "x");
mysql_select_db("test");

if (!($xmlparser = xml_parser_create())) {   die ("Connect create parser"); }


          function start_tag($parser, $name, $attribs) {
          }


          function tag_contents($parser, $data) {

            static $i = 1; # We don't want this reset each time, do we?

            if ($i >= 5) { $i = 1; } # Stops the amount of columns going out of range

            switch ($i) {
              case 1:
                mysql_query("INSERT INTO test (name) VALUES ('$data')");
                $i++;
                break;

              case 2:
                $query = mysql_query("SELECT * from test");
                $rCount = mysql_num_rows($query);
                $i++;
                  mysql_query("UPDATE test SET age='$data' WHERE id='$rCount'");
                break;

              case 3:
                $query = mysql_query("SELECT * from test");
                $rCount = mysql_num_rows($query);
                $i++;
                  mysql_query("UPDATE test SET height='$data' WHERE id='$rCount'");
                break;

              case 4:
                $query = mysql_query("SELECT * from test");
                $rCount = mysql_num_rows($query);
                $i++;
                  mysql_query("UPDATE test SET sex='$data' WHERE id='$rCount'");
                break;

              default:
                $i = 1;
                break;
            }
          }


          function end_tag($parser, $name) {
          }


  xml_set_element_handler($xmlparser, "start_tag", "end_tag");

  xml_set_character_data_handler($xmlparser, "tag_contents");
$filename = "data.xml";
    if (!($fp = fopen($filename, "r"))) { die("Cannot open ". $filename); }

  while ($data = fread($fp, 4096)) {
    $data = eregi_replace(">"."[[]]+"."<","><", $data);
    if (!xml_parse($xmlparser, $data, feof($fp))) {
      $reason = xml_error_string(xml_get_error_code($xmlparser));
      $reason .= xml_get_current_line_number($xmlparser);
      die ($reason);
    }
  }

  xml_parser_free($xmlparser);

?>

Posted: Thu Oct 19, 2006 2:19 pm
by volka
Sorry, I really don't know what to say about it. But that doesn't matter since you're taking my advices anyway.