vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   PHP API code in template, php file, or somewhere else? (https://vborg.vbsupport.ru/showthread.php?t=167866)

petteyg359 01-16-2008 03:53 AM

PHP API code in template, php file, or somewhere else?
 
I have the following PHP code:
Code:

<?
require_once('./api/class.api.php');
require_once('./api/class.charselect.php');
$api = new Api();
$api->debug(false);
$api->cache(false);
$api->setCredentials($apiuser,$apipass);
$uchars = $api->getCharacters();
$chars = CharSelect::getCharacters($uchars);
$apichar = $chars[0]['charname'];
$api->setCredentials($apiuser,$apipass,$apichar);
?>

I need to use this, and several more $api->getSomeInfo calls, in a new template I've created. I've got the template and php file and a link to it in the footer bar all working, I just need to know where to put this code, so that it will run when the page is accessed and so I can call the $api functions as needed from the template. Or should I run this in the php file itself and grab everything it might need, then just have the template display the requested parts? Also, if will need <if condition=$bbuserinfo[fieldX]> stuff, and I've read that doesn't work well outside of the template. Does that mean that I _can't_ put the code into the php file?

Dismounted 01-16-2008 04:00 AM

You will need to create a plugin at the appropriate location as you cannot run PHP code in templates directly.

petteyg359 01-16-2008 02:45 PM

This will need to be called many times with different settings. Do I need a seperate plugin for each call ($359api_chars, $359api_tree, etc) or can the plugin be used as an array? I followed the instructions from https://vborg.vbsupport.ru/showthrea...ghlight=plugin, but when I put $359eveit[1][charname] in the display template, it actually shows the variable name, rather than nothing. Considering it should be a blank variable, it should display nothing, right?

Opserty 01-16-2008 03:34 PM

PHP variable names cannot begin with a number.

petteyg359 01-16-2008 04:03 PM

Thank you, having all these problems and never thought of that :)

Alright, I've changed everything around so it is now $eveit359. Plugin output is now getting to the template, as long as it's plain HTML. When I try to make $eveit359 into an array (store a value into $eveit359[1], and put that in the template, I get output like "p[charname]". I am completely lost trying to figure out what the heck the template/plugin tutorials are saying with variable renaming or whatever it is they're talking about. I need to have several arrays of data accessible to the template, preferably in one big array $eveit359[1][charname], $eveit359[1][charid], $eveit359[1][tree][current], $eveit359[1][tree][time], etc.

Opserty 01-16-2008 05:20 PM

I don't know might be something to do with your script. Arrays start from 0 not 1. I don't know why you are storing it in arrays you can store it as a variable normally. If it makes it easier to use arrays then use arrays. If you have multidimension arrays consider surrounding it in brackets. Like in your template have:
PHP Code:

{$eveit359[1][tree][time]} 

If you run this at the end of your plugin it will give you a dump of all the information stored in the variable.
PHP Code:

var_dump($eveit359); 


petteyg359 01-16-2008 05:40 PM

My current plugin template content:
PHP Code:

<?php
require_once('./eveitapi/class.api.php');
require_once(
'./eveitapi/class.charselect.php');
$api359 = new Api();
$api359->debug(false);
$api359->cache(true);
$api359->setCredentials($apiuser359,$apipass359);
$uchars359 $api359->getCharacters();
$chars359 CharSelect::getCharacters($uchars359);
$eveit359 $chars359[0][charname];
var_dump($eveit359);
?>

On the display template, rather than showing the contents of $eveit359 (as I would expect it to from var_dump), it outputs the php script as plain text.

I tried changing the plugin template to a simple
PHP Code:

<?php
$eveit359 
"Hello";
var_dump($eveit359);
?>

and got no output.
PHP Code:

<?php
echo "Hello";
?>

also produces no output.

Not sure if what you meant was to use another variable in the plugin and display templates, but I tried
PHP Code:

<?php $testing359="test"?>

and $testing359 in the display template got no output.

Could the Api functions be causing a problem? The plain text script output starts with "debug(false); $api359->cache(true);" as if that first "->" is a problem.

--------------- Added [DATE]1200517434[/DATE] at [TIME]1200517434[/TIME] ---------------

I just tried
PHP Code:

<?php
echo "<font color=white>Hello</font>";
?>

as the plugin template, and now the display template outputs the following in view source:
HTML Code:

<?php
echo \"<font color=white>Hello</font>\";
?>

On the page it appears as Hello\"; ?>

If I change the double quotes to single quotes, output is blank.

Dismounted 01-17-2008 03:13 AM

You don't need to have <?php and ?> in plugins, as the code is evaluated (using eval()).

petteyg359 01-17-2008 02:28 PM

New plugin template:
PHP Code:

require_once('./eveitapi/class.api.php');
require_once(
'./eveitapi/class.charselect.php');
$api359 = new Api();
$api359->debug(false); // enable debugging
$api359->cache(false); // do not use a cache-file, if one exists
$api359->setCredentials($bbuserinfo[field6],$bbuserinfo[field7]);
$uchars359 $api359->getCharacters();
$chars359 CharSelect::getCharacters($uchars359);
$eveitapi $chars359[0][charname];
$charblahtest "Hello";
echo 
"Hello"

New output:
Code:

require_once('./eveitapi/class.api.php'); require_once('./eveitapi/class.charselect.php'); = new Api(); (false); // enable debugging (false); // do not use a cache-file, if one exists (1188020,RDQiA46jtGtzMUEFVtgbtJ2CfhTCIPGkD0JeUz22NomPeQWCx552fwe0gwkSi4bh); = (); = CharSelect::getCharacters(); = [charname]; = "Hello"; echo "Hello";
It's still pretending it's plain text rather than executing the PHP code.

The above is using the "eval" plugin and "array_merge" cache plugin.

When I use the method from this post I get the following output. Still doesn't parse it, but at least this way it's not evaluating the variables.

Code:

require_once('./eveitapi/class.api.php'); require_once('./eveitapi/class.charselect.php'); $api359 = new Api(); $api359->debug(false); // enable debugging $api359->cache(false); // do not use a cache-file, if one exists $api359->setCredentials(" . $GLOBALS['vbulletin']->userinfo['field6'] . "," . $GLOBALS['vbulletin']->userinfo['field7'] . "); $uchars359 = $api359->getCharacters(); $chars359 = CharSelect::getCharacters($uchars359); $eveitapi = $chars359[0][charname]; $charblahtest = \"Hello\"; echo \"Hello\";
Would it be easier to just build the variables and/or array in eveit.php itself, rather than using another template, since the API part won't be displaying anything, just storing information?

Marco van Herwaarden 01-17-2008 02:47 PM

What editor are you using? Looks like all linebreaks are removed.


All times are GMT. The time now is 04:47 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01949 seconds
  • Memory Usage 1,768KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (3)bbcode_code_printable
  • (1)bbcode_html_printable
  • (8)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.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
  • printthread_start
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete