0

i have a little program that feeds from two xml files into a Mysql. im having a trouble with a php code

i keep getting this errors:

Notice: Undefined index: e_cod in on line 327

Notice: Undefined index: nombre in on line 328

Notice: Undefined index: titulo in  on line 449

Notice: Undefined index: codbar in  on line 450

Notice: Undefined index: precio in on line 451

Notice: Undefined index: editorial in on line 452

Notice: Undefined index: codbar in on line 453

Notice: Undefined index: autor in on line 454

Now here are some key parts of the file

Line 313 to 338

 public function getmanufacturers()
 {

    $feedurl='http://mysite/manufacturer.xml';
    $xml=simplexml_load_file($feedurl);
    $products =get_object_vars($xml);
    $i=0;
    $manufacturer_array =array();
    foreach($products as $product)
    {
        foreach($product as $productitem)
        {
            $i++;
            $feedproduct = (array)($productitem);
            $e_cod = $feedproduct['e_cod']; //3
            $name = $feedproduct['nombre']; //1
            $manufacturer_array['$e_cod'] = $name;

        }
    }


    return $manufacturer_array;
 }

and my XML from manufacturer.

<?xml version="1.0" standalone="yes"?>
<RECORDS>
<RECORD>
<id>1</id>
<nombre>AMERICAN BOOK STORE</nombre>
<proveedor_cod>0410</proveedor_cod>
<e_cod>0001</e_cod>
<nombrec>ABS</nombrec>
<cambio>2012/3/14</cambio>
</RECORD>
</RECORDS>

