help on adding (pushing) xml nodes into arrays

  • Quickstrike
  • Newbie
  • Newbie
  • Quickstrike
  • Posts: 7

Post 3+ Months Ago

I want to push and remove xml nodes into an array. This will be sort of like a collection basket, but I want to do this using xml and arrays.

When you click a button it will add an xml node (an image file) into a collection basket section. They you can either remove or view these images in your collections basket.

Could someone show me some examples and coding of how this is done? I'm a beginner at action scripting and need to see some functional coding to understand.

Thanks!
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Abelius
  • Proficient
  • Proficient
  • User avatar
  • Posts: 260
  • Loc: Miami Beach, FL, USA

Post 3+ Months Ago

In order to add items to arrays I would go through the macromedia site and learn the "push", "splice" and "slice" of the trade...

After that you'll be ready to tackle XML, I don't suggest you do it before dealing well with arrays...
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

A basic example, Click on items to add them to the basket, click on items in the basket to remove them.
XML objects basically are arrays when you consider childNodes, placing chilldNodes into a variable gives you an easily accessable array of xml childNodes to deal with.

http://www.joebertvision.net/examples/f ... /index.swf

http://www.joebertvision.net/examples/f ... /index.fla (MX 6)

Code: [ Select ]
registerInventory = function(){
    //register the products into a _global variable, this makes cloning the xml easier later on.
    _global.registeredItems = this.firstChild.childNodes;
    //show how many items are in the catalog
    inventoryItems = registeredItems.length + " Items in inventory";
    //delete the original XML object to free up memory
    delete inventory;
    //tell the display were ready to show the first item in our inventory
    inventoryLoaded();
}
inventoryLoaded = function(){
    //loop through the array of xml childNodes
    for(var i=0; i<registeredItems.length; i++){
        //attach a "product" mc for graphic representation
        attachMovie("product", "product"+i, 100+i);
        //set the label for this product
        this["product"+i].label = unescape(registeredItems[i].attributes.name);
        //link this mc to a basket childNode by product id
        this["product"+i].id = unescape(registeredItems[i].attributes.id);
        //position mc
        this["product"+i]._x = 15;
        this["product"+i]._y = 35+(i*25);
        //attach addToBasket handler to mc
        this["product"+i].onRelease = addToBasket;
    }
}
addToBasket = function(){
    //get a clone of the childNode associated with this MCs id
    var item = getItem(this.id);
    //attach the clone to the basket XML object
    basket.appendChild(item);
    //update graphic representation of basket
    updateBasket();
}
removeFromBasket = function(){
    //loop through childNodes of basket XML object untill a childNode with a product id matching the MCs id is found
    for(var i=0; i<basket.childNodes.length; i++){
        //if ids match remove the childNode from basket XML
        if(this.id == unescape(basket.childNodes[i].attributes.id)){
            basket.childNodes[i].removeNode();
            //break loop
            i = basket.childNodes.length;
        }
    }
    //update graphic representation of basket
    updateBasket();
}

function getItem(id){
    var item;
    //loop through registered items untill matching id is found
    for(var i=0; i<registeredItems.length; i++){
        if(unescape(registeredItems[i].attributes.id) == id){
            //return a clone of the childNode, breaks the loop as well
            return registeredItems[i].cloneNode(true);
        }
    }
}
updateBasket = function(){
    //removes basketCase MC & all MCs attached to it, creates a fresh one
    refreshBasketCase();
    //loop through childNodes of basket XML
    for(var i=0; i<basket.childNodes.length; i++){
        //attach product MC for each childNode
        basketCase.attachMovie("product", "product"+i, 200+i);
        //set MC label/id for linkage
        basketCase["product"+i].label = unescape(basket.childNodes[i].attributes.name);
        basketCase["product"+i].id = unescape(basket.childNodes[i].attributes.id);
        //position MC
        basketCase["product"+i]._x = 0;
        basketCase["product"+i]._y = i*25;
        //set removeFromBasket handler
        basketCase["product"+i].onRelease = removeFromBasket;
    }
    //show how many items are in the basket
    basketItems = basket.childNodes.length + " Items in basket";
}
refreshBasketCase = function(){
    //pretty self explainitory here
    removeMovieClip(basketCase);
    createEmptyMovieClip("basketCase", 99);
    basketCase._x = 215;
    basketCase._y = 35;
}

