Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 General Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 11-19-2005, 08:58 PM
Shameless Shameless is offline
 
Join Date: Jan 2004
Posts: 1
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default NewEgg Watch/Stock Watch vbAdvanced Modules

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('"', "&#34", $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($res0$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_name030) . "..";
        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>&nbsp;<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>&nbsp;<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>&nbsp;<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>&nbsp;<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 '">&nbsp;<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
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 05:48 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.03518 seconds
  • Memory Usage 2,456KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (5)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)post_thanks_box
  • (1)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit_info
  • (1)postbit
  • (1)postbit_onlinestatus
  • (1)postbit_wrapper
  • (1)showthread_list
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_threadedmode.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids_threaded
  • showthread_threaded_construct_link
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete