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

Reply
 
Thread Tools Display Modes
  #1  
Old 05-10-2009, 06:07 PM
visitangeles visitangeles is offline
 
Join Date: Feb 2009
Posts: 23
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Trying to add PHP to my forums

I am trying to add this simple PHP code to scrape the latest currency to my forums. What is the best and easiest way? thanks,

<?php
$today = date('Y-m-d');
$con = mysql_connect('localhost', 'user', 'passwd'); # please put appropriate values here
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("currency", $con); # please replace 'mydb' with your database name.
$query = "SELECT cur_rate FROM currency WHERE cur_date = '$today' AND cur_code = 'PHP'";
$result = mysql_query($query)
or die(mysql_error());
$row = mysql_fetch_array( $result );
$php = $row['cur_rate'];

echo $php;

?>

i tried to write a plugin and used global_start as the hook and it worked one time and then went away and never came back. PLEASE HELP!

also, while i am at it, how would one format the number that it outputs so it is only 2 decimal places?

thanks in advance
Reply With Quote
  #2  
Old 05-11-2009, 05:38 AM
Dismounted's Avatar
Dismounted Dismounted is offline
 
Join Date: Jun 2005
Location: Melbourne, Australia
Posts: 15,047
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Method 1 Including External Files

Method 2 Place the code inside a plugin (no PHP start/end tags), and grab the information into a variable. Do not echo it, just use that variable in a template.
Reply With Quote
  #3  
Old 05-11-2009, 05:51 AM
visitangeles visitangeles is offline
 
Join Date: Feb 2009
Posts: 23
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Dismounted View Post
Method 1 Including External Files

Method 2 Place the code inside a plugin (no PHP start/end tags), and grab the information into a variable. Do not echo it, just use that variable in a template.
i did that in method B. it worked just the one time. i used global_start as the hook.

is there a better hook or how would you create a better hook?

i would imagine that if i used global start, it just does it the one time - showing the currency.

any ideas?

thanks.
Reply With Quote
  #4  
Old 05-11-2009, 08:18 AM
Cip Cip is offline
 
Join Date: Jul 2008
Location: Stockholm, Sweden
Posts: 47
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Plugins don't just disappear. Make sure that you didn't add the plugin in a product that you later uninstalled. Other than that I would recommend Method B, echoing has a tendency not to show up because the template system overwrites all your previous output.

