2017-10-08 46 views
1

我有這個功能。如何合併這2個函數來顯示錶內

我想轉換2個功能。 我的目標是要有最好的模塊化,並提取特定的信息,如產品的尺寸,重量....包括在我的數據庫中。

1 - 顯示產品頁面中的所有信息

2 - 提取特定信息數據庫,包括(重量,深度,...)

我不知道這是否是做的好方法那。

如果你能幫我解決這個問題,那將會很棒。

原有功能

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>'; 

結果是:

[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 
           ) 

         ) 

       ) 
...  
     ) 

我的功能

// 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>'; 
     } 
     } 
    } 

結果

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

...

我的第二個功能

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']; 
     } 
    } 

結果

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 

....

後,我不知道,如果它是正確的合併這2個功能,在良好的類別中有好的元素。

謝謝

回答

0
function extract($user_data) 
{ 
    .... 
    return $extracted_data; 
} 

function display($extracted_data) 
{ 
    .... 
    return $results; 
} 

echo display(extract($user_input)); 

像這樣的事情?