inventory = new XML();
inventory.ignoreWhite = true;
inventory.onLoad = registerInventory;
inventory.load("inventory.php");

createEmptyMovieClip("basketCase", 99);
basket = new XML();
basket.ignoreWhite = true;
  1. registerInventory = function(){
  2.     //register the products into a _global variable, this makes cloning the xml easier later on.
  3.     _global.registeredItems = this.firstChild.childNodes;
  4.     //show how many items are in the catalog
  5.     inventoryItems = registeredItems.length + " Items in inventory";
  6.     //delete the original XML object to free up memory
  7.     delete inventory;
  8.     //tell the display were ready to show the first item in our inventory
  9.     inventoryLoaded();
  10. }
  11. inventoryLoaded = function(){
  12.     //loop through the array of xml childNodes
  13.     for(var i=0; i<registeredItems.length; i++){
  14.         //attach a "product" mc for graphic representation
  15.         attachMovie("product", "product"+i, 100+i);
  16.         //set the label for this product
  17.         this["product"+i].label = unescape(registeredItems[i].attributes.name);
  18.         //link this mc to a basket childNode by product id
  19.         this["product"+i].id = unescape(registeredItems[i].attributes.id);
  20.         //position mc
  21.         this["product"+i]._x = 15;
  22.         this["product"+i]._y = 35+(i*25);
  23.         //attach addToBasket handler to mc
  24.         this["product"+i].onRelease = addToBasket;
  25.     }
  26. }
  27. addToBasket = function(){
  28.     //get a clone of the childNode associated with this MCs id
  29.     var item = getItem(this.id);
  30.     //attach the clone to the basket XML object
  31.     basket.appendChild(item);
  32.     //update graphic representation of basket
  33.     updateBasket();
  34. }
  35. removeFromBasket = function(){
  36.     //loop through childNodes of basket XML object untill a childNode with a product id matching the MCs id is found
  37.     for(var i=0; i<basket.childNodes.length; i++){
  38.         //if ids match remove the childNode from basket XML
  39.         if(this.id == unescape(basket.childNodes[i].attributes.id)){
  40.             basket.childNodes[i].removeNode();
  41.             //break loop
  42.             i = basket.childNodes.length;
  43.         }
  44.     }
  45.     //update graphic representation of basket
  46.     updateBasket();
  47. }
  48. function getItem(id){
  49.     var item;
  50.     //loop through registered items untill matching id is found
  51.     for(var i=0; i<registeredItems.length; i++){
  52.         if(unescape(registeredItems[i].attributes.id) == id){
  53.             //return a clone of the childNode, breaks the loop as well
  54.             return registeredItems[i].cloneNode(true);
  55.         }
  56.     }
  57. }
  58. updateBasket = function(){
  59.     //removes basketCase MC & all MCs attached to it, creates a fresh one
  60.     refreshBasketCase();
  61.     //loop through childNodes of basket XML
  62.     for(var i=0; i<basket.childNodes.length; i++){
  63.         //attach product MC for each childNode
  64.         basketCase.attachMovie("product", "product"+i, 200+i);
  65.         //set MC label/id for linkage
  66.         basketCase["product"+i].label = unescape(basket.childNodes[i].attributes.name);
  67.         basketCase["product"+i].id = unescape(basket.childNodes[i].attributes.id);
  68.         //position MC
  69.         basketCase["product"+i]._x = 0;
  70.         basketCase["product"+i]._y = i*25;
  71.         //set removeFromBasket handler
  72.         basketCase["product"+i].onRelease = removeFromBasket;
  73.     }
  74.     //show how many items are in the basket
  75.     basketItems = basket.childNodes.length + " Items in basket";
  76. }
  77. refreshBasketCase = function(){
  78.     //pretty self explainitory here
  79.     removeMovieClip(basketCase);
  80.     createEmptyMovieClip("basketCase", 99);
  81.     basketCase._x = 215;
  82.     basketCase._y = 35;
  83. }
  84. inventory = new XML();
  85. inventory.ignoreWhite = true;
  86. inventory.onLoad = registerInventory;
  87. inventory.load("inventory.php");
  88. createEmptyMovieClip("basketCase", 99);
  89. basket = new XML();
  90. basket.ignoreWhite = true;