For bugtesting I would make a plugin in global_start (in product vBulletin) that looked something like this:
PHP Code:
if (isset($_REQUEST['testing'])){
   
//Fetch latest Coke prices from coke.com
   
getLatestCurrencyCode();

   
//350 litres gives you 2 gold coins.
   
echo $CokeToGoldRatio;

   
//Make sure that we don't let vBulletin continue
   //(to prevent the template from writing over your echo)
   
exit;

Reply With Quote
  #5  
Old 05-11-2009, 03:00 PM
Lynne's Avatar
Lynne Lynne is offline
 
Join Date: Sep 2004
Location: California/Idaho
Posts: 41,180
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

What do you mean that it worked just one time? You mean only the first time you hit the site? First time you hit that page? I don't understand what you mean.
Reply With Quote
  #6  
Old 05-11-2009, 03:12 PM
visitangeles visitangeles is offline
 
Join Date: Feb 2009
Posts: 23
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Lynne View Post
What do you mean that it worked just one time? You mean only the first time you hit the site? First time you hit that page? I don't understand what you mean.
yes. i finally got it to work. it showed the currency. i refreshed and it was gone!

so i thought it was the hook - global_start - like it will deal with global commands but just on the start or one time. it is my first plugin attempt.

not sure what i am doing.

could someone walk me through the whole thing? that wud be even better. maybe there is a better vbulletin hook to use than global_start? not sure.

thanks.
Reply With Quote
  #7  
Old 05-11-2009, 03:37 PM
Lynne's Avatar
Lynne Lynne is offline
 
Join Date: Sep 2004
Location: California/Idaho
Posts: 41,180
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Post your exact plugin code, please, and your template edit (a few lines above and below and tell us what template it is).
Reply With Quote
  #8  
Old 05-11-2009, 04:48 PM
visitangeles visitangeles is offline
 
Join Date: Feb 2009
Posts: 23
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Lynne View Post
Post your exact plugin code, please, and your template edit (a few lines above and below and tell us what template it is).
original code is this:

<?php
$today = date('Y-m-d');
$con = mysql_connect('localhost', 'username', 'passwd1'); # please put appropriate values here
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbname_currency", $con); # please replace 'mydb' with your database name.
$query = "SELECT cur_rate FROM currency WHERE cur_date = '$today' AND cur_code = 'PHP'";
$result = mysql_query($query)
or die(mysql_error());
$row = mysql_fetch_array( $result );
$php = $row['cur_rate'];

echo "<p style=\"font-size:70%;\"><b>1 USD = PHP $php</b></p>";?>

//end orignal code

i set up a plugin called currency using vbulletin and global_start and did this:

$today = date('Y-m-d');
$con = mysql_connect('localhost', 'username', 'passwd1');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbname_currency", $con);
$query = "SELECT cur_rate FROM currency WHERE cur_date = '$today' AND cur_code = 'PHP'";
$result = mysql_query($query)
or die(mysql_error());
$row = mysql_fetch_array($result);
$php = $row['cur_rate'];

then i save this as currency (plugin)

and then go to the header to insert the information i wanted:

echo "<p style=\"font-size:70%;\"><b>1 USD = PHP $php</b></p>";

what am i doing wrong?

thanks.
Reply With Quote
  #9  
Old 05-11-2009, 05:20 PM
Lynne's Avatar
Lynne Lynne is offline
 
Join Date: Sep 2004
Location: California/Idaho
Posts: 41,180
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Please use the html/php tags. It is very difficult to read code when you don't use those.

Don't use echo. Put the variable into the template or use a template_hook. Also, it is probably not a good idea to use a variable named $php.
Reply With Quote
  #10  
Old 05-11-2009, 06:08 PM
visitangeles visitangeles is offline
 
Join Date: Feb 2009
Posts: 23
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Lynne View Post
Please use the html/php tags. It is very difficult to read code when you don't use those.

Don't use echo. Put the variable into the template or use a template_hook. Also, it is probably not a good idea to use a variable named $php.

ok.

original code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
$today = date('Y-m-d');
$con = mysql_connect('localhost', 'username', 'passwd1'); # please put appropriate values here
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbname_currency", $con); # please replace 'mydb' with your database name.
$query = "SELECT cur_rate FROM currency WHERE cur_date = '$today' AND cur_code = 'PHP'";
$result = mysql_query($query)
or die(mysql_error());
$row = mysql_fetch_array( $result );
$php = $row['cur_rate'];

echo "<p style=\"font-size:70%;\"><b>1 USD = PHP $php</b></p>";?>
</body>
</html>

and then the plugin using vbulletin and global_start


$today = date('Y-m-d');
$con = mysql_connect('localhost', 'username', 'passwd1'); # please put appropriate values here
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbname_currency", $con); # please replace 'mydb' with your database name.
$query = "SELECT cur_rate FROM currency WHERE cur_date = '$today' AND cur_code = 'PHP'";
$result = mysql_query($query)
or die(mysql_error());
$row = mysql_fetch_array( $result );
$php = $row['cur_rate'];


then in the header:
echo "<p style=\"font-size:70%;\"><b>1 USD = PHP $php</b></p>

$php is scraping philippine peso and is standard at yahoo for that currency so i cannot change it unfortunately.

is this what you wanted?

can you help me now?
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:31 PM.


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.17415 seconds
  • Memory Usage 2,272KB
  • Queries Executed 13 (?)
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
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)bbcode_php
  • (4)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (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_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • 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
  • 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
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete