vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 General Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=251)
-   -   How to set a variable for a cached template in a custom page? (https://vborg.vbsupport.ru/showthread.php?t=302296)

bzcomputers 09-14-2013 07:04 PM

How to set a variable for a cached template in a custom page?
 
I'm trying to use data from MYSQL to cache a template.

I know everything with the MYSQL database connection works fine, but I can't seem to get the syntax right for placing the variable within the globaltemplates array.


What I'm working with:


Code:

// pre-cache templates used by all actions
$globaltemplates = array('cruisecams_ports', 'cruisecams_ports_menu', 'adblock_cruisecams_sidebar', 'sociallinks');


I'm trying to replace 'cruisecams_ports' with the data from MYSQL stored in $portcamtemplate.


Currently I'm just getting this error "Fatal error: Call to undefined method dbPDO::query_first_slave()" anytime I try and place the variable in the array.

All other MYSQL data works fine in the rendered page it's just when I try to set a variable in the template array that the issue occurs.

ozzy47 09-14-2013 08:45 PM

What does the code look like that you are running the query?

bzcomputers 09-14-2013 08:55 PM

Code:

// Get parameters from url query string   
    $db = new dbPDO();
 
    $cruiseportcam_id = !empty($_GET['id'])?intval($_GET['id']):1;


    // get all the values form your table limited to the first matching row (there should only be one)
    $query = "SELECT * FROM cruiseportcams WHERE cruiseportcam_id = $portcamid";


    // the prepare and bind methods prevent any sort of SQL injection
    $stmt = $db->prepare($query);
    $stmt->bindValue("$portcamid",$cruiseportcam_id);
    $stmt->execute();
   
    // we are only expecting 1 row here...
    $portcam = $stmt->fetch(PDO::FETCH_ASSOC);
    // now $portcam is an array of the values from your database


$portcamtemplate = $portcam['cruiseportcam_template'];


kh99 09-14-2013 10:20 PM

I don't know what dbPDO is, but you probably need to include files to define it.

bzcomputers 09-14-2013 11:29 PM

Quote:

Originally Posted by kh99 (Post 2445672)
I don't know what dbPDO is, but you probably need to include files to define it.


http://net.tutsplus.com/tutorials/ph...tabase-access/

http://net.tutsplus.com/tutorials/ph...-it-correctly/

PDO is a newer and more secure way for PHP to access databases.

It is working fine pulling db data I just want to place a template name from the database into the cached templates array but I'm not sure how to write the correct reference to it in PHP to make it work.

I've tried options like:
1) $portcamtemplate
2) $portcam['cruiseportcam_template']
3) echo $portcamtemplate


..and about a dozen other variations

kh99 09-15-2013 01:46 AM

Oh, yeah OK. For some reason I was thinking your error said "Call to undefined method dbPDO" but I guess I spaced out and only read part of it.

Maybe the array keys are numbers and you could use the number of the column with the template name, like $portcam[3]. (I assume the column name is 'cruiseportcam_template' and you tried $portcam['cruiseportcam_template'] already).

But I'm not sure if what you're doing makes sense. I think the only thing caching templates does is save a database read for each one by getting them all at the beginning, so if you have to do a read just to get the template name you wouldn't save anything.

bzcomputers 09-15-2013 02:42 AM

Quote:

Originally Posted by kh99 (Post 2445686)
Oh, yeah OK. For some reason I was thinking your error said "Call to undefined method dbPDO" but I guess I spaced out and only read part of it.

Maybe the array keys are numbers and you could use the number of the column with the template name, like $portcam[3]. (I assume the column name is 'cruiseportcam_template' and you tried $portcam['cruiseportcam_template'] already).

But I'm not sure if what you're doing makes sense. I think the only thing caching templates does is save a database read for each one by getting them all at the beginning, so if you have to do a read just to get the template name you wouldn't save anything.

You're right, I guess I'll just skip the caching of it. Not really much being saved anyway.


Interesting though that actually both $portcamtemplate and $portcam['cruiseportcam_template'] work just fine when referring to it when it needs to be rendered but for some reason both cause that error when trying to cache the template.


Thanks.


All times are GMT. The time now is 11:42 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.01155 seconds
  • Memory Usage 1,732KB
  • 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
  • (2)bbcode_code_printable
  • (2)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (7)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
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete