SitePoint Sponsor

User Tag List

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

    Exclamation How do I retrieve an array?

    Hey guys,

    So I have this script that consists of 2 files:

    PHP File

    Code:
    <?
    
    $use_curl = 1;
    
    $symbols = "MSFT,GOOG,YHOO,BCE.TO";
    
    
    
    $url = "http://download.finance.yahoo.com/d/quotes.csv?s=$symbols&f=sl9c6p4&e=.csv";
    
    
    
    if($use_curl) {
    
      $ch = curl_init();
    
      curl_setopt ($ch, CURLOPT_URL, $url);
    
      curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    
      curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
    
      $fcontents = explode("\n", trim(curl_exec($ch)));
    
      curl_close($ch);
    
    } else {
    
      $fcontents = @file($url);
    
    }
    
    
    
    //echo "fcontents=$fcontents";
    
    
    
    foreach($fcontents as $line) {
    
      $info = split(",", $line);
    
      for($i = 0; $i < sizeof($info); $i ++) {
    
        $info[$i] = trim(str_replace('"', "", $info[$i]));
    
      }
    
      $last = $info[1];
    
      $change = $info[2];
    
      $changep = $info[3];
    
      $symbol = $info[0];
    
      echo "$symbol|$last|$change|$changep\n";
    
      flush();
    
    } 
    
    ?>
    Javascript File

    Code:
    working = false;
    
    feed = '/ajax-sqt.php';
    imagesPath = '/images/';
    
    
    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 &#37; 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);
      }  
    }
    And to view the information I simply type:

    Code:
    <script type="text/javascript" language=JavaScript>
    showWatchlist();
    doUpdate();
    </script>
    How would I go about only retrieving a specific value form the array of quotes? What if I only wanted to show item number 2?

    Any help would be greatly appreciated.

    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 Member RIAguy's Avatar
    Join Date
    Jul 2008
    Location
    Boston
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So, the array you want access is called stocks. If you wanted to access the second member of that array it would be:

    Code:
    stocks[1]
    Remember that arrays start at zero in JavaScript.

    So maybe you want a function that writes that value to your page somewhere. here is an example:

    Code:
    function showOneStock(number){
        var myStock = stocks[number];
        document.write("<div>"+myStock+"</div>");
    }
    You would just call that function with the id of the stock you wanted. This is really simplistic, but it should point you in the right direction.


    RIAspot.com

    RIA Community


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
  •