lundi 8 octobre 2018

Convert a .txt file and create an 3D associative array in php as a product list

I am currently a newbie in the php scene and have been trying to convert .txt file to a 3D Associative Array that stores a list of products that will $_POST data to a cart.php and checkout.php page.

First Dimension I want to get the 'Title' of the text file to be the $key values of the first dimension.

Second Dimension Row 1 header will be the $key values

Third dimension will be rows matching the headers (Row 2 - Row 4) of the products.txt file.

An example of the desired format:

$products = Array

( ['Chain Loop Earrings'] => Array ( [ID] => 100 [OID] => 'One Size', [Title] => 'Chain Loop Earrings', [Description] => 'Custom Designer Earrings from X Collection', [Option] => 'NA'
[Price] => 30 );

 ['Buckle Belt'] => Array
    (
        [ID] => 200
        [OID] => 'S',
        [Title] => 'Buckle Belt',
        [Description] =>  'Custom Designer Buckle Belt from 
                           X Collection', 
        [Option] => 'Small'       
        [Price] => 120
    );

This is the products.txt page:

ID,OID,TITLE,Description,Option,Price
100,-,Chain Loop Earrings,CUSTOM CHAIN MID * LOCALLY SOURCED 
MATERIALS * ONE-OFF PIECE ,-,30.0.0
200,-,Rope Necklace,CUSTOM CHAIN MID * LOCALLY SOURCED 
MATERIALS * ONE-OFF PIECE ,-,60.0
300,S,Buckle Belt,SZ SMALL GUN-METAL BUCKLE * PIECES by YJ 
ENGRAVING * ONE-OFF PIECE,Small,120.0

This array will be added to a products.php page which will be called upon by

echo $products['Buckle Belt']['Price']; 

and applicable values from the array to display what item/s the user has added to their cart, and a calculation of the total cart price.

NOTE: Please no SQL, just PHP for now :)




Aucun commentaire:

Enregistrer un commentaire