Code: [ Select ]
<?xml version="1.0" encoding="iso-8859-1"?>
<inventory>
    <product id="1" name="product+1" stock="966" price="9895">
        <description> this is product ones description </description>
    </product>
    <product id="2" name="product+2" stock="926" price="91895">
        <description> this is product twos description </description>
    </product>
    <product id="3" name="product+3" stock="266" price="12345">
        <description> this is product threes description </description>
    </product>
</inventory>
  1. <?xml version="1.0" encoding="iso-8859-1"?>
  2. <inventory>
  3.     <product id="1" name="product+1" stock="966" price="9895">
  4.         <description> this is product ones description </description>
  5.     </product>
  6.     <product id="2" name="product+2" stock="926" price="91895">
  7.         <description> this is product twos description </description>
  8.     </product>
  9.     <product id="3" name="product+3" stock="266" price="12345">
  10.         <description> this is product threes description </description>
  11.     </product>
  12. </inventory>


Code: [ Select ]
<?php
require("includes/sql_config.php");

$pic_base = "images/inventory/";

$sql = "SELECT * FROM inventory";
$inventory = mysql_query($sql);

$inventory_xml = array(
    '<?xml version="1.0" encoding="iso-8859-1"?>',
    '<inventory picbase="'. urlencode($pic_base) . '">'
);

while($item = mysql_fetch_assoc($inventory)){
    $inventory_xml[] = '<product id="' . urlencode($item['id']) . '" name="' . urlencode($item['name']) . '" stock="' . urlencode($item['stock']) . '" price="' . urlencode($item['price']) . '" pic="' . urlencode($item['pic']) . '"><description><![CDATA[ ' . $item['description'] . ' ]]></description></product>';
}

$inventory_xml[] = '</inventory>';
$inventory_xml = implode("", $inventory_xml);

header("Content-Type:text/xml");
echo $inventory_xml;

/*
<?xml version="1.0" encoding="iso-8859-1"?>
<inventory>
    <product id="1" name="product+1" stock="966" price="9895">
        <description> this is product ones description </description>
    </product>
    <product id="2" name="product+2" stock="926" price="91895">
        <description> this is product twos description </description>
    </product>
    <product id="3" name="product+3" stock="266" price="12345">
        <description> this is product threes description </description>
    </product>
</inventory>
*/

?>
  1. <?php
  2. require("includes/sql_config.php");
  3. $pic_base = "images/inventory/";
  4. $sql = "SELECT * FROM inventory";
  5. $inventory = mysql_query($sql);
  6. $inventory_xml = array(
  7.     '<?xml version="1.0" encoding="iso-8859-1"?>',
  8.     '<inventory picbase="'. urlencode($pic_base) . '">'
  9. );
  10. while($item = mysql_fetch_assoc($inventory)){
  11.     $inventory_xml[] = '<product id="' . urlencode($item['id']) . '" name="' . urlencode($item['name']) . '" stock="' . urlencode($item['stock']) . '" price="' . urlencode($item['price']) . '" pic="' . urlencode($item['pic']) . '"><description><![CDATA[ ' . $item['description'] . ' ]]></description></product>';
  12. }
  13. $inventory_xml[] = '</inventory>';
  14. $inventory_xml = implode("", $inventory_xml);
  15. header("Content-Type:text/xml");
  16. echo $inventory_xml;
  17. /*
  18. <?xml version="1.0" encoding="iso-8859-1"?>
  19. <inventory>
  20.     <product id="1" name="product+1" stock="966" price="9895">
  21.         <description> this is product ones description </description>
  22.     </product>
  23.     <product id="2" name="product+2" stock="926" price="91895">
  24.         <description> this is product twos description </description>
  25.     </product>
  26.     <product id="3" name="product+3" stock="266" price="12345">
  27.         <description> this is product threes description </description>
  28.     </product>
  29. </inventory>
  30. */
  31. ?>
  • Quickstrike
  • Newbie
  • Newbie
  • Quickstrike
  • Posts: 7

Post 3+ Months Ago

Thanks for the coding, joebert, it was helpful!

Right now I'm trying to learn how to apply shared objects, and try to make a checkout button that writes a .sol file so I can record what the user actually checks out on the client side.

Thoughts on this?

Basically I'm trying to set up a personal portfolio page and try to record what pieces the user in interested in.

Thanks!
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Theese would allow you to store ids in a SOL, going on the code above.

Code: [ Select ]
//add param to basket function
addToBasket = function(id){
  //get a clone of the childNode associated with this MCs id
  var item = getItem(id ? id : this.id);
  //attach the clone to the basket XML object
  basket.appendChild(item);
  //update graphic representation of basket
  updateBasket();
}

