troubleshooting Question

PHP Error : Warning: Invalid argument supplied for foreach() in

Avatar of sunda2010
sunda2010 asked on
PHP
4 Comments1 Solution996 ViewsLast Modified:

Warning: Invalid argument supplied for foreach() in C:\wamp\www\stocktracker-1.0\pgs\home.php on line 62

Warning: Invalid argument supplied for foreach() in C:\wamp\www\stocktracker-1.0\inc\utilities.inc.php on line 60

Notice: Undefined variable: data in C:\wamp\www\stocktracker-1.0\inc\functions.inc.php on line 50
March 27, 2010, 7:17:18 am
<?php

// Prevent this page from being accessed directly as
// it should always be called from within index.php

if (!defined('IN_VALID')) {
  die("Error: This File Cannot Be Accessed Directly");
}

require_once('inc/navigation.inc.php');

// If the stocks data hasn't already been loaded into the session
// load all the initial core information and save it to the session
// info about all the stocks in a users watch list including:
// id, symbol, name, exhange and date_added
if(!isset($_SESSION['user_stocks'])) {
  $_SESSION['user_stocks'] = get_stocks_list($_SESSION['user_id']);
}

/* do some quick error checking in case this is the users first visit 
and there aren't any stocks in their watchlist
*/

if(count($_SESSION['user_stocks']) == 0) {
	exit();
}

// Load the highs array using the batch function and cache the query.
// Use the existing stocks session to pull the stock ids into a single array.
// Store the results into the session if it hasn't already been loaded.
if(!isset($_SESSION['user_highs'])) {
  $ids_array = getSingleArray($_SESSION['user_stocks'], 'stock_id');
  $_SESSION['user_highs'] = get_batch_high($ids_array);
}

// Load the stock_names array for use later ... 

foreach ($_SESSION['user_stocks'] as $full_name) {
  $stock_names[$full_name['stock_symbol']] = $full_name['stock_name'];
}

/*
print_r($_SESSION);

echo '<pre>';
print_r($ids);
echo '</pre>';
*/

$date = getdate();

// Simple tests to see if the market is trading
// Doesn't take into account holidays or other unusual closures
if($date['hours'] > 16 || $date['hours'] < 10 || $date['weekday'] == "Saturday" || $date['weekday'] == "Sunday") {
  $market_open = false;
} else {
  $market_open = true;
}

// Load the symbols array from the existing stocks session
$symbols_array = getSingleArray($_SESSION['user_stocks'], 'stock_symbol');

// Live query to Yahoo! to get the latest stock statistics
$stock_stats = get_batch_yahoo($symbols_array);


echo date("F j, Y, g:i:s a");
echo '<table border="1" cellpadding="4" cellspacing="0">';

// Build the row with the column header information. Will repeat the header every 30 rows.
$column_labels =  '<tr bgcolor="' . $header_row . '"><td><b>Stock Name (Symbol)</b></td><td><b>';
if($market_open === true) {
  $column_labels .= 'Last Trade';
} else {
  $column_labels .= 'Close';
}
$column_labels .= '</b></td><td><b>Total Change</b></td><td><b>Opened</b></td><td><b>Low</b></td><td><b>High</b></td><td><b>Volume</b></td><td><b>52 Week High</b></td><td><b>52 Week Low</b></td></tr>';

echo $column_labels;

// The $row_primary and $row_secondary varibles come from the configuration file
// This allows for different color schemes to be applied later.
$bgcolor = $row_primary;

// Iterate through the statistics received from Yahoo! and dump them into an
// HTML table. $counter tracks the number of rows and determines when to draw the
// column header row.
foreach($stock_stats as $i) {
  static $counter = 1;

  if($counter == 30) {
    echo $column_labels;
    $counter = 1;
  }

/*
The $i array looks like this:
	[0] => AAPL				Ticker Symbol
    [1] => 70.20			Closing Price/Last Trade
    [2] => 1/14/2005		Trade Date
    [3] => 4:00pm			Trade Time
    [4] => +0.40			Percent Change
    [5] => 70.25			Opening Price
    [6] => 71.72			High Price
    [7] => 69.19			Low Price
    [8] => 31620512			Volume
*/

  echo '<tr bgcolor="' . $bgcolor . '"><td>';
  echo '<b><a href="?pg=history&symbol=' . $i[0] . '" title="' . $stock_names[$i[0]] . '" id="' . $i[0] . '">' . $stock_names[$i[0]] . ' (' . $i[0] . ')</b>'; //SYMBOL
  
  echo '</td><td>' . number_format($i[1],2) . '</td>'; // Closing Price/Last Trade
  // Calculate the total change by subtracting the current price from the opening price
  $total_change = round($i[1]-$i[5],2);
  if ($total_change >= 0) {
    echo '<td><span class="green">' . $total_change . '</span></td>';
  } else {
    echo '<td><span class="red">' . $total_change . '</span></td>';
  }   
  echo '<td>' . number_format($i[5],2) . '</td>'; // Opening Price
  echo '<td>' . number_format($i[7],2) . '</td>'; // Low Price
  echo '<td>' . number_format($i[6],2) . '</td>'; // High Price
  echo '<td>' . number_format($i[8]) . '</td>'; // Volume
  echo '<td>' . number_format($_SESSION['user_highs'][$i[0]][0],2) . '</td>'; // 52 Week High
  echo '<td>' . number_format($_SESSION['user_highs'][$i[0]][1],2) . '</td>'; // 52 Week Low
  echo '</tr>';

  // Alternate the background colors of rows for readability
  // Use the colors defined in the config file for flexibility
  if($bgcolor == $row_primary) {
    $bgcolor = $row_secondary;
  } else {
    $bgcolor = $row_primary;
  }

  $counter++;
}
echo '</table>';

?>
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros