Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls

Proper way for query functionality
 
Reply
   X-Cart forums > X-Cart 4 > Changing and adding new features
 
Thread Tools Search this Thread
  #1  
Old 04-07-2017, 11:30 PM
 
simetria simetria is offline
 

Advanced Member
  
Join Date: Jul 2013
Posts: 71
 

Default 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
__________________
Thanks.
Reply With Quote
  #2  
Old 04-08-2017, 03:36 PM
  cherie's Avatar 
cherie cherie is offline
 

X-Wizard
  
Join Date: May 2003
Location: USA
Posts: 1,532
 

Default 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.
__________________
redlimeweb.com
custom mods and design integration
4.7 linux
Reply With Quote

The following user thanks cherie for this useful post:
qualiteam (04-09-2017)
  #3  
Old 04-08-2017, 05:22 PM
 
simetria simetria is offline
 

Advanced Member
  
Join Date: Jul 2013
Posts: 71
 

Default 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
__________________
Thanks.
Reply With Quote
  #4  
Old 04-08-2017, 05:50 PM
  cflsystems's Avatar 
cflsystems cflsystems is offline
 

Veteran
  
Join Date: Apr 2007
Posts: 13,585
 

Default 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.
__________________
Steve Stoyanov
CFLSystems.com
Web Development
Reply With Quote

The following user thanks cflsystems for this useful post:
qualiteam (04-09-2017)
  #5  
Old 04-10-2017, 05:48 AM
 
simetria simetria is offline
 

Advanced Member
  
Join Date: Jul 2013
Posts: 71
 

Default 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
__________________
Thanks.
Reply With Quote
  #6  
Old 04-10-2017, 09:18 AM
  cherie's Avatar 
cherie cherie is offline
 

X-Wizard
  
Join Date: May 2003
Location: USA
Posts: 1,532
 

Default 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.
__________________
redlimeweb.com
custom mods and design integration
4.7 linux
Reply With Quote
  #7  
Old 04-10-2017, 09:46 AM
 
simetria simetria is offline
 

Advanced Member
  
Join Date: Jul 2013
Posts: 71
 

Default 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
__________________
Thanks.
Reply With Quote
  #8  
Old 04-10-2017, 10:03 AM
  cflsystems's Avatar 
cflsystems cflsystems is offline
 

Veteran
  
Join Date: Apr 2007
Posts: 13,585
 

Default 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
__________________
Steve Stoyanov
CFLSystems.com
Web Development
Reply With Quote
  #9  
Old 04-10-2017, 12:55 PM
 
simetria simetria is offline
 

Advanced Member
  
Join Date: Jul 2013
Posts: 71
 

Default 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
__________________
Thanks.
Reply With Quote
  #10  
Old 04-10-2017, 03:21 PM
  cflsystems's Avatar 
cflsystems cflsystems is offline
 

Veteran
  
Join Date: Apr 2007
Posts: 13,585
 

Default 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
__________________
Steve Stoyanov
CFLSystems.com
Web Development
Reply With Quote
Reply
   X-Cart forums > X-Cart 4 > Changing and adding new features


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 10:27 AM.

   

 
X-Cart forums © 2001-2018