Php array

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Michael vanDijk

Michael vanDijk

17/05/2020 17:27:42
Quote Anchor link
Hoi

ben al tijdje aan het stoeien met een array waar ik een variable uit wil halen maar om een of anderre reden maak ik een domme fout geloof ik..
ik heb dus uit variations de producten nodig. en dan het liefst de eancode.

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
Array
(
    [product_id] => 314955
    [sku] => 0401103078
    [name] => Bomber cardigan Bethany
    [set] => 41
    [type] => configurable
)
Array
(
    [product_id] => 314955
    [sku] => 0401103078
    [set] => 41
    [type] => configurable
    [shoot_photo] =>
    [customer_group_id] => 5
    [customer_group_price] => Array
        (
            [price_id] => 107292129
            [website_id] => 12
            [all_groups] => 0
            [cust_group] => 5
            [price] => 38.4000
            [website_price] => 38.4000
            [customer_group_code] => PL0
            [special_price] =>
        )

    [customer_group_price_rrp] => Array
        (
            [price_id] => 107292130
            [website_id] => 12
            [all_groups] => 0
            [cust_group] => 12
            [price] => 99.95
            [website_price] => 99.95
            [customer_group_code] => PL0_RRP
        )

    [model] => 0401103078
    [name] => Bomber cardigan Bethany
    [season_usage_label] => Winter
    [image] => http://cp.hv-polo.com/media/catalog/product/0/4/0401103078-stogre.jpg
    [variations] => Array
        (
            [0401103078-5001-2XL] => Array
                (
                    [color] => Navy
                    [shirt_size] => 2XL
                    [is_active] => 1
                    [is_saleable] => 1
                    [name] => Bomber cardigan Bethany Navy XXL
                    [eancode] => 8718563881661
                    [stock] => 24
                    [image] => http://cp.hv-polo.com/media/catalog/product/b/o/bomber_cardigan_bethany_navy_l_1.jpg
                )

            [0401103078-5001-3XL] => Array
                (
                    [color] => Navy
                    [shirt_size] => 3XL
                    [is_active] => 1
                    [is_saleable] => 1
                    [name] => Bomber cardigan Bethany Navy XXXL
                    [eancode] => 8718563881678
                    [stock] => 6
                    [image] => http://cp.hv-polo.com/media/catalog/product/b/o/bomber_cardigan_bethany_navy_l_1.jpg
                )




        )

)

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
dit is mijn stukje script
public function updateProduct($data) {
        $option = $this->db->query("SELECT * FROM " . DB_PREFIX . "power_options_data WHERE pwr_sku = '" . $this->db->escape($data['sku']) . "' LIMIT 1");

        //print $data;
        print "<br />data sku ".$data['sku']."<br />";
        print "data sku ".$data['eancode']."<br />";
        print $data['variations']['eancode'];


        if ($option->num_rows) {
            $pwr_id = $option->row['pwr_id'];

            $this->db->query("UPDATE " . DB_PREFIX . "power_options_data SET pwr_quantity = '" . (int)$data['stock'] . "' WHERE eancode = '" . (int)$data['eancode'] . "'");
            //print_r(" . (int)$data['stock_qty'] . ")
        }
        
    
        
    }
Gewijzigd op 17/05/2020 17:28:09 door Michael vanDijk
 
PHP hulp

PHP hulp

08/11/2024 22:00:29
 
- Ariën  -
Beheerder

- Ariën -

17/05/2020 17:36:11
Quote Anchor link
Variantions is een array, dus je zult wel moeten aangeven welke serienummer voor de variation je moet hebben. Of als je ze allemaal wilt gebruiken, dan moet je met foreach() de boel doorlopen.

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
<?php
// single item:
echo $data['variations']['0401103078-5001-2XL']['eancode'];
?>


Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
<?php
// foreach om alles door te lo(o)pen.
foreach($data['variations'] as $variation) {
    echo "<pre>".print_r($variation)."</pre>"; // ter debugging om te kijken wat er in de array zit.
    echo $variation['eancode'];
}

?>
Gewijzigd op 17/05/2020 17:37:00 door - Ariën -
 
Michael vanDijk

Michael vanDijk

