I am by no means an experienced coder, but I managed to get a working mod for watching NewEgg and stock prices.
Check it out here:
http://www.puremhz.com (on the right side of the page)
It allows registered users to watch whichever NewEgg items or stock prices they choose. NewEgg items are updated daily, stock prices hourly.
I'm sure my source code is terrible, so here it is, maybe someone can think of a better way to do things.
PHP Code:
dbconn.php
<?php
//Your MySQL Server's IP Address
$db_host = 'localhost';
//Your MySQL DataBase Name
$db_name = 'db';
//Your MySQL User Name
$db_user = 'user';
//Your MySQL Password
$db_pass = 'pass';
?>
PHP Code:
functions.php
<?php
function psyNumRows($param) {
require 'db_conn.php';
$a = mysql_db_query($db_name,$param);
if ($a)
$b = mysql_num_rows($a);
return $b;
}
function psyRunQuery($param) {
require 'db_conn.php';
mysql_db_query($db_name,$param);
}
function psyGrabOne($param) {
require 'db_conn.php';
$a = mysql_db_query($db_name,$param);
if ($a)
list($b) = mysql_fetch_row($a);
return $b;
}
function psyPrepList($param) {
require 'db_conn.php';
return mysql_db_query($db_name,$param);
}
function psyDateDiff($date1, $date2) {
$a = strtotime($date2)-strtotime($date1);
return intval($a/86400);
}
function psyNoBadChars($param) {
$param = str_replace("\\", "\", $param);
$param = str_replace("'", "'", $param);
// $param = str_replace("/", "/", $param);
$param = str_replace('"', """, $param);
return $param;
}
function psyTagNumber($param) {
$x = substr($param, -1);
if (strlen($param) > 1)
$y = substr($param, -2, 1);
else
$y = 0;
if ($x == 1)
if ($y == 1)
$res_string = 'th';
else
$res_string = 'st';
elseif ($x == 2)
if ($y == 1)
$res_string = 'th';
else
$res_string = 'nd';
elseif ($x == 3)
if ($y == 1)
$res_string = 'th';
else
$res_string = 'rd';
else
$res_string = 'th';
return $param . $res_string;
}
function psyFileList ($dirPath) {
if ($handle = opendir($dirPath))
{
while (false !== ($file = readdir($handle)))
if ($file != "." && $file != "..")
$filesArr[] = trim($file);
closedir($handle);
}
return $filesArr;
}
function psyTime ($time) {
if (!$time)
$time = date("G:i");
if (substr($time, 0, 2) > 12)
$time = substr($time, 0, 2)-12 . substr($time, 2, 3) . "PM";
else
$time = $time . "AM";
return $time;
}
function psyNEBetween ($orig_html, $start, $end, $buffer) {
$s_res = strpos($orig_html, $start);
$res = substr($orig_html, $s_res + strlen($start), $buffer);
$e_res = strpos($res, $end);
$res = substr($res, 0, $e_res);
return $res;
}
function psyNEName ($f_item) {
$f_html = implode('', file('http://www.newegg.com/Product/Product.asp?Item=' . $f_item));
return psyNEBetween($f_html, 'Description=', '" class=', 200);
}
function psyNEPrice ($f_item) {
$f_html = implode('', file('http://www.newegg.com/Product/Product.asp?Item=' . $f_item));
$f_price = psyNEBetween($f_html, '<span class="showPrice"><h3>$', '<', 10);
return str_replace(",", "", $f_price);
}
function psyBetween ($orig_html, $start, $end, $buffer) {
$s_res = strpos($orig_html, $start);
$res = substr($orig_html, $s_res + strlen($start), $buffer);
$e_res = strpos($res, $end);
$res = substr($res, 0, $e_res);
return $res;
}
function psyName ($f_item) {
$f_html = implode('', file('http://finance.yahoo.com/q?s=' . $f_item));
return psyBetween($f_html, '<td height="30" class="ygtb"><b>', '</b>', 200);
}
function psyShortName ($f_name) {
if (strlen($f_name) > 30)
$res = substr($f_name, 0, 30) . "..";
else
$res = $f_name;
return $res;
}
function psyPrice ($f_item) {
$f_html = implode('', file('http://finance.yahoo.com/q?s=' . $f_item));
return psyBetween($f_html, 'Last Trade:</td><td class="yfnc_tabledata1"><big><b>', '<', 10);
}
function psyNEError ($f_error) {
$f_output = "<b>NewEgg Watch Error #" . $f_error . ": </b>";
switch ($f_error) {
case 1: $f_output .= "You must be <a href=\"register.php\">registered</a> and logged in to modify items. If this is the first time you are using NewEgg Watch, try adding an item first.";
break;
case 2: $f_output .= "Invalid item number. Hint: NewEgg item numbers are 15 characters long.";
break;
case 3: $f_output .= "Invalid item number. Hint: NewEgg item numbers start with a capital \"N\".";
break;
case 4: $f_output .= "You are already watching that item.";
break;
case 5: $f_output .= "Invalid item number or NewEgg is unreachable for verification at this time.";
break;
case 6: $f_output .= "Invalid mode.";
break;
case 7: $f_output .= "Invalid item id number.";
break;
}
return $f_output;
}
function psyStockError ($f_error) {
$f_output = "<b>NewEgg Watch Error #" . $f_error . ": </b>";
switch ($f_error) {
case 1: $f_output .= "You must be <a href=\"register.php\">registered</a> and logged in to modify items. If this is the first time you are using Stock Watch, try adding a symbol first.";
break;
case 2: $f_output .= "Invalid stock symbol. Hint: Stock symbols are less than 5 characters long.";
break;
case 4: $f_output .= "You are already watching that stock.";
break;
case 5: $f_output .= "Invalid stock symbol or Yahoo is unreachable for verification at this time.";
break;
case 6: $f_output .= "Invalid mode.";
break;
case 7: $f_output .= "Invalid item id number.";
break;
}
return $f_output;
}
PHP Code:
newegg.php
<?php
require_once('functions.php');
$real_user_id = $bbuserinfo['userid'];
if ((!$real_user_id) || (!psyGrabOne("SELECT `id` FROM `vb_egguser` WHERE `user_id`=" . $real_user_id)))
$userid = 0;
else
$userid = $real_user_id;
$res = psyPrepList("SELECT * FROM `vb_egguser` WHERE `user_id`=" . $userid);
$rows = psyNumRows("SELECT `id` FROM `vb_egguser` WHERE `user_id`=" . $userid);
$tdclass = 'alt1';
$ne_total = 0;
for($x=0; $x<$rows; $x++) {
list($id, $item_id, $user_id) = mysql_fetch_row($res);
$lastupdate = psyGrabOne("SELECT `date` FROM `vb_egghistory` WHERE `item_id`=" . $item_id . " ORDER BY `date` DESC LIMIT 1");
$item_no = psyGrabOne("SELECT `item_no` FROM `vb_eggitems` WHERE `id`=" . $item_id);
$now = date("Y-m-d");
if ($lastupdate != $now) {
$price[$x] = psyNEPrice($item_no);
$description[$x] = psyGrabOne("SELECT `description` FROM `vb_eggitems` WHERE `id`=" . $item_id);
if (!$description[$x])
$description[$x] = psyNEName($item_no);
psyRunQuery("INSERT INTO `vb_egghistory` (`item_id`, `date`, `price`) VALUES ('" . $item_id . "', '" . $now . "', '" . $price[$x] . "')");
} else {
$price[$x] = psyGrabOne("SELECT `price` FROM `vb_egghistory` WHERE `item_id`=" . $item_id . " AND `date`='" . $now . "'");
$description[$x] = psyGrabOne("SELECT `description` FROM `vb_eggitems` WHERE `id`=" . $item_id);
}
$prev_price[$x] = psyGrabOne("SELECT `price` FROM `vb_egghistory` WHERE `item_id`=" . $item_id . " AND `price`<>'" . $price[$x] . "' ORDER BY `date` DESC LIMIT 1");
$link = 'http://www.newegg.com/Product/Product.asp?Item=' . $item_no;
$s_name = psyShortName($description[$x]);
if ($tdclass == 'alt2')
$tdclass = 'alt1';
else
$tdclass = 'alt2';
if ($prev_price[$x]) {
$price_change[$x] = $price[$x] - $prev_price[$x];
if ($price_change[$x] > 0)
$output .= '<tr><td class="' . $tdclass . '"><span class="smallfont"><a target="_new" href="' . $link . '">' . $s_name . '</a><br><img src="/images/newegg/up.gif"> (<b><font color="#FF0000">$' . number_format($price[$x],2) . '</font></b>) <a href="cmps_index.php?page=eggupdate&mode=edit&item_id=' . $item_id . '"><img border="0" src="/images/newegg/edit.gif"></a> <a href="cmps_index.php?page=eggupdate&mode=drop&item_id=' . $item_id . '"><img border="0" src="/images/newegg/drop.gif"></a></span></td></tr>';
elseif ($price_change[$x] < 0)
$output .= '<tr><td class="' . $tdclass . '"><span class="smallfont"><a target="_new" href="' . $link . '">' . $s_name . '</a><br><img src="/images/newegg/down.gif"> (<b><font color="#009900">$' . number_format($price[$x],2) . '</font></b>) <a href="cmps_index.php?page=eggupdate&mode=edit&item_id=' . $item_id . '"><img border="0" src="/images/newegg/edit.gif"></a> <a href="cmps_index.php?page=eggupdate&mode=drop&item_id=' . $item_id . '"><img border="0" src="/images/newegg/drop.gif"></a></span></td></tr>';
else
$output .= '<tr><td class="' . $tdclass . '"><span class="smallfont"><a target="_new" href="' . $link . '">' . $s_name . '</a><br>(<b>$' . number_format($price[$x],2) . '</b>) <a href="cmps_index.php?page=eggupdate&mode=edit&item_id=' . $item_id . '"><img border="0" src="/images/newegg/edit.gif"></a> <a href="cmps_index.php?page=eggupdate&mode=drop&item_id=' . $item_id . '"><img border="0" src="/images/newegg/drop.gif"></a></span></td></tr>';
} else
$output .= '<tr><td class="' . $tdclass . '"><span class="smallfont"><a target="_new" href="' . $link . '">' . $s_name . '</a><br>(<b>$' . number_format($price[$x],2) . '</b>) <a href="cmps_index.php?page=eggupdate&mode=edit&item_id=' . $item_id . '"><img border="0" src="/images/newegg/edit.gif"></a> <a href="cmps_index.php?page=eggupdate&mode=drop&item_id=' . $item_id . '"><img border="0" src="/images/newegg/drop.gif"></a></span></td></tr>';
$ne_total += $price[$x];
}
if ($tdclass == 'alt2')
$tdclass = 'alt1';
else
$tdclass = 'alt2';
$output .= '<tr><td class="' . $tdclass . '"><span class="smallfont"><b>Total: $' . number_format($ne_total,2) . '</b></span></td></tr>';
if ($tdclass == 'alt2')
$tdclass = 'alt1';
else
$tdclass = 'alt2';
eval('$home["$mods[modid]"][\'content\'] = "' . fetch_template('adv_portal_newegg') . '";');
?>
PHP Code:
eggupdate.php
<?php
require_once('functions.php');
$_POST['new_item_no'] = psyNoBadChars($_POST['new_item_no']);
$_GET['description'] = psyNoBadChars($_GET['description']);
$_GET['mode'] = psyNoBadChars($_GET['mode']);
$_GET['item_id'] = psyNoBadChars($_GET['item_id']);
$_GET['sure'] = psyNoBadChars($_GET['sure']);
$error = 0;
$real_user_id = $bbuserinfo['userid'];
if ($_GET['mode'] && $_GET['item_id']) {
if (!$real_user_id)
$error = 1;
elseif (($_GET['mode'] != "edit") && ($_GET['mode'] != "drop"))
$error = 6;
elseif (!psyGrabOne("SELECT `id` FROM `vb_eggitems` WHERE `id`=" . $_GET['item_id']))
$error = 7;
if ($error)
$new_output = psyNEError($error);
else {
if ($_GET['mode'] == "edit")
if ($_GET['description'])
psyRunQuery("UPDATE `vb_eggitems` SET `description`='" . $_GET['description'] . "' WHERE `id`=" . $_GET['item_id']);
else {
$desc = psyGrabOne("SELECT `description` FROM `vb_eggitems` WHERE `id`=" . $_GET['item_id']);
$new_output = '<form action="cmps_index.php" method="get"><input type="hidden" name="page" value="eggupdate"><input type="hidden" name="mode" value="edit"><input type="hidden" name="item_id" value="' . $_GET['item_id'] . '"><br><b>You may rename the item below:</b><br><br><input type="text" size="50" name="description" value="' . $desc . '"> <input type="submit" value="Update"></form><br><br>';
}
else
if ($_GET['sure'] == 1)
psyRunQuery("DELETE FROM `vb_egguser` WHERE `user_id`=" . $real_user_id . " AND `item_id`=" . $_GET['item_id']);
elseif (!$_GET['sure'])
$new_output = '<form action="cmps_index.php" method="get"><input type="hidden" name="page" value="eggupdate"><input type="hidden" name="mode" value="drop"><input type="hidden" name="item_id" value="' . $_GET['item_id'] . '"><br><b>Are you sure you want to stop watching this item?</b><br><input type="radio" name="sure" value="3"> No<br><input type="radio" name="sure" value="1"> Yes<br><input type="submit" value="Update"></form><br><br>';
}
} else {
if (!$real_user_id)
$error = 1;
elseif (strlen($_POST['new_item_no']) != 15)
$error = 2;
elseif ($_POST['new_item_no']{0} != "N")
$error = 3;
elseif (psyGrabOne("SELECT `id` FROM `vb_eggitems` WHERE `item_no`='" . $_POST['new_item_no'] . "'"))
$error = 4;
elseif (!psyNEPrice($_POST['new_item_no']))
$error = 5;
if (!$error) {
$new_price = psyNEPrice($_POST['new_item_no']);
$new_description = psyNEName($_POST['new_item_no']);
$now = date("Y-m-d");
psyRunQuery("INSERT INTO `vb_eggitems` (`item_no`, `description`) VALUES ('" . $_POST['new_item_no'] . "', '" . $new_description . "')");
$new_item_id = psyGrabOne("SELECT `id` FROM `vb_eggitems` WHERE `item_no`='" . $_POST['new_item_no'] . "'");
psyRunQuery("INSERT INTO `vb_egghistory` (`item_id`, `date`, `price`) VALUES ('" . $new_item_id . "', '" . $now . "', '" . $new_price . "')");
psyRunQuery("INSERT INTO `vb_egguser` (`item_id`, `user_id`) VALUES ('" . $new_item_id . "', " . $real_user_id . ")");
$new_output = "<b>NewEgg Watch:</b> Item added successfully.";
} elseif ($error == 4) {
$new_item_id = psyGrabOne("SELECT `id` FROM `vb_eggitems` WHERE `item_no`='" . $_POST['new_item_no'] . "'");
if (!psyGrabOne("SELECT `id` FROM `vb_egguser` WHERE `item_id`='" . $new_item_id . "' AND `user_id`=" . $real_user_id )) {
psyRunQuery("INSERT INTO `vb_egguser` (`item_id`, `user_id`) VALUES ('" . $new_item_id . "', " . $real_user_id . ")");
$error = 0;
$new_output = "<b>NewEgg Watch:</b> Item " . $_POST['new_item_no'] . " added successfully.";
} else
$new_output = psyNEError($error);
} else
$new_output = psyNEError($error);
}
echo $new_output;
?>
PHP Code:
adv_portal_newegg
$output
<form action="cmps_index.php" method="post">
<tr><td class="$tdclass"><font size="1">Enter an item number below. (<a href="faq.php?faq=faq_watch_panels#faq_faq_newegg_watch">Help</a>)</font><br>
<input type="hidden" name="page" value="eggupdate">
<input class="bginput" type="text" name="new_item_no" size="18"> <input class="button" type="submit" value="Add">
</td></tr>
</form>