duanlu1908 2017-10-08 13:49
浏览 22

如何合并这两个函数来显示表内

I have this function.

I am trying to convert in 2 functions. My objective is to have the best modularity and also extract a specific information like the dimension of the product, weight .... to include inside my DB.

1 - Display all information inside product page

2 - Extract specific information to include in DB (weight, depth, ...)

I don't know if it's the good way to do that.

it would be great if you can help me to resolve this element.

The original function

 function icecat_to_array($data = array())
  {
    // Extract data array
    extract($data);

    if(isset($ean)){ $url = 'http://' . $username . ':' . $password . '@data.Icecat.biz/xml_s3/xml_server3.cgi?ean_upc=' . $ean . ';lang=' . $language . ';output=productxml'; }

// Get data
$xml = @file_get_contents($url,false,stream_context_create(array('http' => array('ignore_errors' => true))));

    // Load into Simple XML Element
    $xml = new SimpleXMLElement($xml);

    $spec_group         = $xml->xpath("/ICECAT-interface/Product/CategoryFeatureGroup");
    $spec_item          = $xml->xpath("/ICECAT-interface/Product/ProductFeature");

    // Set specification groups
    foreach($spec_group as $group)
    {
      $p['spec'][(int)$group[0]['ID']]['name'] = (string)$group->FeatureGroup->Name[0]['Value'];
    }

    // Set specifications
    foreach($spec_item as $item)
    {
      if($item[0]['Value'] != 'Icecat.biz')
      {
        $p['spec'][(int)$item[0]['CategoryFeatureGroup_ID']]['features'][(int)$item->Feature->Name[0]['ID']]['name'] = (string)$item->Feature->Name[0]['Value'];
        $p['spec'][(int)$item[0]['CategoryFeatureGroup_ID']]['features'][(int)$item->Feature->Name[0]['ID']]['value'] = (string)$item[0]['Value'];
        $p['spec'][(int)$item[0]['CategoryFeatureGroup_ID']]['features'][(int)$item->Feature->Name[0]['ID']]['sign'] = (string)$item->Feature->Measure->Signs->Sign;
        $p['spec'][(int)$item[0]['CategoryFeatureGroup_ID']]['features'][(int)$item->Feature->Name[0]['ID']]['pres_value'] = (string)$item[0]['Presentation_Value'];
      }
    }

    // Remove empty specification groups
    foreach($p['spec'] as $key=>$value)
    {
      if(!isset($value['features'])){
        unset($p['spec'][$key]);
      }
    }
  }

  // Get Icecat data in array by EAN number
  $data = array(
    'ean'       => '4712900236903',
    'language'  => 'EN',
    'username'  => 'openIcecat-xml',
    'password'  => 'freeaccess'
  );
  $data = icecat_to_array($data);
  echo '<pre>'.print_r($data,TRUE).'</pre>';

the result is :

[spec] => Array
        (
            [35] => Array
                (
                    [name] => Processor
                    [features] => Array
                        (
                            [1036524] => Array
                                (
                                    [name] => Processor frequency
                                    [value] => 2.6
                                    [sign] => GHz
                                    [pres_value] => 2.6 GHz
                                )

                            [11271] => Array
                                (
                                    [name] => Processor family
                                    [value] => Intel Core i7-6xxx
                                    [sign] => 
                                    [pres_value] => 6th gen Intel® Core™ i7
                                )
.....

                        )

                )
[108] => Array
                (
                    [name] => Weight & dimensions
                    [features] => Array
                        (
                            [1525] => Array
                                (
                                    [name] => Weight
                                    [value] => 2591
                                    [sign] => g
                                    [pres_value] => 2.59 kg
                                )

                            [5143] => Array
                                (
                                    [name] => Width
                                    [value] => 384.5
                                    [sign] => mm
                                    [pres_value] => 384.5 mm
                                )

                            [5145] => Array
                                (
                                    [name] => Depth
                                    [value] => 256.9
                                    [sign] => mm
                                    [pres_value] => 256.9 mm
                                )

                            [117745] => Array
                                (
                                    [name] => Height (front)
                                    [value] => 32.75
                                    [sign] => mm
                                    [pres_value] => 3.27 cm
                                )

                            [117763] => Array
                                (
                                    [name] => Height (rear)
                                    [value] => 34.75
                                    [sign] => mm
                                    [pres_value] => 3.48 cm
                                )

                        )

                )
...       
       )

My functions

// display the options category like Processor, Dimensions
    public function getProductFeature11() {
      $xml = $this->getSearchProductEanXML();
      $category_feature_group = $xml->xpath("//CategoryFeatureGroup");

      foreach($category_feature_group as $item) {
        $test = new SimpleXMLElement($item->asXML());

        $feature_group = $test->xpath("//FeatureGroup");

        foreach($feature_group as $item1) {
          var_dump($feature_group);
          $test1 = new SimpleXMLElement($item1->asXML());
          $name = $test1->Name['ID'] . ' ' . $test1->Name['Value'];
          echo $name . '<br>';
        }
      }
    }

result

1222500 Processor
1222498 Hard drive
4863 Memory
4865 Optical drive
4867 Display
4977 Audio
4995 Ports & interfaces
5023 Weight & dimensions

...

My second function

 public function getProductFeature1() {
      $xml = $this->getSearchProductEanXML();
      $ProductFeature = $xml->xpath("//ProductFeature");

      foreach($ProductFeature as $item2) {

        $test2 = new SimpleXMLElement($item2->asXML());
        $test2 = $item2->attributes();
        echo '<br>' . $test2['CategoryFeature_ID'] .  ' ' . $test2['Name'] . ' ' . $test2['Value'];
      }
    }

result

29776 Black
101037 Notebook
4692 Clamshell
59777 Gaming
2787 15.6
9350 1920 x 1080
36303 N
36493 Y
149038 Full HD
99902 Matt
16353 16:9
73715 2.6

....

after I don't know to merge this 2 functions if it's correct and to have the good element inside the good categories.

Thank you

  • 写回答

1条回答 默认 最新

  • duanan1228 2017-10-08 14:21
    关注
    function extract($user_data)
    {
        ....
        return $extracted_data;
    }
    
    function display($extracted_data)
    {
        ....
        return $results;
    }
    
    echo display(extract($user_input));
    

    Something like this?

    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能