17/05/2020 18:00:41
Quote Anchor link
Hier kan ik inderdaad een stuk mee verder, helaas krijg ik het nog niet helemaal lekker werkend met wat het moet doen.
ik ga nog even stoeien bedankt in ieder geval

Toevoeging op 17/05/2020 18:06:49:

ik heb een stukje class

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

public function update() {
        $this->client = new SoapClient('http://cp.hv-polo.com/api/soap/?wsdl');
        $this->session_id = $this->client->login($this->user, $this->key);
        try {
            $products = $this->client->call($this->session_id, 'ipad_api.customerproducts', [null, $this->DefaultCategoryID, $this->storeID]);
            if ($products) {
                $this->load->model('api/hvpolo');
                    $counter = 0;
                    $new = '';
                    $duration = 0;
                    $i=0;
                    $max_aantal_producten_toevoegen = 1;
                    $max_products = $max_aantal_producten_toevoegen;

                foreach ($products as $product) if ($i < $max_products) {
                if (!$this->model_api_hvpolo->checkProduct2($product)) {
                    $details = $this->client->call($this->session_id, 'ipad_api.customerproduct', [$product['product_id'], $this->storeID]);
                    
                if ($details) {
                if (isset($details['customer_group_price']['special_price'])) {
                }
                                    
                            $starttime = microtime(true);
                            $this->model_api_hvpolo->updateProduct($details);                            
                            $endtime = microtime(true);                            
                            $duration += ($endtime - $starttime);
                            $counter++;
                        //    echo "<pre>";
                         //    print_r($product);
                         //    print_r($details);
                         //    echo "</pre>";
                            $new .= ''. $details['name']. '\r\n';
                            $new .= '<br />';
                            $i+=1;
                        }
                    }
                }
                    if ($new) {
            //echo "dit is gedaan?";
                if ($counter > 0) {
                            $this->db->query("INSERT INTO " . DB_PREFIX . "api_log SET type = 'update', aantal_producten = '". $new ."',  duration = '" . $duration . "', datetime = NOW(), site = 'Hv Polo'");
                            }
                                
                                // Duration to DB table
                                $this->db->query("INSERT INTO " . DB_PREFIX . "api_log SET type = 'update', aantal_producten = '". $new ."',  duration = '" . $duration . "', datetime = NOW(), site = 'Hv Polo'");
                            }
                }
                echo "dit is 2x";
        } catch(Exception $e) {
            foreach ($this->emails as $email) {
                mail($email, 'HV Polo API error', $e->getMessage());
            }
        }
    }

die maakt gebruik van deze twee classes
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public function checkProduct2($data) {
        $search = '';
        if ($data['sku']) {
            $search .= "pwr_sku = '" . $this->db->escape($data['sku']) . "'";
            //print_r($search);
        }
        if ($search) {
            $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "power_options_data WHERE " . $search . " LIMIT 1");
            if ($query->num_rows) {
                return $query->row['pwr_id'];
                print ("<pre>");
                print ("power id ".$pwr_id."");
                print ("</pre>");
            }
        }
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_description WHERE name = '" . $this->db->escape(trim($data['name'])) ."' LIMIT 1");

        if ($query->num_rows) {
            return $query->row['product_id'];
        }
        return false;
    }

Nou wil ik dus een class hebben dat hij de quantity update bij de variaties
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public function updateProduct($data) {
        $option = $this->db->query("SELECT * FROM " . DB_PREFIX . "power_options_data WHERE pwr_sku = '" . $this->db->escape($data['sku']) . "' LIMIT 1");

        //print $data;
    //    print "<br />data sku ".$data['sku']."<br />";
        //print "data sku ".$data['eancode']."<br />";
        //print "".$data['variations']['eancode']."<br />";
//echo "data variations ". $data['variations']['0401103078-5001-2XL']['eancode'] ."";
                //    foreach om alles door te lo(o)pen.
                    foreach($data['variations'] as $variation) {
                        $this->db->query("UPDATE " . DB_PREFIX . "power_options_data SET pwr_quantity = '" . (int)$variation['stock'] . "' WHERE pwr_product_code = '" . (int)$variation['eancode'] . "'");
                        print "<br />data sku ".$data['sku']."<br />";

                        echo "<pre>variation stock " . (int)$variation['stock'] . "</pre>";
                        //echo "<pre>".print_r($variation)."</pre>"; // ter debugging om te kijken wat er in de array zit.
                        echo "<pre>variation eancode ". $variation['eancode']."";
                    }

        if ($option->num_rows) {
            $pwr_id = $option->row['pwr_id'];

            $this->db->query("UPDATE " . DB_PREFIX . "power_options_data SET pwr_quantity = '" . (int)$variation['stock'] . "' WHERE pwr_product_code = '" . (int)$variation['eancode'] . "'");
            //print_r(" . (int)$variation['stock'] . ")
        }
        
    
        
    }

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

