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


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>';

?>

Open in new window

sunda2010Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

V4nP3rs13Commented:
Try this:
$userStocks = $_SESSION['user_stocks'];
foreach($userStocks as $full_name) {
0
john-formbyCommented:
Can you confirm which lines it is complaining about?
0
Flappi282Commented:
It looks like you have cut out some lines in the source. Line 62 is blank, And line 39 is carrying a foreach. Please attach the full source for me to be able to help you.
0
Ray PaseurCommented:
The PHP "foreach() statement is an iterator, and it is used on arrays and objects.  Usually the error message means that your script said something like this:

foreach($things as $thing)

And the $things variable is undefined or is not of the array or object type.

Start by putting this statement at the top of your script(s) so you can catch any undefined vars.
error_reporting_E_ALL);

Next, check to be sure you have session_start() before you use the values in the $_SESSION array (that is a common error that leads to undefined variables).  Without session_start() you can assign values to $_SESSION but they will not be carried to the next page, and no error message will be issued.  Sorry, but that is one of the flaws in PHP.  $_SESSION should be immutable until after session_start(), but alas, it is not.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.