1

I need to store a set of values eg: productprice,no of products customer bought, customer name under unique productId. There can be any number of customers hence many set of values. If I create a srtucture

struct product{
  bytes12 productid;
  uint price;
  uint quantity
}

the values are overwritten.So I concatenated price,quantity and name to a single string and stored in an array.

struct Product{
  bytes32[] ProductArray; 
}

mapping(string =>  MapDetails) ProductStructs;

function appendDetails(string pid,bytes32 details) payable returns(bool success){
ProductStructs[pid].ProductArray.push(details);
}

details is a concatenated string holding price,quantity and customer name. Guess that's not the way of doing it. Can anyone direct me to proper way of storing them.Help will be appreciated :)

1

I guess you want something like this here:

contract X {
    struct PRODUCT {
        bytes12 productid;
        uint price;
        uint quantity;
    }

    mapping (string => PRODUCT[]) products;

    function appendDetails(string pid, bytes12 productid, uint price, uint quantity) payable public {
        products[pid].push(PRODUCT(productid, price, quantity));
    }

    function getDetails(string pid, uint idx) 
    public view returns(bytes12 productid, uint price, uint quantity) {
        PRODUCT storage p = products[pid][idx];

        productid = p.productid;
        price = p.price;
        quantity = p.quantity;
    }

    function getDetailsCount(string pid) 
    public view returns(uint) {
        return products[pid].length;
    }
}

In the mapping you can map to an array, then you get exactly what you want.

Alternatively:

contract X {
    struct PRODUCT {
        uint price;
        uint quantity;
    }

    mapping (string => mapping (bytes12 /*productid*/ PRODUCT) ) products;

    function appendDetails(string pid, bytes12 productid, uint price, uint quantity) payable public {
        products[pid][productid].push(PRODUCT(price, quantity));
    }

    function getDetails(string pid, bytes12 productid) 
    public view returns(uint price, uint quantity) {
        PRODUCT storage p = products[pid][productid];

        price = p.price;
        quantity = p.quantity;
    }
}
  • Thanks...:) This is exactly what I need...Can you explain 'idx' in getDetails function – angelmaria Apr 5 '18 at 5:44
  • your mapping is showing to an array. idx is the index (location) of the product in this array. Alternatively you could also write mapping (string => mapping (bytes12 /*productid*/ => PRODUCT)) products if you don't want to use arrays, but nested mappings. – ivicaa Apr 5 '18 at 5:45
  • Is it possible to get all set of values under pid? – angelmaria Apr 5 '18 at 5:48
  • 1
    AFAIK, it's not working yet in Solidity to return dynamically sized arrays. Hence you'll have to call getDetails multiple times. You can also add a method for fetching the size of the array -> return products[pid].length (see modified answer) – ivicaa Apr 5 '18 at 5:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.