Announcement

Collapse
No announcement yet.

User Defined Variable To Array

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    User Defined Variable To Array

    Hi

    Is there some clever way to push a unique list of custom product variables to an array?

    For example

    I have 5000 products and they all have a colour variable. For arguments sake we will say the colours are red, blue and yellow.


    Edit: also the array of colours need to specific to that section of products.. So if section one only has red and blue, the array will only have two values, where as other sections might have all 3 colours.

    I want to push those values into am array so I can loop through and do some stuff with.

    I know I could probably do it by querying the DB with ODBC, but I thought it might be able to be done a bit more elegant.

    To succeed with ODBC. I would need a list of product ID'S for that section, is there any way of getting the sections ID's into a php Array to loop through?

    #2
    Sorry for a late reply, no doubt you have resolved this yourself by now but I figured I'd leave a response for others with the same question.

    SellerDeck allows you to iterate through various levels using their <block> tags.

    Assuming you have a section with products in it, you can gain access to instances of these products using this block as a wrapper.

    Code:
    <actinic:block type="ProductList" >
    	
    </actinic:block>
    To get this type block (select "block" from the options provided) then double click on the opening tag. A new window will pop up and you can utilise the "Type" field to select an appropriate option (perhaps have a play around with what's available there to see how the various options work).

    Now assuming your custom product variable is called CustomColour you can reference that variable for each product within the block statement like so.

    Code:
    <actinic:block type="ProductList" >
    	<actinic:variable name="CustomColour " />
    </actinic:block>
    Obviously you are seeking to fetch this into a PHP array so lets add the necessary blocks for that to happen. Typing block again will give you a new block tag, double click the opening tag again and you can now change the "PHP" field to "true". Wrap this block around everything we have previously done.

    Code:
    <actinic:block php="true" >
    	<actinic:block type="ProductList" >
    		<actinic:variable name="CustomColour" />
    	</actinic:block>
    </actinic:block>
    To fetch these values into an array we just need to add the necessary code and tweak the custom product variable tag slightly.

    Code:
    <actinic:block php="true" >
    	$colours = array();
    	<actinic:block type="ProductList" >
    		$colours[] = '<actinic:variable encoding="perl" name="CustomColour" selectable="false" />';
    	</actinic:block>
    	var_dump($colours);
    </actinic:block>
    In the code above we create a blank array before we initiated our product iteration. Then we added to the array within the iteration. Note that I used single quotes to wrap the variable value, and by double clicking the variable I set "encoding" field to "QuotedPerl" (will escape any single quotes) and set the "Selectable" field to "false". Then obviously I dumped the array for the sake of some visual confirmation.

    The only thing to add now is a condition to ensure this only happen in sections. Type block again but this time use "block if" in the options and make your condition PageType == "Section". Wrap this around our previous code.

    Code:
    <actinic:block if="%3cactinic%3avariable%20name%3d%22PageType%22%20%2f%3e%20%3d%3d%20%22Section%22" >
    	<actinic:block php="true" >
    		$colours = array();
    		<actinic:block type="ProductList" >
    			$colours[] = '<actinic:variable encoding="perl" name="CustomColour" selectable="false" />';
    		</actinic:block>
    		var_dump($colours);
    	</actinic:block>
    </actinic:block>
    You can mix and match "block"s as required. Say perhaps you have a section with 3 other sections and you want all the products within those sections into an array. Then you would just wrap the above "ProductList" block in a "ChildSectionList" block. You could even do a combination where you fetch the immediate sections "ProductList" values and then iterate the "ChildSectionList" > "ProductList" to futher extend the array.

    You mentioned using a connection to the database to fetch your values via a query. I've often found this method preferable and faster for the more complex lookups. You can fetch the SectionID using the <actinic:variable name="SectionID" /> variable. You will then want to join the [Catalog section] table to [Product] table on [Catalog section].[nSectionID] = [Product].[nParentSectionID]. You will also then need another join to pair the [Product] table to the [UserDefinedProperties] where custom variable values are stored, and if you are looking up with a variable name, then probably linking through the [Variable] table to convert from variable name to variable ID ([nVariableID] is referenced in the [UserDefinedProperties] table).

    As you can see the query can get a little complicated even for basic lookups. Either method will get you what you want, so go with what you find easiest.

    Please note that the block tag names change in newer versions of SellerDeck, but they should be obvious to relate to the guide above.

    EDIT
    ------
    In your original question you asked for a unique array of values, I overlooked this criteria. Just pass your array through the array_unique PHP function, or even ascribe the values as array keys and references the keys in your code.
    Last edited by DaveSMR; 03-Feb-2016, 09:40 AM. Reason: various typos | overlooked a part of the question
    Dave

    Comment

    Working...
    X