![]() |
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 |
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. |
Quote:
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. |
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:
|
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.
|
Quote:
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. |
Post your exact plugin code, please, and your template edit (a few lines above and below and tell us what template it is).
|
Quote:
<?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. |
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. |
Quote:
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? |
I'll repeat... Please use the html/php tags. It is very difficult to read code when you don't use those. They are right here in the Quick Reply box - very easy to use. I don't care about your original code, I just want to see the code in your plugin. And, again, I'll repeat that you should be putting the *variable* name into a template. You CANNOT use php in a template, which means you CANNOT do an echo statement. You need to simply put the variable in there - and I would NOT name it php (I don't know that that variable named is reserved, but it sounds like it should be).
|
Quote:
i thought you were not suppose to use php or html in the plugins?. again, i simply use vb 3.8.1 and set up a plugin called currency using vbulletin and global_start as the hook i guess and inserted this code below only: PHP Code:
HTML Code:
<td align="$stylevar[right]" id="header_right_cell"> --------------- Added [DATE]1242104548[/DATE] at [TIME]1242104548[/TIME] --------------- its working now. lol. not sure what was wrong before but in the future, is there a better hook to use than global_start or does it matter? or should you create your own hook? how hard is that? |
When I was talking about the php/html tags, I'm talking about the tags on this site (that you used to surround your code in your last post). Thank you - it's much easier to read.
This is the line I think you may want to rename your variable in: PHP Code:
HTML Code:
<td> Are you only trying to show this on the forumhome (index.php) page? I would pick a plugin that is just on that page, if so - forumhome_start maybe. |
You should rename $php to something like $currency_php or similar. As Lynne suggested, $php is confusing because PHP is also the language you're coding in.
As far as hook location goes, I'd go with forumhome_start (as Lynne also suggested) or if you need it on every page, global_complete. |
All times are GMT. The time now is 05:48 PM. |
Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
![]() |
|
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|