View Full Version : Template Variable Issue
KGodel
12-04-2014, 07:41 PM
Hey guys. Before proceeding, I have read https://vborg.vbsupport.ru/showthread.php?t=228078 and am still not sure what's up with my code.
First, I have this function which is based on a user profile field single select menu.
function get_ccode ($countryname) {
$json = file_get_contents('http://country.io/names.json');
$countries = json_decode($json, true);
while ($cname = current($countries)) {
if ($cname == $countryname) {
return key($countries);
}
next($countries);
}
}
This and other functions are located in a folder on the server. I have added a plugin at the hook "global_bootstrap_init_start" with this code:
require(DIR . '/custom/functions.php');
I am now trying to create a code to output a template variable in postbit_legacy. This is the code I have for my plugin which is at the hook "process_templates_complete".
$country = $user['field58'];
$ccode = get_ccode($country);
if ($ccode != null) {
$imgoutput = "<img src='images/flags/$ccode.png' /> ";
}
$templater = vB_Template::create('countrytemplate');
$templater->register('cimg', $imgoutput);
$templatevalues['ccimg'] = $templater->render();
vB_Template::preRegister('postbit_legacy', $templatevalues);
Then I attempt t use {vb:raw ccimg} in the postbit_legacy template and nothing appears. Where am I going wrong? Thanks in advance.
I think the first thing I'd do is to make sure that $imgoutput is set to something even if get_ccode returns null, just for testing. Maybe something like:
if ($ccode != null) {
$imgoutput = "<img src='images/flags/$ccode.png' /> ";
}
else {
$imgoutput = "get_ccode('$country') returned null";
}
If it's still blank then at least you know that it's something to do with the templates.
KGodel
12-04-2014, 07:51 PM
I tried your suggestion kh99, still nothing displaying even with the null alert.
Lynne
12-04-2014, 08:07 PM
What is the content of the template countrytemplate? And have you turned on the option to view templates in the source code and verified it isn't being called?
$templater->register('cimg', $imgoutput);
But you say you use {vb:raw ccimg}? Notice the extra c.
Edit: nvm you used $templatevalues['ccimg'] = $templater->render();
although I never used that before.
OK, maybe try
$templatevalues['ccimg'] = "ccimg";
(you can put it right after the existing line so you don't have to delete anything). And see if that shows anything.
Edit: two more good answers snuck in above mine, but I think we're all thinking along the same lines, which is to find out if your template is rendering like you expect.
KGodel
12-04-2014, 08:31 PM
It did display "ccimg" after inserting the line kh99 suggested. Also, I am not sure what the content of countrytemplate is. Is it unnecessary to define a new template to add a variable?
Oh, yeah, if you didn't create a template named 'countrytemplate' then that's the problem. You could do this:
$country = $user['field58'];
$ccode = get_ccode($country);
if ($ccode != null) {
$imgoutput = "<img src='images/flags/$ccode.png' /> ";
}
$templatevalues['ccimg'] = $imgoutput;
vB_Template::preRegister('postbit_legacy', $templatevalues);
or if you want you could create a template called countrytemplate. Maybe it could contain this:
<img src='images/flags/{vb:raw ccode}.png' />
and then the plugin would be
$country = $user['field58'];
$ccode = get_ccode($country);
$templater = vB_Template::create('countrytemplate');
$templater->register('cccode', $ccode);
$templatevalues['ccimg'] = $templater->render();
vB_Template::preRegister('postbit_legacy', $templatevalues);
I don't think it matters much, but some people like to keep all html in templates.
KGodel
12-04-2014, 08:59 PM
Makes sense. I tried the way you suggested, and it does work. The issue is that country isn't being set. Do I need to do something else to get the field of the person who made the post?
Oh, yeah, I didn't even think about that. Probably what you should do is use hook location postbit_display_complete, and use $post['field58'] (the $post array has the userinfo of the post's author).
Also what you could do if you wanted, instead of preRegistering to postbit_legacy, is to set $post['ccode'], then just use {vb:raw post.ccode}. I guess that's kind of cheating but as long as nothing else is setting a ccode value then it will work.
...but if you still do want to preRegister your own variable, you could use $this->template_prefix . $this->templatename as the template name instead of 'postbit_legacy', so that it will work no matter which postbit template is being used (you could look in file includes/class_postbit.php around line 406 if you want to see why).
KGodel
12-04-2014, 09:13 PM
Woo! Thanks everyone, it's working as intended! ^^
nerbert
12-04-2014, 09:19 PM
I don't think you can use file_get_contents() to read a file you aren't hosting on your own site.
You should make your own file for that
{"BD": "Bangladesh", "BE": "Belgium", "BF": "Burkina Faso", "BG": "Bulgaria", "BA": "Bosnia and Herzegovina", "BB": "Barbados", "WF": "Wallis and Futuna", "BL": "Saint Barthelemy", "BM": "Bermuda", "BN": "Brunei", "BO": "Bolivia", "BH": "Bahrain", "BI": "Burundi", "BJ": "Benin", "BT": "Bhutan", "JM": "Jamaica", "BV": "Bouvet Island", "BW": "Botswana", "WS": "Samoa", "BQ": "Bonaire, Saint Eustatius and Saba ", "BR": "Brazil", "BS": "Bahamas", "JE": "Jersey", "BY": "Belarus", "BZ": "Belize", "RU": "Russia", "RW": "Rwanda", "RS": "Serbia", "TL": "East Timor", "RE": "Reunion", "TM": "Turkmenistan", "TJ": "Tajikistan", "RO": "Romania", "TK": "Tokelau", "GW": "Guinea-Bissau", "GU": "Guam", "GT": "Guatemala", "GS": "South Georgia and the South Sandwich Islands", "GR": "Greece", "GQ": "Equatorial Guinea", "GP": "Guadeloupe", "JP": "Japan", "GY": "Guyana", "GG": "Guernsey", "GF": "French Guiana", "GE": "Georgia", "GD": "Grenada", "GB": "United Kingdom", "GA": "Gabon", "SV": "El Salvador", "GN": "Guinea", "GM": "Gambia", "GL": "Greenland", "GI": "Gibraltar", "GH": "Ghana", "OM": "Oman", "TN": "Tunisia", "JO": "Jordan", "HR": "Croatia", "HT": "Haiti", "HU": "Hungary", "HK": "Hong Kong", "HN": "Honduras", "HM": "Heard Island and McDonald Islands", "VE": "Venezuela", "PR": "Puerto Rico", "PS": "Palestinian Territory", "PW": "Palau", "PT": "Portugal", "SJ": "Svalbard and Jan Mayen", "PY": "Paraguay", "IQ": "Iraq", "PA": "Panama", "PF": "French Polynesia", "PG": "Papua New Guinea", "PE": "Peru", "PK": "Pakistan", "PH": "Philippines", "PN": "Pitcairn", "PL": "Poland", "PM": "Saint Pierre and Miquelon", "ZM": "Zambia", "EH": "Western Sahara", "EE": "Estonia", "EG": "Egypt", "ZA": "South Africa", "EC": "Ecuador", "IT": "Italy", "VN": "Vietnam", "SB": "Solomon Islands", "ET": "Ethiopia", "SO": "Somalia", "ZW": "Zimbabwe", "SA": "Saudi Arabia", "ES": "Spain", "ER": "Eritrea", "ME": "Montenegro", "MD": "Moldova", "MG": "Madagascar", "MF": "Saint Martin", "MA": "Morocco", "MC": "Monaco", "UZ": "Uzbekistan", "MM": "Myanmar", "ML": "Mali", "MO": "Macao", "MN": "Mongolia", "MH": "Marshall Islands", "MK": "Macedonia", "MU": "Mauritius", "MT": "Malta", "MW": "Malawi", "MV": "Maldives", "MQ": "Martinique", "MP": "Northern Mariana Islands", "MS": "Montserrat", "MR": "Mauritania", "IM": "Isle of Man", "UG": "Uganda", "TZ": "Tanzania", "MY": "Malaysia", "MX": "Mexico", "IL": "Israel", "FR": "France", "IO": "British Indian Ocean Territory", "SH": "Saint Helena", "FI": "Finland", "FJ": "Fiji", "FK": "Falkland Islands", "FM": "Micronesia", "FO": "Faroe Islands", "NI": "Nicaragua", "NL": "Netherlands", "NO": "Norway", "NA": "Namibia", "VU": "Vanuatu", "NC": "New Caledonia", "NE": "Niger", "NF": "Norfolk Island", "NG": "Nigeria", "NZ": "New Zealand", "NP": "Nepal", "NR": "Nauru", "NU": "Niue", "CK": "Cook Islands", "XK": "Kosovo", "CI": "Ivory Coast", "CH": "Switzerland", "CO": "Colombia", "CN": "China", "CM": "Cameroon", "CL": "Chile", "CC": "Cocos Islands", "CA": "Canada", "CG": "Republic of the Congo", "CF": "Central African Republic", "CD": "Democratic Republic of the Congo", "CZ": "Czech Republic", "CY": "Cyprus", "CX": "Christmas Island", "CR": "Costa Rica", "CW": "Curacao", "CV": "Cape Verde", "CU": "Cuba", "SZ": "Swaziland", "SY": "Syria", "SX": "Sint Maarten", "KG": "Kyrgyzstan", "KE": "Kenya", "SS": "South Sudan", "SR": "Suriname", "KI": "Kiribati", "KH": "Cambodia", "KN": "Saint Kitts and Nevis", "KM": "Comoros", "ST": "Sao Tome and Principe", "SK": "Slovakia", "KR": "South Korea", "SI": "Slovenia", "KP": "North Korea", "KW": "Kuwait", "SN": "Senegal", "SM": "San Marino", "SL": "Sierra Leone", "SC": "Seychelles", "KZ": "Kazakhstan", "KY": "Cayman Islands", "SG": "Singapore", "SE": "Sweden", "SD": "Sudan", "DO": "Dominican Republic", "DM": "Dominica", "DJ": "Djibouti", "DK": "Denmark", "VG": "British Virgin Islands", "DE": "Germany", "YE": "Yemen", "DZ": "Algeria", "US": "United States", "UY": "Uruguay", "YT": "Mayotte", "UM": "United States Minor Outlying Islands", "LB": "Lebanon", "LC": "Saint Lucia", "LA": "Laos", "TV": "Tuvalu", "TW": "Taiwan", "TT": "Trinidad and Tobago", "TR": "Turkey", "LK": "Sri Lanka", "LI": "Liechtenstein", "LV": "Latvia", "TO": "Tonga", "LT": "Lithuania", "LU": "Luxembourg", "LR": "Liberia", "LS": "Lesotho", "TH": "Thailand", "TF": "French Southern Territories", "TG": "Togo", "TD": "Chad", "TC": "Turks and Caicos Islands", "LY": "Libya", "VA": "Vatican", "VC": "Saint Vincent and the Grenadines", "AE": "United Arab Emirates", "AD": "Andorra", "AG": "Antigua and Barbuda", "AF": "Afghanistan", "AI": "Anguilla", "VI": "U.S. Virgin Islands", "IS": "Iceland", "IR": "Iran", "AM": "Armenia", "AL": "Albania", "AO": "Angola", "AQ": "Antarctica", "AS": "American Samoa", "AR": "Argentina", "AU": "Australia", "AT": "Austria", "AW": "Aruba", "IN": "India", "AX": "Aland Islands", "AZ": "Azerbaijan", "IE": "Ireland", "ID": "Indonesia", "UA": "Ukraine", "QA": "Qatar", "MZ": "Mozambique"}
vBNinja
12-04-2014, 10:00 PM
I think you should get rid of the file_get_contents and replace it with this: :)
$countries = array (
'AD' => 'Andorra',
'AE' => 'United Arab Emirates',
'AF' => 'Afghanistan',
'AG' => 'Antigua and Barbuda',
'AI' => 'Anguilla',
'AL' => 'Albania',
'AM' => 'Armenia',
'AO' => 'Angola',
'AQ' => 'Antarctica',
'AR' => 'Argentina',
'AS' => 'American Samoa',
'AT' => 'Austria',
'AU' => 'Australia',
'AW' => 'Aruba',
'AX' => 'Aland Islands',
'AZ' => 'Azerbaijan',
'BA' => 'Bosnia and Herzegovina',
'BB' => 'Barbados',
'BD' => 'Bangladesh',
'BE' => 'Belgium',
'BF' => 'Burkina Faso',
'BG' => 'Bulgaria',
'BH' => 'Bahrain',
'BI' => 'Burundi',
'BJ' => 'Benin',
'BL' => 'Saint Barthelemy',
'BM' => 'Bermuda',
'BN' => 'Brunei',
'BO' => 'Bolivia',
'BQ' => 'Bonaire, Saint Eustatius and Saba',
'BR' => 'Brazil',
'BS' => 'Bahamas',
'BT' => 'Bhutan',
'BV' => 'Bouvet Island',
'BW' => 'Botswana',
'BY' => 'Belarus',
'BZ' => 'Belize',
'CA' => 'Canada',
'CC' => 'Cocos Islands',
'CD' => 'Democratic Republic of the Congo',
'CF' => 'Central African Republic',
'CG' => 'Republic of the Congo',
'CH' => 'Switzerland',
'CI' => 'Ivory Coast',
'CK' => 'Cook Islands',
'CL' => 'Chile',
'CM' => 'Cameroon',
'CN' => 'China',
'CO' => 'Colombia',
'CR' => 'Costa Rica',
'CU' => 'Cuba',
'CV' => 'Cape Verde',
'CW' => 'Curacao',
'CX' => 'Christmas Island',
'CY' => 'Cyprus',
'CZ' => 'Czech Republic',
'DE' => 'Germany',
'DJ' => 'Djibouti',
'DK' => 'Denmark',
'DM' => 'Dominica',
'DO' => 'Dominican Republic',
'DZ' => 'Algeria',
'EC' => 'Ecuador',
'EE' => 'Estonia',
'EG' => 'Egypt',
'EH' => 'Western Sahara',
'ER' => 'Eritrea',
'ES' => 'Spain',
'ET' => 'Ethiopia',
'FI' => 'Finland',
'FJ' => 'Fiji',
'FK' => 'Falkland Islands',
'FM' => 'Micronesia',
'FO' => 'Faroe Islands',
'FR' => 'France',
'GA' => 'Gabon',
'GB' => 'United Kingdom',
'GD' => 'Grenada',
'GE' => 'Georgia',
'GF' => 'French Guiana',
'GG' => 'Guernsey',
'GH' => 'Ghana',
'GI' => 'Gibraltar',
'GL' => 'Greenland',
'GM' => 'Gambia',
'GN' => 'Guinea',
'GP' => 'Guadeloupe',
'GQ' => 'Equatorial Guinea',
'GR' => 'Greece',
'GS' => 'South Georgia and the South Sandwich Islands',
'GT' => 'Guatemala',
'GU' => 'Guam',
'GW' => 'Guinea-Bissau',
'GY' => 'Guyana',
'HK' => 'Hong Kong',
'HM' => 'Heard Island and McDonald Islands',
'HN' => 'Honduras',
'HR' => 'Croatia',
'HT' => 'Haiti',
'HU' => 'Hungary',
'ID' => 'Indonesia',
'IE' => 'Ireland',
'IL' => 'Israel',
'IM' => 'Isle of Man',
'IN' => 'India',
'IO' => 'British Indian Ocean Territory',
'IQ' => 'Iraq',
'IR' => 'Iran',
'IS' => 'Iceland',
'IT' => 'Italy',
'JE' => 'Jersey',
'JM' => 'Jamaica',
'JO' => 'Jordan',
'JP' => 'Japan',
'KE' => 'Kenya',
'KG' => 'Kyrgyzstan',
'KH' => 'Cambodia',
'KI' => 'Kiribati',
'KM' => 'Comoros',
'KN' => 'Saint Kitts and Nevis',
'KP' => 'North Korea',
'KR' => 'South Korea',
'KW' => 'Kuwait',
'KY' => 'Cayman Islands',
'KZ' => 'Kazakhstan',
'LA' => 'Laos',
'LB' => 'Lebanon',
'LC' => 'Saint Lucia',
'LI' => 'Liechtenstein',
'LK' => 'Sri Lanka',
'LR' => 'Liberia',
'LS' => 'Lesotho',
'LT' => 'Lithuania',
'LU' => 'Luxembourg',
'LV' => 'Latvia',
'LY' => 'Libya',
'MA' => 'Morocco',
'MC' => 'Monaco',
'MD' => 'Moldova',
'ME' => 'Montenegro',
'MF' => 'Saint Martin',
'MG' => 'Madagascar',
'MH' => 'Marshall Islands',
'MK' => 'Macedonia',
'ML' => 'Mali',
'MM' => 'Myanmar',
'MN' => 'Mongolia',
'MO' => 'Macao',
'MP' => 'Northern Mariana Islands',
'MQ' => 'Martinique',
'MR' => 'Mauritania',
'MS' => 'Montserrat',
'MT' => 'Malta',
'MU' => 'Mauritius',
'MV' => 'Maldives',
'MW' => 'Malawi',
'MX' => 'Mexico',
'MY' => 'Malaysia',
'MZ' => 'Mozambique',
'NA' => 'Namibia',
'NC' => 'New Caledonia',
'NE' => 'Niger',
'NF' => 'Norfolk Island',
'NG' => 'Nigeria',
'NI' => 'Nicaragua',
'NL' => 'Netherlands',
'NO' => 'Norway',
'NP' => 'Nepal',
'NR' => 'Nauru',
'NU' => 'Niue',
'NZ' => 'New Zealand',
'OM' => 'Oman',
'PA' => 'Panama',
'PE' => 'Peru',
'PF' => 'French Polynesia',
'PG' => 'Papua New Guinea',
'PH' => 'Philippines',
'PK' => 'Pakistan',
'PL' => 'Poland',
'PM' => 'Saint Pierre and Miquelon',
'PN' => 'Pitcairn',
'PR' => 'Puerto Rico',
'PS' => 'Palestinian Territory',
'PT' => 'Portugal',
'PW' => 'Palau',
'PY' => 'Paraguay',
'QA' => 'Qatar',
'RE' => 'Reunion',
'RO' => 'Romania',
'RS' => 'Serbia',
'RU' => 'Russia',
'RW' => 'Rwanda',
'SA' => 'Saudi Arabia',
'SB' => 'Solomon Islands',
'SC' => 'Seychelles',
'SD' => 'Sudan',
'SE' => 'Sweden',
'SG' => 'Singapore',
'SH' => 'Saint Helena',
'SI' => 'Slovenia',
'SJ' => 'Svalbard and Jan Mayen',
'SK' => 'Slovakia',
'SL' => 'Sierra Leone',
'SM' => 'San Marino',
'SN' => 'Senegal',
'SO' => 'Somalia',
'SR' => 'Suriname',
'SS' => 'South Sudan',
'ST' => 'Sao Tome and Principe',
'SV' => 'El Salvador',
'SX' => 'Sint Maarten',
'SY' => 'Syria',
'SZ' => 'Swaziland',
'TC' => 'Turks and Caicos Islands',
'TD' => 'Chad',
'TF' => 'French Southern Territories',
'TG' => 'Togo',
'TH' => 'Thailand',
'TJ' => 'Tajikistan',
'TK' => 'Tokelau',
'TL' => 'East Timor',
'TM' => 'Turkmenistan',
'TN' => 'Tunisia',
'TO' => 'Tonga',
'TR' => 'Turkey',
'TT' => 'Trinidad and Tobago',
'TV' => 'Tuvalu',
'TW' => 'Taiwan',
'TZ' => 'Tanzania',
'UA' => 'Ukraine',
'UG' => 'Uganda',
'UM' => 'United States Minor Outlying Islands',
'US' => 'United States',
'UY' => 'Uruguay',
'UZ' => 'Uzbekistan',
'VA' => 'Vatican',
'VC' => 'Saint Vincent and the Grenadines',
'VE' => 'Venezuela',
'VG' => 'British Virgin Islands',
'VI' => 'U.S. Virgin Islands',
'VN' => 'Vietnam',
'VU' => 'Vanuatu',
'WF' => 'Wallis and Futuna',
'WS' => 'Samoa',
'XK' => 'Kosovo',
'YE' => 'Yemen',
'YT' => 'Mayotte',
'ZA' => 'South Africa',
'ZM' => 'Zambia',
'ZW' => 'Zimbabwe',
);
...and reverse the keys and values, then you can use isset() to check if the country is there, and you don't need to do a search.
vBNinja
12-04-2014, 10:12 PM
...and reverse the keys and values, then you can use isset() to check if the country is there, and you don't need to do a search.
What about in_array() ?
What about in_array() ?
Yeah, but then you'd still have to get the key (since that's what the function returns). There is a function whose name I can't remember that searches an array and returns the key. But I was under the impression that using a key to access an array was fast (like it was a hash table or something), so if that's true then it would be faster to reverse them.
vBNinja
12-04-2014, 10:27 PM
Yeah, but then you'd still have to get the key (since that's what the function returns). There is a function whose name I can't remember that searches an array and returns the key. But I was under the impression that using a key to access an array was fast (like it was a hash table or something), so if that's true then it would be faster to reverse them.
I think you're talking about array_search which returns the key
I think you're talking about array_search which returns the key
lol, yeah, that's the one. I guess I should be able to remember that.
KGodel
12-04-2014, 10:48 PM
With JSON you can get the contents on other sites. This is how many Streaming service APIs work. It's simply an ordered list. They keep it updated, whereas if I were to create one it'd be edited every time. I considered switching the keys and such but meh, I'm not sure how much it will make a difference.
I did switch to array_search though. ;)
With JSON you can get the contents on other sites. This is how many Streaming service APIs work. It's simply an ordered list. They keep it updated, whereas if I were to create one it'd be edited every time. I considered switching the keys and such but meh, I'm not sure how much it will make a difference.
I did switch to array_search though. ;)
I think you're right, it's a relatively small array so it won't make a lot of difference. And that json thing is cool, I hadn't really seen that before.
Not to criticize the other guys who are being helpful, but I didn't want to rewrite all your code for you because you didn't ask. But one thing I think I would do is change the function so it only gets the array once and saves it in a static or a global variable. That way at least you aren't getting it from a remote server once for each post.
Edit : BTW, there's also a function that swaps keys and values, but I can't remember what that one's called either. :(
ozzy47
12-04-2014, 11:01 PM
Or you can write a cron job, t get it once a day, or weekly, then save that in a static or a global variable. :)
vBNinja
12-04-2014, 11:31 PM
With JSON you can get the contents on other sites. This is how many Streaming service APIs work. It's simply an ordered list. They keep it updated, whereas if I were to create one it'd be edited every time. I considered switching the keys and such but meh, I'm not sure how much it will make a difference.
I did switch to array_search though. ;)
Im pretty sure everyone here knows how json works. I would create a cron or fetch it on demand but I see no point in this situation since it is countries and i don't think a country is added/removed as often ;)
Another thing to think about is what happens if the server you're fetching from is down? Well so will your "plugin"
ozzy47
12-04-2014, 11:33 PM
Yup, that is why I suggested getting the info in a cron. :)
KGodel
12-04-2014, 11:46 PM
Do I simply need to define a global variable in the cron itself and save it?
nerbert
12-05-2014, 01:05 AM
If you can get it into the form of a php array you could serialize it and store it in datastore. Is it available as such an array? I could get some code for storing in datastore.
Also, did you see my post in the country flags thread? I have small flags for all countries. Not sure how up to date it is though
KGodel
12-05-2014, 01:20 AM
I was able to get a set that matches the list pretty well. I can get the array and serialize it yes.
nerbert
12-05-2014, 01:31 AM
To store in datastore
require_once(DIR . '/includes/functions.php');
build_datastore('countries', serialize($countries), 1);
You need a plugin in "init_startup"
if(THIS_SCRIPT == 'showthread') // or whatever file reads the array
{
$datastore_fetch[] = "'countries'";
}
Then the array should be available as $vbulletin->countries
--------------- Added 1417752544 at 1417752544 ---------------
Edit : BTW, there's also a function that swaps keys and values, but I can't remember what that one's called either. :(
array_flip()
http://www.w3schools.com/php/func_array_flip.asp
KGodel
12-05-2014, 06:18 PM
I decided to save the codes to a DB where I can add/edit as I please. Is there a way to get them as you did in the init_startup without using datastore, or by using datastore some other way? Right now I have:
if(THIS_SCRIPT == 'showthread') {
$sql = "SELECT * FROM " . TABLE_PREFIX . "countrycodes";
$result = $db->query_read_slave($sql);
while ($c = mysql_fetch_array($result) {
$countries[$c[0]] = $c[1];
}
}
nerbert
12-05-2014, 08:35 PM
Well, I think init_startup is as good a place to run your SELECT query as any (provided it's not too early, before the connection to the database is created), but showthread_start would work too. Theoretically you can edit the contents of datastore but it's stored as a compressed blob that's hard to read.
As for your flag images have you considered a country flag sprite? I'm working on converting a project to use sprites instead of images. There are several such sprites open source to download.
KGodel
12-05-2014, 08:37 PM
I think we're good for now. If I run my select at showthread_start and simply save everything to $countries, will that be accessible by my plugin in the postbit?
After testing, it seems no matter where I put the $countries selection it does not execute. Do sql executions not work in plugins?
nerbert
12-05-2014, 09:36 PM
I do queries all the time with them. As long as your query comes after the file has required global.php you'll have a database connection. Do you have a function that needs $countries? Use "global $countries;" in it.
If your plugin is inside an existing vbulletin function (and I suspect it is) you have to get $countries inside the function. You could use the line
$vbulletin->countries = $countries;
after your query and then refer to it that way in your plugin, or if that doesn't work refer to it as
$this->registry->countries
in your plugin.
KGodel
12-05-2014, 10:05 PM
Okay, here are my plugins, which are not working (or at least no flags are showing up):
showthread_start
$sql = "SELECT * FROM " . TABLE_PREFIX . "countrycodes";
$result = $db->query_read_slave($sql);
while ($c = mysql_fetch_array($result) {
$countries[$c[0]] = $c[1];
}
$vbulletin->countries = $countries;
postbit_display_complete
$ccode = array_search($post['field58'], $vbulletin->countries);
if ($ccode != null) {
$imgoutput = "<img src='images/flags/$ccode.png' class='inlineimg' alt='$country' /> ";
}
$templatevalues['ccimg'] = "$imgoutput";
vB_Template::preRegister('postbit_legacy', $templatevalues);
Note: When I attempt to var_dump the $countries array after it's created, I get null, so the query itself is not being executed, even though you'd think that this was LONG after the global.php had been included...
nerbert
12-06-2014, 12:34 AM
I'm not very good with sql. What are your actual field names? I would try using those instead of 0 and 1.
And try
$result = $vbulletin->db->query_read_slave($sql);
$db doesn't always work but $vbulletin->db should
KGodel
12-06-2014, 12:35 AM
Update: When I switched from using the $vbulletin->db commands to just using mysql_ commands everything worked. ^^
nerbert
12-06-2014, 03:00 AM
That's strange. When you require global.php it requires various other files such as includes/core.php which has all the database stuff.
Glad you got it working.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.