X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Dev Questions (https://forum.x-cart.com/forumdisplay.php?f=20)
-   -   Proper way for query functionality (https://forum.x-cart.com/showthread.php?t=75179)

simetria 04-07-2017 11:30 PM

Proper way for query functionality
 
Hello,

What is the proper way to perform a db query -> saving the query as a smarty variable -> accessing the variable in the frontend tpl files? Is there documentation available for such tasks?

I've searched around and cannot find any information on this.

-S

cherie 04-08-2017 03:36 PM

Re: Proper way for query functionality
 
PHP Code:

$q "SELECT * FROM $sql_tbl[...] ...";
$myQueryResult func_query($q);
smarty->assign('mySmartyVar',$myQueryResult); 

See include/func/func.db.php for different types of func_query functions.

simetria 04-08-2017 05:22 PM

Re: Proper way for query functionality
 
Thank You Cherie.
Does the PHP file need to live in a specific directory? or just somewhere in the root of the app?
Also, by assigning the smarty var, does it automatically make it available through out the app or do I need to do anything in the TPL file to make it available in that file?

Thank you so much for the insight.

Sergio

cflsystems 04-08-2017 05:50 PM

Re: Proper way for query functionality
 
This will depend on what you need and how you intend to use it.

You can look at root/product.php for example how the php file is structured and the query called to collect initial product info. The smarty assign is in there too.

The tpl file is skins/common_files/customer/main/product.tpl - you can see there how the smarty $product variable is used.

simetria 04-10-2017 05:48 AM

Re: Proper way for query functionality
 
Thank you cflsystems,

I have tried a simple string example, and cannot get to display it on the product.tpl. Here's what I have so far (I followed from Smarty's documentation)....

PHP FILE:
PHP Code:

$smarty = new Smarty();

$testMssg 'Testing Message!';

$smarty->assign('testing'$testMssg);

$smarty->display('skin/common_files/customer/main/product.tpl'); 


TPL FILE:
PHP Code:

<h1>{$testing}</h1


The php file is a new php file in the root dir, and the tpl file is the product.tpl under skin/common_files/customer/main/

Thank you for your help.

-S

cherie 04-10-2017 09:18 AM

Re: Proper way for query functionality
 
The cart does all of this for you if you follow the example from product.php. At the top it calls the important stuff that include setting up the new Smarty object. At the bottom it handles display itself. Also make sure you are looking at the right skin directory as set in Appearance options.

simetria 04-10-2017 09:46 AM

Re: Proper way for query functionality
 
I've tried a barrage of different combinations of including bits and pieces from the product.php file to my new file, but nothing as yielded any positive results in the product.tpl file. Here's the current code in my new php file taken straight from the product.php, just added my simple smarty element - maybe I'm missing some additional things....

PHP Code:

define('OFFERS_DONT_SHOW_NEW',1);

 
define('STORE_NAVIGATION_SCRIPT''Y');

 require 
'./auth.php';

 if (
     isset(
$productid)
     && !empty(
$productid)
     && 
$config['SEO']['clean_urls_enabled'] == 'Y'
     
&& !defined('DISPATCHED_REQUEST')
 ) {
     
func_clean_url_permanent_redirect('P'intval($productid));
 }

 
x_load(
     
'product',
     
'templater'
 
);

$smarty = new Smarty();
$testMssg 'Testing Message!';
$smarty->assign('testing'$testMssg);

func_display('customer/home.tpl'$smarty); 


I'm assuming anything above the new smarty declaration is the necessary things for Smarty, and the last line for display purposes.

BTW: I am on version 4.6.1

Thanks Guys!

-S

cflsystems 04-10-2017 10:03 AM

Re: Proper way for query functionality
 
You don't need to start new smarty object. This is done by XC when calling auth.php. When you do this you overwrite the already created smarty object.
Also product.tpl and any other main template will show based on what the $main variable value is. So what you have will not show your variable in product.tpl

Check this out - https://help.x-cart.com/index.php?title=X-Cart:Generating_module_distribution_packs_for_X-Cart_4
You don't need the portion "installation" but it will show you an empty module and its way of connecting/working with the system

simetria 04-10-2017 12:55 PM

Re: Proper way for query functionality
 
Thank you for that link...it totally helped me.
I have it kind of working, but not sure about one thing... I guess the only thing I was missing was the inclusion of the Smarty var in the home_main.tpl file. My question (or unsure about) is, If you run an elseif on the smarty variable but the value is a php variable storying the real value (like I have above), how do you insert the php variable in the elseif statement?

Here's what I have and it is working, but I would like to check if it is available before continuing.

PHP Code:

{elseif $testing}
{include 
file="customer/main/product.tpl"


I tried something like this, but i get a "Page not Found"...
PHP Code:

{elseif $testing eq $testMssg}
{include 
file="customer/main/product.tpl"


Would a .... ne ' ' suffice in this case?

Thank you all for your help, you've been a huge help in understanding how everything is wired up.

-S

cflsystems 04-10-2017 03:21 PM

Re: Proper way for query functionality
 
{elseif $testing eq $testMssg}

implies both variable have same value in order to load product.tpl. So most likely your $testMssg is empty. You cannot call php variable directly in smarty templates. Not this way at least. If you need to make this check assign the $testMssg variable to smarty from the php file


All times are GMT -8. The time now is 12:53 PM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.