SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Jul 2003
    Location
    Toronto, Ontario
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Retrieve a single object?

    Hey guys,

    I was just wondering how I could retrieve a single object from this list of objects...

    Code:
    stocks = new Array();
    stock = new Object();
    
    o = new Stock("Yahoo!", "YHOO", null, null, null);
    stocks[0] = o;
    
    o = new Stock("Microsoft", "MSFT", null, null, null);
    stocks[1] = o;
    
    o = new Stock("Google", "GOOG", null, null, null);
    stocks[2] = o;
    
    o = new Stock("Company", "BCE.TO", null, null, null);
    stocks[3] = o;
    
    function getStock(id) {
      for(i = 0; i < stocks.length; i ++) {
        if(stocks[i].id == id) {
          return stocks[i];
        }
      }
      return null;
    }
    
    function showWatchlist() {
      document.write("<div class=watchlist>");
      showStocks();
      document.write("</div>");
    }
    
    function showStocks() {
      for(i = 0; i < stocks.length; i ++) {
        stock = stocks[i];
        sclass = i % 2 == 0 ? "light" : "dark";
        document.write("<div class=" + sclass + ">");
        showStock(stock);
        document.write("</div>");
      }
    }
    
    function showStock(stock) {
      document.write("<div id=" + stock.id + ">")
      stockHTML = getStockHTML(stock);
      document.write(stockHTML);
      document.write("</div>");
    }
    
    function getStockHTML(stock) {
      s = "<div class=row>";
      s += "<div class=stockName>" + stock.name + "</div>";
      if(stock.last) {
        slast = stock.last;
      } else {
        slast = "N/A";
      }
    
      sicon = "";
      sChangeClass = "";
      if(stock.change) {
        if(stock.change > 0) {
          sicon = "<img src='" + imagesPath + "up.gif'>";
          sChangeClass = "valueUp";
        } else if(stock.change < 0) {
          sicon = "<img src='" + imagesPath + "dn.gif'>";
          sChangeClass = "valueDn";
        } else {      
          sicon = "<img src='" + imagesPath + "st.gif'>";
        }    
      } 
    
      schange = stock.change;  
      schangep = stock.changep;
    
      s += "<div align=center class=stockLast>" + slast + "</div>";
      s += "<div class=stockIcon>" + sicon + "</div>";
      s += "<div align=right class=stockChange><span class=" + sChangeClass + ">" + schange + "</span></div>";
      s += "<div align=right class=stockChangep><span class=" + sChangeClass + ">" + schangep + "</span></div>";
      s += "</div>";
      return s;
    }
        
    
    
    function Stock(name, id, last, change, changep) {
      this.id = id;
      this.name = name;
      this.last = last;
      this.change = change;
      this.changep = changep;
    }
    
    function createRequestObject() {
      var ro;
      var browser = navigator.appName;
      if(browser == "Microsoft Internet Explorer") {
        ro = new ActiveXObject("Microsoft.XMLHTTP");
      } else {
        ro = new XMLHttpRequest();
      }
      return ro;
    }
    
    var http = createRequestObject();
    
    function sndReq() {
      working = true;
      http.open('get', feed + "?time=" + new Date().getTime());
      http.onreadystatechange = handleResponse;
      http.send(null);
    }
    
    function doUpdate() {
      if(!working) {
        sndReq();
      }
    }
    
    
    function handleResponse() {
      if(http.readyState == 4){
        var response = http.responseText;
        var update = new Array();
    
        lines = response.split("\n");
        for(var i = 0; i < lines.length; i ++) {
          line = lines[i];
          if(line.length > 0 && line.indexOf('|' != -1)) {
            update = line.split('|');
            id = update[0];
            last = update[1];
            change = update[2];
            changep = update[3];
            updateStock(id, last, change, changep);        
          }
        }
        setTimeout("doUpdate()", 5000);
        working = false;
      } 
      
    }
    
    function updateStock(id, last, change, changep) {  
      stock = getStock(id);
      if(stock != null) {
        stock.last = last;
        stock.change = change;
        stock.changep = changep;
        s = getStockHTML(stock);
        document.getElementById(id).innerHTML = getStockHTML(stock);
      }  
    }
    To pull the object I use...

    Code:
    showWatchlist();
    But how would I go about only pulling say item #3 or only item #4?

    Thanks,
    Mario
    $ Available For Hire
    Need a website designed or coded? Then feel free to contact me!

    PSP Backgrounds - PSPBG.net || Photoshop Brushes - PSBrushes.net

  2. #2
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use this function:
    Code:
    function showOneItem(index) {
      document.write("<div class=\"watchlist\">");
      document.write("<div class=\"light\">");
      showStock(stocks[index]);
      document.write("</div>");
      document.write("</div>");
    }
    and call it:
    Code:
    showOneItem(3);
    I'm creating trouble-free Apache, PHP, MySQL installer, WITSuite,
    and use it to setup my development environment.
    Demo, support, contact. Questions?

  3. #3
    SitePoint Addict
    Join Date
    Jul 2003
    Location
    Toronto, Ontario
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    Thank you so much for the reply, I will give that a shot in just a little bit.

    Thank you again,
    Mario
    $ Available For Hire
    Need a website designed or coded? Then feel free to contact me!

    PSP Backgrounds - PSPBG.net || Photoshop Brushes - PSBrushes.net


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •