next up previous
Next: fn.php Up: Appendix I: Code Previous: viewer.php

portfolio.php

<HTML>
<!--Updated every 15 seconds-->
<META HTTP-EQUIV="Refresh" CONTENT="15">
<HEAD><TITLE>Portfolio</TITLE></HEAD>
<STYLE>  A {text-decoration: none;} A:hover {color: gold;}
<!--potential formatting for Internet Explorer (MS)
BODY {SCROLLBAR-FACE-COLOR: "";
      SCROLLBAR-HIGHLIGHT-COLOR: "";
      SCROLLBAR-SHADOW-COLOR: "";
      SCROLLBAR-3DLIGHT-COLOR: "";
      SCROLLBAR-ARROW-COLOR: "";
      SCROLLBAR-TRACK-COLOR: "";
      SCROLLBAR-DARKSHADOW-COLOR: "";
     }-->
</STYLE>
<BODY BGCOLOR="#19638c" TEXT="#FFFFFF" LINK="gold" ALINK="gold" VLINK="gold">
<FONT FACE="arial" SIZE="-1">

<?php

 echo "<B>American Markets</B><BR>";

//Day Date Month Year [Time (24-hour system)]
 print(date("l d F Y"));
 print(" [");
 print(date("H"));
 print(date("i"));
 print("]<P>");

//include fn.php for use below
 $dir='.';
 include($dir.'/fn.php');

//call retrieving function in fn.php for the major indices
//^DJI, ^IXIC, and ^GSPC are Yahoo! Finance designations for the indices
//retrieve obtains current value, change in points, and change in percent
//retrieve also continues printing in table started below

 echo "<TABLE CELLPADDING=0 CELLSPACING=0><TR><TD WIDTH=144>";
 echo "<FONT SIZE=2 FACE=arial>Dow Jones Industrial</FONT></TD>";
 $markets = '^DJI';
 $quotes = retrieve($markets);

 echo "<TR><TD WIDTH=144><FONT SIZE=2 FACE=arial>NASDAQ Composite</FONT></TD>";
 $markets = '^IXIC';
 $quotes = retrieve($markets);

 echo "<TR><TD WIDTH=144><FONT SIZE=2 FACE=arial>";
 echo "Standard & Poor's 500</FONT></TD>";
 $markets = '^GSPC';
 $quotes = retrieve($markets);
 echo "</TABLE><P>";

 echo "<P><FONT COLOR=white><B>Portfolio of $ViewMethod</B><BR>";
 echo "<HR COLOR=FFFFFF>";

/*
 echo "&#151;&#151;&#151;&#151;&#151;&#151;&#151;&#151;";
 echo "&#151;&#151;&#151;&#151;&#151;&#151;&#151;&#151;";
 echo "&#151;&#151;&#151;&#151;&#151;&#151;&#151;&#151;";
 echo "&#151;&#151;&#151;&#151;&#151;&#151;&#151;&#151;";
 echo "&#151;&#151;&#151;&#151;&#151;&#151;&#151;&#151;";
 echo "&#151;&#151;&#151;&#151;&#151;&#151;&#151;&#151;";
 echo "&#151;&#151;&#151;&#151;&#151;&#151;&#151;&#151;";
 echo "&#151;&#151;&#151;&#151;&#151;&#151;&#151;&#151;</FONT><BR>";
*/

 $link = mysql_connect("mysql.tjhsst.edu", "jhess", "939525");
 mysql_select_db("jhess", $link);

 if ($ViewMethod=="Mr. Latimer")
  $query = "select * from latimer";
 if ($ViewMethod=="Yosemite Sam")
  $query = "select * from sam";
 $result = mysql_query($query, $link);
 $num_rows = mysql_num_rows($result);

 echo "<TABLE CELLSPACING=0 CELLPADDING=0><TR>";
 echo "<TD WIDTH=60><FONT FACE=arial SIZE=2><B>Stock</B></TD>";
 echo "<TD WIDTH=75><FONT FACE=arial SIZE=2><B>Quantity</B></TD>";
 echo "<TD WIDTH=60><FONT FACE=arial SIZE=2><B>Price</B></TD>";
 echo "<TD WIDTH=75><FONT FACE=arial SIZE=2><B>Change</B></TD>";
 echo "<TD WIDTH=420><FONT FACE=arial SIZE=2><B>Information</B></TD>";
 echo "<TD WIDTH=75><FONT FACE=arial SIZE=2><B>Net Worth</B></TD>";
 echo "</TR></TABLE>";

 echo "<HR COLOR=FFFFFF>";