//create shared object, attempt to find item ids & put them in the basket
cachedBasket = sharedobject.getLocal("basket");
cachedItems = cachedBasket.data.items.split("||");
for(var i=0; i<cachedItems.length; i++){
    addToBasket(cachedItems[i]);
}

//cache basket
cacheBasket = function(){
    basketItems = new Array();
    for(var i=0; i<basket.childNodes.length; i++){
        basketItems.push( unescape(basket.childNodes[i].attributes.id) );
    }
    cachedBasket.data.items = basketItems.join("||");
}
  1. //add param to basket function
  2. addToBasket = function(id){
  3.   //get a clone of the childNode associated with this MCs id
  4.   var item = getItem(id ? id : this.id);
  5.   //attach the clone to the basket XML object
  6.   basket.appendChild(item);
  7.   //update graphic representation of basket
  8.   updateBasket();
  9. }
  10. //create shared object, attempt to find item ids & put them in the basket
  11. cachedBasket = sharedobject.getLocal("basket");
  12. cachedItems = cachedBasket.data.items.split("||");
  13. for(var i=0; i<cachedItems.length; i++){
  14.     addToBasket(cachedItems[i]);
  15. }
  16. //cache basket
  17. cacheBasket = function(){
  18.     basketItems = new Array();
  19.     for(var i=0; i<basket.childNodes.length; i++){
  20.         basketItems.push( unescape(basket.childNodes[i].attributes.id) );
  21.     }
  22.     cachedBasket.data.items = basketItems.join("||");
  23. }
  • Quickstrike
  • Newbie
  • Newbie
  • Quickstrike
  • Posts: 7

Post 3+ Months Ago

I added that code, but I don't see an external .sol file being made when I run the .fla. Is that supposed to happen?

If a button is added (that says checkout) and if this checkout button is clicked, can it store the users selections placed in the basket in an external .sol file? and could it store their e-mail address or names as well?

I'm a bit confused right now, and if you could show me how this could work, then that would be great.

-Thanks!
  • Quickstrike
  • Newbie
  • Newbie
  • Quickstrike
  • Posts: 7

Post 3+ Months Ago

Could the inventory button that adds the item to the basket disapear and be unclickable so the user can only add it once. if the user removes the item from the basket section -- the item will go back to the inventory section, and thus, the user can re-add it to the basket again.

This would help me out greatly, thanks!
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

SOL files are stored in the "Application Data" folder if you're using windows, this by default is a hidden folder.

SOLs from websites as well as programs like Fireworks that use swfs within the program are stored in diffeent folders within this folder.

For instance, C:\Documents and Settings\%USERNAME%\Application Data\Macromedia\Flash Player\#SharedObjects\ will take you to a folder full of folders that have names comprised of random letters & numbers. Going into theese folders will bring up folders with websites for names. (yes it gets really confusing...)

Opening an SOL in notepad or somthing similar will give you a basically unreadable file, Seppy has an SOL reader if you want to take a look at what's in theese SOLs.

After taking a look at a few of thoose from the big boys like Macromedia or Kirupaforum you'll notice you can store everything from var a=1; to entire swf structures in them.
Here's a few Macromedia.com specific pages on SOLs as well http://www.google.com/search?q=site:www ... art=0&sa=N
  • Quickstrike
  • Newbie
  • Newbie
  • Quickstrike
  • Posts: 7

Post 3+ Months Ago

Would there be a way to make the user click on a 'checkout' button and then flush the collection basket data into the .sol file?

I'm just not sure how flash can record the user data.
  • Quickstrike
  • Newbie
  • Newbie
  • Quickstrike
  • Posts: 7

Post 3+ Months Ago

Could the collected items be sent to a different section in the timeline? is that possible?

I have included a .fla file to show you what I'm trying to do because I don't think I'm doing a good job explaining it.

http://www.student.yorku.ca/~niccolo/collect.fla
http://www.student.yorku.ca/~niccolo/inventory.xml
Please look at my file and help me out as best as you can.

So basically in the .fla I want to have different sections that will contain an inventory item. When each inventory item is clicked on it will disapear and then be added to the collections section in the timeline.
(you'll have to look at the file to understand)

-Thanks in advance.

Post Information

  • Total Posts in this topic: 10 posts
  • Users browsing this forum: No registered users and 7 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.