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 |
Re: Proper way for query functionality
PHP Code:
|
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 |
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. |
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:
TPL FILE: PHP Code:
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 |
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.
|
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:
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 |
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 |
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:
I tried something like this, but i get a "Page not Found"... PHP Code:
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 |
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 |
Re: Proper way for query functionality
Yes of course... I totally missed that - duuuhhh.
I am more concerned if the var is empty or not. And with the - ne '' - I get that functionality. Thank you again!!! -S |
Re: Proper way for query functionality
Hi guys, sorry for re-hashing this post.
As I previously mentioned I have everything wired up from product.php to product.tpl with a simple string being passed along. Now reverting to my original question about sending over a query to the tpl file. If I am running a SELECT * query and assign it to the smarty var I can see the array or rows and the array of columns being passed along with a print_r. The trouble I am running into is, where do i run the foreach loop - in the php file or the tpl file? In addition, where do you stipulate the column's name of the data being retrieved in this type of setup - in this case image_path column? Here's what I have so far: PHP PHP Code:
TPL PHP Code:
At the moment, what I am getting is an img tag for every column in every row - and I understand why. But when I try to specify a certain column name, no matter where I do it, I can't get it to out put only that column for each of the rows. I've tried using column name as such ['image_path'] and also the index as such [0]. ALSO - For some strange reason, even the img path columns do not display the image properly. it shows the path saved in the db inside the href, but does not display the image on the page - strange. I can see the path is correct because with dev tools if I right click over the href and choose open in new tab, it opens the image with no issues. Any input guys is really appreciated. Thank you again. -S |
Re: Proper way for query functionality
Tell us exactly what you want to display, plus where and we might be able to give you a solution...
|
Re: Proper way for query functionality
You are correct in running the foreach in the tpl. We might need to see a sample output from print_r to help you pinpoint the proper foreach code. Off the top of my head I'm guessing you are looking for something like this:
Code:
{foreach from=$data item=p} |
Re: Proper way for query functionality
I'm trying to retrieve the image paths from an image table (the maufacturers image table for this example) and then displaying the images in a page - I am using the product page just for testing purposes.
|
Re: Proper way for query functionality
Cherie, that did what I'm looking for, but the images still aren't rendering on the page. But the href paths are showing up correctly - see image.
http://www.simetriastudio.com/wp-content/themes/simetria/images/ex.jpg |
Re: Proper way for query functionality
That shows ./ as the starting point which means from your current location. You probably need the full url. modules/Manufacturers/customer_manufacturers_list.php does it this way if that helps:
PHP Code:
|
Re: Proper way for query functionality
There's probably a more efficient way, using joins on the initial query, but here's an example of how I do it, cobbled together from all the beautiful people on this forum.
Put this get_manufacturers.php in the root, noting... Quote:
Code:
<?php In your PHP page... Code:
if ($active_modules['Manufacturers']) { Smarty... Code:
{foreach from=$featured_manufacturers item=m name=m} |
Re: Proper way for query functionality
Thank you guys, you've been SUPER helpful.
PhilJ I will give your suggestion a go and post back on the results. Thanks again guys. -S |
Re: Proper way for query functionality
PhilJ,
I have implemented your code, and seems to be working good on the testing example I created, but if I try to move the code to the manufacturers' module/page, it displays things a bit differently. It seems to be also including the sidebar list as well - including the parameters defined in the backend. For example in the sidebar I have it so it only display 10 manufacturers, and on the actual manufacturers list php page it first displays those 10, then if displays the ones from your code. I tested this by changing the limit # in the get_manufacturers.php file. How can I omit the initial 10 since those are only targeted for the sidebar? Thank you, -S |
Re: Proper way for query functionality
If all you are looking to do is display manufacturer icons on the manufacturer list page...
In modules/Manufacturers/customer_manufacturers_list.php Before... Code:
$smarty->assign('manufacturers', $manufacturers); Code:
if ($manufacturers) { In the foreach loop... Code:
<img src="{$m.image_path|amp}" width="{$m.image_x}" height="{$m.image_y}" alt="{$m.manufacturer|escape}> |
All times are GMT -8. The time now is 07:08 AM. |
Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.