working on it ...

Filters

Explore Public Snippets

Sort by

Found 1,081 snippets matching: collections

    public by shebin512  937255  3  6  0

    Product Listing in Magento using SKUs

    Code to fetch Products in collection and useing SKU's and display the products
    <html>
      <body>
    <?php
    /**
    * Code to fetch Products in collection and useing SKU's and  display the products
    *
    */
    $pCollection = Mage::getModel('catalog/product')
        ->getCollection()
        ->addAttributeToFilter(
            'sku',
            array('in' => array(
    			    18811601,51361203,51360601,18811602,51361201,83545603,18811603// Test Sku's
            	//18807601,18807603,10264601,18792202,  83527701, 56821204// sku dev setup
    			  )
    		  )
        )	
        ->addAttributeToSelect('price')
        ->addAttributeToSelect('name')
        ->addAttributeToSelect('image')
    	  ->addAttributeToSelect('special_price');
    	  
    	  //Media model to fetch Image URL
    	  $productMediaConfig = Mage::getModel('catalog/product_media_config');
    	  ?>
    	  <div class="product-list owlCarousel">
    		<?php
    				if (count($pCollectionBolt)>0){
    					foreach ( $pCollectionBolt as $_product) {
                //Mage::Helper('training')->getProductDetails($_product); You can use helper to display the product details as you Wish
                //I'm adding code here itself
                $_pSku = $_product->getSku();
                $_pName = $_product->getName();
                $_pUrl = $_product->getProductUrl();
                $_pImageUrl = Mage::helper('catalog/image')->init($_product, 'image');
                //$_pPrice = $_product->getPrice();
                $_pPrice = $_product->getFinalPrice();
                echo "<div><a href='".$_pUrl."'><img src='".$_pImageUrl."' alt='".$_pName."' /></a>
                    <div class='productname-price'><a href='".$_pUrl."' style='min-height:0px;'>".$_pName."</a><span>&#8377;".$_pPrice."</span></div>
                  </div>";
    					}
    				}else{
    					echo "<span style='text-align:center'>No products in the List</span>";
    				}
    			?>
    		</div>
    	</body>
    </html>            

    public by marksimon232  5448  2  9  2

    Swift Collections: Writing a Switch statement

    Objective-C to Swift
    In Swift, switch statements are a lot more flexible and can be used to test a variety of comparison operations. In Objective-C, switch statements were limited to integer cases.
    
    Objective-C:
    int numberOfPeople = 1;
     
    switch (numberOfPeople)
    {
        case 1:
            // code for this case
            break;
        case 2:
            // code for this case
            break;
        default:
            // code for this case
            break;
    }
    
    ========THIS CODE WILL BE WRITTEN AS:==============
    
    Swift:
    Notice that you don’t need the break statements anymore!
    
    var numberOfPeople = 1
     
    switch numberOfPeople {
        case 1:
            // code for this case
        case 2:
            // code for this case
        default:
            // code for this case
    }
     
    var carMake = "Toyota"
     
    switch carMake {
        case "Toyota":
            // code for this case
        case "Honda":
            // code for this case
        case "Nissan", "Subaru":
            // code for this case
        default:
            // code for this case
    }
    

    public by marksimon232  4132  0  6  4

    Swift Collections: Declaring a delegate property

    Delegation works hand in hand with protocols because it allows a class to specify a delegate property which conforms to some protocol.
    Objective-C:
    In Objective-C, declaring a delegate property involved using the “id” 
    keyword as shown below.
    
    @interface FirstClass : NSObject
    @property (nonatomic, weak) id<SampleProtocol> delegate;
    @end
    
    -----------------------
    
    Swift:
    In Swift, declaring a delegate property is just like declaring any other 
    property and you specify the protocol name as the type of the property.
    
    You may notice the question mark syntax which indicates that it’s a property 
    with an optional value (there may or may not be an object assigned to it).
    
    class FirstClass
    {
        var delegate:SampleProtocol?
    }

    public by marksimon232  4285  1  6  1

    Swift Collections: Declaring an array

    Objective-C to Swift
    Arrays in Objective-C used the NSArray and NSMutableArray classes and each array could contain a mixture of objects of different class types. In Swift, there’s one Array class and you have to specify the type of objects that the array will contain.
    
    Objective-C:
    NSMutableArray *myArray = [[NSMutableArray alloc] init];
    NSMutableArray *myArray2 = [@[@"item 1", @"item 2"] mutableCopy];
    
    ========THIS CODE WILL BE WRITTEN AS:==============
    
    Swift:
    Notice that in the second and third examples, we don’t specify that the variable is of type String Array uses because it can be inferred from the array that we’re assigning it. From the assigned array items, Swift can infer that the variable is an Array and it contains String objects.
    We don’t need to specify the variable type because it can be inferred from what we’re assigning to it.
    
    var myArray:String[] = String[]()
    var myArray2 = ["item 1", "item 2"]

    public by marksimon232  3752  2  6  0

    Swift Collections: Swift Array reference Snippets

    Swift makes creating and modifying arrays really easy. When you create your array, you don’t have to initialize it with a certain type, if all of your given values are the same type.
    Example: If you create an array and all of the initial values are Strings, Swift automatically infers that the array will be a string array.
      
    => Creating a mutable array:
    Notice that the beginning of the line begins with var, which means variable and allows the contents of the array to be changed in the future.
    var cityArray = ["Portland","San Francisco","Cupertino"]
    
    
    => Creating an immutable array:
    Notice that the beginning of the line begins with let, which means constant and means the contents can not be changed in the future.
    let cityArray = ["Portland","San Francisco","Cupertino"]
    
    
    => Creating an empty array:
    If you need to create an empty array, you must initialize it with the type of object that will be added later.
    var animalArray = String[]()
    //This array will only allow strings.
     
    var animalAgeArray = Int[]()
    //This array will only hold integers, nothing else.
    
    
    => Creating an array to hold a specific type:
    This variable array will only hold string objects, nothing else.
    var cityArray: String[] = ["Portland","San Francisco","Cupertino"]
    
    This variable array will only hold integers, nothing else.
    var numberArray:Int[] = [1,3,4]
    
    
    => Counting number of items inside an array:
    Use the read-only count property to count the number of items in an array.
    var cityArray: String[] = ["Portland","San Francisco","Cupertino"]
    let count = cityArray.count
    //count = 3
    
    
    => Check if array is empty:
    Use the Boolean isEmpty as a quick way to determine if an array is empty.
    var cityArray: String[] = ["Portland","San Francisco","Cupertino"]
    
    if cityArray.isEmpty {
        println("Empty") 
    }else{
        println("Not Empty")
    }
    //prints "Not Empty"
    
    
    => Add an item to the end of an array:
    var cityArray: String[] = ["Portland","San Francisco","Cupertino"]
    cityArray.append("Seattle")
    //The array now contains 4 items
    
    Alternately, you can use the addition assignment operator (+=) to quickly add an item.
    cityArray += "Seattle"
    
    
    => Add an array of items to an existing array:
    Use the addition assignment operator (+=) to quickly add an array of items.
    
    var cityArray: String[] = ["Portland","San Francisco","Cupertino","Seattle"]
    cityArray += ["Vancouver", "Los Angeles", "Eugene"]
    //The array now contains 7 items
    
    
    Alternately, if you have already created an array:
    
    var cityArrayA: String[] = ["Portland","San Francisco","Cupertino","Seattle"]
    var cityArrayB: String[] = ["Vancouver", "Los Angeles", "Eugene"]
    cityArrayA += cityArrayB;
    
    //cityArrayA now contains 7 items.
    
    
    => Change a value inside an array:
    Pass the index of the value you wish to change, along with the new value.
    
    var cityArray: String[] = ["Portland","San Francisco","Cupertino","Seattle"]
    cityArray[0] = "Portland, Oregon"
    //cityArray is now ["Portland, Oregon", "San Francisco", "Cupertino", "Seattle"]
    
    
    => Changing multiple values inside an array:
    Swift makes it easy to change multiple values inside an array at once. Using the subscript syntax, you provide the index of the items you wish to modify as well as the new additions.
    
    var cityArray = ["Portland, Oregon","San Francisco","Cupertino","Seattle"]
    cityArray[1...3] = ["San Francisco, California","Cupertino, California","Seattle, Washington"]
    
    //We replaced the values at indexes 1,2 &amp; 3 and now cityArray contains:
     ["San Francisco, California", "Cupertino, California", "Seattle, Washington"]
    
    
    Note: The amount of replacement items does not have to equal the length of the items you are replacing
    Example:
    
    var cityArray: String[] = ["Portland, Oregon","San Francisco","Cupertino","Seattle"]
    cityArray[1...3] = ["San Francisco, California","Cupertino, California"]
    //cityArray contains: ["Portland, Oregon", "San Francisco, California", "Cupertino, California"]
    Even though we tell the cityArray to replace indexes 1 through 3, we only give 2 values back to the array.
    
    
    
    => Removing a value from a specific index:
    var animalArray: String[] = ["Dog", "Cat", "Fish", "Owl", "Beaver"]
    animalArray.removeAtIndex(4)
    //animalArray now contains: ["Dog", "Cat", "Fish", "Owl"]
    
    
    Alternately, if you wish to save the value before you remove it:
    let stringAnimal = animalArray.removeAtIndex(4)
    //Saves "Beaver" to the string constant stringAnimal and also removes it from the array.
    
    
    => Removing the last value of an array:
    To avoid having to call count on an array, Swift allows developers to simply call removeLast to remove the last item in an array.
    
    var animalArray: String[] = ["Dog", "Cat", "Fish", "Owl" ]
    animalArray.removeLast
    //animalArray now contains 3 items, "Owl" has been removed.
    
    
    Again, if you wish to save the value before you remove it:
    
    let stringAnimal = animalArray.removeLast
    //Saves "Owl" (which was the last item in the array) to the string constant stringAnimal and also removes it from the array.
    
    
    => Removing all items:
    To remove all items from an array, use the removeAll method.
    
    var animalArray: String[] = ["Dog", "Cat", "Fish", "Owl" ]
    animalArray.removeAll()
    //animalArray is now empty.
    
    
    => Keeping the capacity of the array:
    animalArray.removeAll(keepCapacity: true)
    //animalArray is now empty but the capacity is kept at 4.
    
    
    => Iterating over an array:
    Simple for in Loop
    
    for animal in animalArray {
    
        println(animal)
    
    }
    //Prints each animal name into the console.
    
    
    for in Loop + Enumeration
    If you want to have the index of the item as well, you must use the enumerate function with your loop. The enumerate function returns a Tuple for each value in the array.
    
    for(index,animal) in enumerate(animalArray) {
    
        println("The \(animal) is at index:\(index")
    }
    
    //Output: 
    //The Dog is at index:0
    //The Cat is at index:1
    //The Fish is at index:2
    //The Owl is at index:3
    
    
    => Reversing the values in an array:
    var animalArray: String[] = ["Dog", "Cat", "Fish", "Owl" ]
    animalArray.reverse()
    
    //The array values are now ["Owl", "Fish", "Cat", "Dog"]
    
    
    => Create a new array from the reversed values:
    var animalArray: String[] = ["Dog", "Cat", "Fish", "Owl" ]
    let newArray = animalArray.reverse()
    //The newly created array contains ["Owl", "Fish", "Cat", "Dog"]

    public by marksimon232  3479  0  6  1

    Swift Collections: Simple function with return value

    I want to our function to get the current time, formatted and returned as a String.
    => Objective-C:
    -(NSString *)getCurrentTime {
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
        NSString *currentTime = [dateFormatter stringFromDate:[NSDate date]];
    
        return currentTime;
        
    }
    
    To call this method within the same class:
    NSString *time = [self getCurrentTime];
    NSLog(@"%@",time);
    //Results in the getCurrentTime function being called and the current time printed to the console.
    
    
    => Swift 
    In Swift, we include the return arrow “->” which specifies that the function will return and the type of object that will be returned (String).
        func getCurrentTime() -> String {
            
            let date = NSDate()
            let formatter = NSDateFormatter()
            formatter.timeStyle = .ShortStyle
            var stringValue = formatter.stringFromDate(date)
            
            return stringValue
            
        }
        
    To call this method within the same class:
    let time = getCurrentTime()
    println(time)
    //Results in the getCurrentTime function being called and the current time printed to the console.

    public by marksimon232  2876  1  6  2

    Swift Collections: Writing an IF statement

    Objective-C to Swift
    Objective-C:
    Notice in the second example that the If statement tests if the variable “aString” is empty.
    
    int score = 25;
    if (score > 25) {
    }
     
    NSString *aString;
    if (aString) {
    }
    
    ==========THIS CODE WILL BE WRITTEN AS==============
    
    Swift:
    In Swift, we need to denote that a variable has an optional value with a question mark. This means that it could be empty.
    Then we use the let keyword and a name to test if aString is empty.
    
    var score = 25
    if score > 25 {
    }
     
    var aString: String?
    if let myString = aString {
    }

    public by marksimon232  3052  0  6  1

    Swift Collections: Simple function with parameter

    I want to pass a name (String) to a function and then print it out to the console.
    => Objective-C:
    -(void)outputName:(NSString *)nameString {
    
       NSLog(@"My name is %@",nameString);
    
    }
    
    To call this method within the same class:
    [self outputName:@"Mark"];
    //Results in the outputName function being called and "My name is Mark" printed to the console.
    
    
    => Swift:
    In Swift, we define the input value first (in our case, the string value), followed by the Type (String).
    func outputName(nameString:String){
    
        println("My name is: \(nameString)")
    
    }
    To call this method within the same class:
    outputName(@"Mark")
    //Results in the outputName function being called and "My name is Mark" printed to the console.

    public by marksimon232  2975  0  6  1

    Swift Collections: Declaring a protocol

    In Objective-C, protocols are declared with the “@protocol” keyword. Below is an example of declaring a protocol containing one required method.
    Objective-C:
    @protocol SampleProtocol <NSObject>
    - (void)someMethod;
    @end
    
    -----------------
    
    Swift:
    In Swift, the syntax is a little different but the idea is the same.
    
    protocol SampleProtocol 
    {
        func someMethod()
    }

    public by marksimon232  2203  1  6  2

    Swift Collections: Writing a For loop

    Objective-C to Swift
    Objective-C:
    for (int i = 0; i < 10; i++) {
    }
     
    NSArray *array = @[@"item 1", @"item 2"];
    for (NSString *item in array) {
    }
    
    ========THIS CODE WILL BE WRITTEN AS:==============
    
    Swift:
    Notice the second swift example below. It’s equivalent to the first example. The .. notation can signify a range.
    
    for var i = 0; i < 10; i++ {
    }
     
    for i in 0..10 {
    }
     
    var array = ["item 1", "item 2"]
    for item in array {
    }
    • Public Snippets
    • Channels Snippets