2

I am trying to make a "stock" website for a class at school, and it's my first dive into php. Basically, the script pulls down a CSV file form a google docs spreadsheet, and (attempts) to put the values into an array for use later. I'd like to show the top 5 rising and falling stocks, but am having issues. Here's main section of the script:

<html>
 <head>
   <?php
     #Global Variables
     $rising = array();
     $falling = array();
     $stocks = array();
     #End Global Variables

     #Function to read data from the spreadsheet
     function get_data($url){
        $ch = curl_init();
        $timeout = 5;
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
     }

     #Process data
     function populateTicker(){
        $document = "https://docs.google.com/spreadsheet/pub?hl=en_US&hl=en_US&key=0AtrtT_MC9_YFdHRDUGx0a2xveXNfOHJVdXJ6bVNkMFE&output=csv";
        $data= get_data($document);
        $lines = explode("\n", $data);
        $val = "";
        foreach($lines as $key => $value){
            if($key != 0){
                $stockInfo = explode(",", $value);
                $perChange = $stockInfo[3];
                $perChangeVal = "up ";
                if($perChange < 0){
                    $perChangeVal = "down ";
                    $falling['$stockInfo[0]'] = $perChange;
                }else{
                    $rising['$stockInfo[0]'] = $perChange;
                }
                $stocks['$stockInfo[0]'] = array("symb" => $stockInfo[0], "name" => $stockInfo[1], "price" => $stockInfo[2]);
                $val = $val . "(" . $stockInfo [0] . ")  " . $stockInfo [1]  . "  " . "\$" . $stockInfo [2] . " " . $perChangeVal .  $perChange . "% today" . "\v \v \v \v | \v \v \v \v";
            }
        }
        //asort($falling);
        //arsort($rising);
        return $val;
     }

     function getRising($index){
        if($index <= count($rising)){
            $keys = array_keys($rising);
            $data = $stocks[$keys[$index]];
            return "(" . $data['symb'] . ")  " . $data['name'] . "  " . "\$" . $data['price'];
        }else{
            return ".";
        }

     }

     function getFalling($index){
        if($index <= count($falling)){
            $keys = array_keys($falling);
            $data = $stocks[$keys[$index]];
            return "(" . $data['symb'] . ")  " . $data['name'] . "  " . "\$" . $data['price'];
        }else{
            return ".";
        }
     }
   ?>
 </head>
 <body>
   <DIV id='DEBUG'>
        <?php
            print_r($stocks);
            print_r($rising);
            print_r($falling);
        ?>
   </DIV>
  <center><b><u><font size="+2">Latest Prices</font><br /></u></b></center>
  <DIV ID="TICKER" STYLE="border-top:2px solid #CCCCCC; border-bottom:2px solid #CCCCCC; overflow:hidden; width:100%" onmouseover="TICKER_PAUSED=true" onmouseout="TICKER_PAUSED=false">
    <?php echo populateTicker(); ?>
  </DIV>
  <script type="text/javascript" src="webticker_lib.js" language="javascript"></script>
  <div id='Top5'>
   <br />
   <center><b>This page does not update automatically! Please refresh the page to update the information!</b></center>
   <br />
   <center><b><u><font size="+2">Top 5's</font><br /></u></b></center>
   <center>
       <table border="1" cellpadding="5">
          <tr>
             <th>Top 5 Rising</th>
             <th>Top 5 Falling</th>
          </tr>
          <tr>
             <td><?php echo getRising(1); ?></td>
             <td><?php echo getFalling(1); ?></td>
          </tr>
          <tr>
             <td><?php echo getRising(2); ?></td>
             <td><?php echo getFalling(2); ?></td>
          </tr>      <tr>
             <td><?php echo getRising(3); ?></td>
             <td><?php echo getFalling(3); ?></td>
          </tr>      <tr>
             <td><?php echo getRising(4); ?></td>
             <td><?php echo getFalling(4); ?></td>
          </tr>      <tr>
             <td><?php echo getRising(5); ?></td>
             <td><?php echo getFalling(5); ?></td>
          </tr>
       </table>
    </center>
  </div>
  <br />
  <center><b><u><font size="+2">All Stocks</font><br /></u></b></center>
  <div id='All'>
    <center>
        <table border="1" cellpadding="5">
          <tr>
             <th>Symbol</th>
             <th>Name</th>
             <th>Price</th>
             <th>High</th>
             <th>Low</th>
             <th>Percent Change</th>
          </tr>
          <?php
            #Dynamic Table Creation
            foreach($stocks as $key => $value){
                echo '<tr>';
                    echo '<td>(' . $value['symb'] . ')</td>';
                    echo '<td>' . $value['name'] . '</td>';
                    echo '<td>' . $value['price'] . '</td>';
                    echo '<td></td>';
                    echo '<td></td>';
                    echo '<td>' . $vaule['perChange'] . '</td>';
                echo '</tr>';
            }
          ?>
        </table>
    </center>
  </div>
 </body>
 <footer>
 </footer>
</html>

But nothing gets assigned to the arrays. Any help would be appreciated.

UPDATE: I added the full source of the front page, index.php UPDATE2: I figured it out. I come from java, and didn't fully understand how the scope of variables worked in php. A simple

    <?php
global $rising, $falling, $stocks;
...
?>

did the trick

nlowe
  • 999
  • 4
  • 11
  • 26
  • You don't seem to be calling your functions anywhere. btw if this is "homework" you should tag it as such so people know. – liquorvicar Feb 09 '12 at 18:11
  • It's not homework, and the function is called elsewhere, sorry for the confusion! I know it's executing, as the populateTicker() function returns expected data. What I'm having an issue with is the array assignments for $rising and $falling don't appear to do anything, as the arrays remain blank... – nlowe Feb 09 '12 at 21:31
  • Welcome to SO btw. Glad you figured it out I was just going to suggest that! I would recommend you invest some time in getting a step debugger working, plus you might want to look at unit-testing. I'm assuming you have experience of these things coming from Java. There is also a bug in your code: $rising['$stockInfo[0]'] should be just $rising[$stockInfo[0]] or else it will treat '$stockInfo[0]' as a string literal. – liquorvicar Feb 10 '12 at 09:57

1 Answers1

1

I don't know exactly about your code but I can show an example for presenting the nested arrays:

$arr = array('1' => array('1', '2'), '2');

function showNested($array)
{
  foreach($array as $key => $value)
  {
    if(is_array($value))
    {
      echo $value;
      showNested($array);
    }
    else
    {
      echo $value;
    }
  }
}

UPDATE

You used $stocks['$stockInfo[0]'] in your code. I think this kind of syntax would never do anything. Totally when you use a variable in a string, you should surround it by {}. And one thing else that I never tested it before, I don't think putting an array with an index in a string would help the PHP to understand what's the current data in [].

MahanGM
  • 2,352
  • 5
  • 32
  • 45
  • Sorry, I must not have been extremely clear (It's my first time posting). My issue is not how to READ the array, but how do I store it? Because both array_push and straight up assignment appear to do nothing... – nlowe Feb 09 '12 at 21:37
  • I can't understand. What do you mean by store? You want to read each field and store it in another array? If yes, just use `$arr[] = $data`. I think it's better to use old fashion way because using of `array_push` would do some manipulation on the array and you should reset your array after each use. I read it somewhere. – MahanGM Feb 10 '12 at 16:22