CREATE TABLE `power_options_data` (
  `pwr_id` int(11) NOT NULL,
  `pwr_product_id` int(11) NOT NULL,
  `pwr_model` varchar(128) NOT NULL,
  `pwr_sku` varchar(128) NOT NULL,
  `pwr_product_code` varchar(128) NOT NULL,
  `pwr_quantity` int(11) NOT NULL,
  `pwr_points` int(11) NOT NULL,
  `pwr_weight` decimal(15,3) NOT NULL,
  `pwr_length` decimal(15,3) NOT NULL,
  `pwr_width` decimal(15,3) NOT NULL,
  `pwr_height` decimal(15,3) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE `power_options` (
  `pwr_product_id` int(11) NOT NULL,
  `pwr_level` int(2) NOT NULL,
  `pwr_option_id` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `product` (
  `product_id` int(11) NOT NULL,
  `model` varchar(64) NOT NULL,
  `sku` varchar(64) NOT NULL,
  `upc` varchar(12) NOT NULL,
  `ean` varchar(14) NOT NULL,
  `jan` varchar(13) NOT NULL,
  `isbn` varchar(17) NOT NULL,
  `mpn` varchar(64) NOT NULL,
  `location` varchar(128) NOT NULL,
  `quantity` int(4) NOT NULL DEFAULT '0',
  `stock_status_id` int(11) NOT NULL,
  `image` varchar(255) DEFAULT NULL,
  `manufacturer_id` int(11) NOT NULL,
  `shipping` tinyint(1) NOT NULL DEFAULT '1',
  `price` decimal(15,4) NOT NULL DEFAULT '0.0000',
  `points` int(8) NOT NULL DEFAULT '0',
  `tax_class_id` int(11) NOT NULL,
  `date_available` date NOT NULL DEFAULT '0000-00-00',
  `weight` decimal(15,8) NOT NULL DEFAULT '0.00000000',
  `weight_class_id` int(11) NOT NULL DEFAULT '0',
  `length` decimal(15,8) NOT NULL DEFAULT '0.00000000',
  `width` decimal(15,8) NOT NULL DEFAULT '0.00000000',
  `height` decimal(15,8) NOT NULL DEFAULT '0.00000000',
  `length_class_id` int(11) NOT NULL DEFAULT '0',
  `subtract` tinyint(1) NOT NULL DEFAULT '1',
  `minimum` int(11) NOT NULL DEFAULT '1',
  `sort_order` int(11) NOT NULL DEFAULT '0',
  `status` tinyint(1) NOT NULL DEFAULT '0',
  `viewed` int(5) NOT NULL DEFAULT '0',
  `date_added` datetime NOT NULL,
  `date_modified` datetime NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Gewijzigd op 17/05/2020 19:06:04 door Michael vanDijk
 
- Ariën  -
Beheerder

- Ariën -

17/05/2020 18:42:18
Quote Anchor link
Waarom ga je voor de eigenschappen en nieuw veld aanmaken? Persoonlijk zou ik deze liever onderbrengen in een aparte tabel met eigenschappen, waaraan je die koppelt aan je product via een koppeltabel. Nu gaat het nog om gewichten, en dimensies, maar wat als er nog kleuren, en maten bij komen? Een mooi punt om te normaliseren.
 
Michael vanDijk

Michael vanDijk

17/05/2020 18:43:30
Quote Anchor link
Dat is al gedaan. Heb niet heel de database neergezet.

Alleen die van toepassing zijn op deze classes.

Toevoeging op 17/05/2020 19:26:28:

Probleem is dus dat hij niet de update wilt uitvoeren in de tabel. Hij moet quantity updaten met de nieuwe gegevens.
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.