again php file from line 415 to 522

            $manufacturer_array= $this->getmanufacturers();



            //inactive features
            $datasource ='TEL';
            $feedurl='http://mysite/libro.xml';
            $xml=simplexml_load_file($feedurl);
            $products =get_object_vars($xml);
            $i=0;

            $raw_products_arr2 =array();
            //$Submitoffset = '894757001973';
            // $Submitoffset = 'FL5018-2';
            //$Submitoffset = '3000000263';
            $gotit =0;
            foreach($products as $product)
            {
                foreach($product as $productitem)
                {

                    $i++;
                    $feedproduct = (array)($productitem);

                    //print_r($feedproduct);
                    //die();
                    Configuration::updateValue('PRODIMPORTER_STAGE',1);
                    if($i<$rowid )
                    {
                        continue;
                    }
                    try
                    {

                         $pname = $feedproduct['titulo']; 
                         $reference = $feedproduct['codbar']; 
                         $price = $feedproduct['precio'];
                         $manufacturer_ref = $feedproduct['editorial']; 
                         $ean13 = $feedproduct['codbar']; 
                         $supplier_reference = $feedproduct['autor']; 


                         if(!isset($feedproduct['id']) || $feedproduct['id']=='')
                        {
                            $this->html .= "<br/>Product id not found :".$pname."<br/>";
                            continue;   
                        }

                         $pid = $feedproduct['id'];

                         $quantity =999;
                         $category_list =array();
                         $parentcatarray =array();
                         $wholesale_price =$price;
                         if($Submitlimit!="0" && $Submitlimit!="" )
                         {


                            if((int)$total >= $Submitlimit)
                            {
                                break;
                            }
                         }

                         $rowid =$rowid + 1;
                         $this->rowid = $rowid;
                         Configuration::updateValue('vcxmlprimporter_counter',$rowid);
                         $total = (int)$rowid-(int)$startrow;
                         if(trim($reference)=="" )
                         {
                            $this->html .= "<br/>Product reference not found :".$pname."<br/>";
                            continue;
                         }

                         Configuration::updateValue('PRODIMPORTER_LASTPROD',$reference);
                         if(trim($pname)=="" )
                         {
                            $this->html .= "<br/>Product name not found :".$pname."<br/>";
                            continue;
                         }

                         if(!isset($price))
                         {
                            $this->html .= "<br/>Price not found :".$pname."<br/>";
                            continue;
                         }


                         if($Submitoffset!="0" && $Submitoffset!="" )
                          {
                              if(trim($reference) == $Submitoffset && $gotit ==0)
                              {
                                    $gotit = 1;
                              }
                                else
                              {
                                    continue;   
                              }
                         }

some of my xml file

    <?xml version="1.0" standalone="yes"?>
<RECORDS>
<RECORD>
<id>3</id>
<titulo>INFORMATICA 1 -COMPETENCIAS+APRENDIZAJE+VIDA</titulo>
<autor>ROMERO</autor>
<editorial>0102</editorial>
<tema>0013</tema>
<codbar>3</codbar>
<isbn></isbn>
<precio>225,0000</precio>
</RECORD>

Now i really think that for some unknown reason the assoc array its not getting the fields of my xml file. or something i dont know really

edit: this differ from other question since using xml that for sure i know it exist. ( check the xml and the PHP)

  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – user10089632 Nov 25 '17 at 00:52
  • edit: this differ from other question since using xml that for sure i know it exist. ( check the xml and the PHP) – juan carlos Nov 25 '17 at 01:13
  • I think the question that I've mentioned is quite general (not necessarily related to an xml file) hence I'm sure you can get a good idea of why that error happens, but thank SO that they precede my comment by *"posssible duplicate of ..."* – user10089632 Nov 25 '17 at 01:17
  • i know that the problem with a undefined variable in an assoc array its that the variable is not declared, or at least php do not recognize it. however if you catch the code the array should be valid. – juan carlos Nov 25 '17 at 01:26
  • @juancarlos You are not getting XML values in PHP variables correctly, that's why the issue is coming. – Amit Gupta Nov 25 '17 at 04:03

2 Answers2

0

You are not getting XML elements values in PHP variables correctly, that's why Undefined index error is coming.

Below code will help you getting XML values correctly from your XML file.

<?php
$xml=simplexml_load_file("http://mysite/manufacturer.xml") or die("Error: 
Cannot create object"); 
$count = count($xml->RECORD);
for($i=0;$i<$count;$i++){
    $id = $xml->RECORD[$i]->id;
    $titulo = $xml->RECORD[$i]->titulo;
    $autor = $xml->RECORD[$i]->autor;
    $editorial = $xml->RECORD[$i]->editorial;
    $tema = $xml->RECORD[$i]->tema;
    $codbar = $xml->RECORD[$i]->codbar;
    $isbn = $xml->RECORD[$i]->isbn;
    $precio = $xml->RECORD[$i]->precio;

    // Execute MySQL Query like Insert related to data received  
}
?>

If you want to add all XML elements in PHP Array Variables for later use then do like below.

<?php
$xml=simplexml_load_file("http://mysite/manufacturer.xml") or die("Error: 
Cannot create object"); 
$count = count($xml->RECORD);
for($i=0;$i<$count;$i++){
    $id[] = $xml->RECORD[$i]->id;
    $titulo[] = $xml->RECORD[$i]->titulo;
    $autor[] = $xml->RECORD[$i]->autor;
    $editorial[] = $xml->RECORD[$i]->editorial;
    $tema[] = $xml->RECORD[$i]->tema;
    $codbar[] = $xml->RECORD[$i]->codbar;
    $isbn[] = $xml->RECORD[$i]->isbn;
    $precio[] = $xml->RECORD[$i]->precio;
}
?> 

Hope it helps!

Amit Gupta
  • 2,771
  • 2
  • 17
  • 31
0

When accessing a SimpleXML element - using [] is used when accessing attributes of an element. So instead you should use ->...

 public function getmanufacturers()
 {
    $feedurl='http://mysite/manufacturer.xml';
    $xml=simplexml_load_file($feedurl);
    $manufacturer_array =array();
    foreach($xml->RECORDS as $product)
    {
        foreach($product->RECORD as $productitem)
        {
            $e_cod = $productitem->e_cod; //3
            $name = $productitem->nombre; //1
            $manufacturer_array['$e_cod'] = $name;
        }
    }
    return $manufacturer_array;
  }

I've also removed some of the coding that wasn't really useful. Changing the foreach to use $xml->RECORDS says to loop through each of the <RECORDS> elements of the XML.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55