/*
 echo "<FONT COLOR=white>&#151;&#151;&#151;&#151;&#151;&#151;&#151;&#151;";
 echo "&#151;&#151;&#151;&#151;&#151;&#151;&#151;&#151;";
 echo "&#151;&#151;&#151;&#151;&#151;&#151;&#151;&#151;";
 echo "&#151;&#151;&#151;&#151;&#151;&#151;&#151;&#151;";
 echo "&#151;&#151;&#151;&#151;&#151;&#151;&#151;&#151;";
 echo "&#151;&#151;&#151;&#151;&#151;&#151;&#151;&#151;";
 echo "&#151;&#151;&#151;&#151;&#151;&#151;&#151;&#151;";
 echo "&#151;&#151;&#151;&#151;&#151;&#151;&#151;&#151;</FONT><BR>";
*/

 echo "<TABLE CELLSPACING=0 CELLPADDING=0>";

 for ($c = 0; $c < $num_rows; $c++)
 {
  $row = mysql_fetch_array($result);
  $stock = $row["stock"];
  $qty = $row["qty"];

  //obtain a page containing data regarding $symbols
  $url = "http://finance.yahoo.com/q?s=$stock&d=t";
  if (!($fp = fopen($url, "r")))
  {
   echo "Unable to open page.";
   exit;
  }

  //if opening is successful, read 100000 bytes from opened page
  $contents = fread($fp, 100000);

  //close page
  fclose($fp);

  if (preg_match("/(<b>[0-9]+\.[0-9]+<\/b>)/", $contents, $quote))
   $price = $quote[1];

  if (preg_match("/(Change<br><font color=ff0020>\-[0-9]+\.[0-9]+)/", 
      $contents, $chg))
  {
   $chg[1] = substr($chg[1], 29);
   $change = $chg[1];
   }

  if (preg_match("/(\+[0-9]+\.[0-9]+)/", $contents, $chg))
   $change = $chg[1];
  
  if (preg_match("/(<font face=arial size=-1>0\.00)/", $contents, $chg))
  {
   $chg[1] = substr($chg[1], 25);
   $change = $chg[1];
  }

  if (preg_match("/(\-[0-9]+\.[0-9]+\%)/", $contents, $chgp))
   $changep = $chgp[1];
  if (preg_match("/(\+[0-9]+\.[0-9]+\%)/", $contents, $chgp))
   $changep = $chgp[1];

  $worth = substr_replace($price, '', 5, 1);

  echo "<TR><TD WIDTH=60 VALIGN=TOP><FONT FACE=arial SIZE=2><B>$stock</B></TD>";
  echo "<TD WIDTH=75 VALIGN=TOP><FONT FACE=arial SIZE=2><B>$qty</B></TD>";
  echo "<TD WIDTH=60 VALIGN=TOP><FONT FACE=arial SIZE=2><B>$price</B></TD>";
  if (preg_match("/(\+)/", $chgp[1], $extra))
  {
   echo "<TD WIDTH=75 VALIGN=TOP><FONT FACE=arial SIZE=2 COLOR=00FF00><B>";
   echo "$change<BR>$changep</B></FONT></TD>";
  }
  else 
  {
   echo "<TD WIDTH=75 VALIGN=TOP><FONT FACE=arial SIZE=2 COLOR=FFFFFF><B>";
   echo "$change<BR>$changep</B></FONT></TD>";
  }
  echo "<TD WIDTH=420><FONT FACE=arial SIZE=2><B>";
  data($stock, $c, $num_rows);
  echo"</B></TD>";
  if (strlen($price)==12)
   echo "<TD WIDTH=75 VALIGN=TOP><FONT FACE=arial SIZE=2><B>$worth.00</B></TD>";
  else echo "<TD WIDTH=75 VALIGN=TOP><FONT FACE=arial SIZE=2><B>N/A</B></TD>";

  echo "</TR>";
 }

 echo "</TABLE>";
 echo "<HR COLOR=FFFFFF><P>";

/*
echo "<FONT COLOR=white>&#151;&#151;&#151;&#151;&#151;&#151;&#151;&#151;";
echo "&#151;&#151;&#151;&#151;&#151;&#151;&#151;&#151;";
echo "&#151;&#151;&#151;&#151;&#151;&#151;&#151;&#151;";
echo "&#151;&#151;&#151;&#151;&#151;&#151;&#151;&#151;";
echo "&#151;&#151;&#151;&#151;&#151;&#151;&#151;&#151;";
echo "&#151;&#151;&#151;&#151;&#151;&#151;&#151;&#151;";
echo "&#151;&#151;&#151;&#151;&#151;&#151;&#151;&#151;";
echo "&#151;&#151;&#151;&#151;&#151;&#151;&#151;&#151;</FONT><P>";
*/
?>

<FONT SIZE="-1">
Stock data retrieved from <A HREF="http://finance.yahoo.com" TARGET="_new">
Yahoo! Finance</A>
<P>
</FONT>
</BODY>
</HTML>


Joseph B. Hess 2003-06-13