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

simetria 04-10-2017 04:58 PM

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

simetria 04-12-2017 05:17 PM

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:

$q " SELECT * FROM some_table ";
$r func_query($q);
$smarty->assign('data'$r); 


TPL
PHP Code:

{foreach from=$data name=row item=t}
  {foreach 
from=$t item=z}
    <
img href="{$z}/>
  {/foreach}
{/foreach} 


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

PhilJ 04-12-2017 05:20 PM

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...

cherie 04-12-2017 05:27 PM

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}
    <img href="{$p.image_path}" />
{/foreach} 

As for image path not displaying an image you might want to post what you are seeing.

simetria 04-12-2017 05:27 PM

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.

simetria 04-12-2017 05:37 PM

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

cherie 04-12-2017 05:42 PM

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:

$manufacturer['image_url'] = func_get_image_url($manufacturerid"M"$manufacturer['image_path']); 


PhilJ 04-12-2017 05:43 PM

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:

$manufacturers_limit = 12;
$manufacturers_sort = "random"; // random, name or orderby


Code:

<?php

// PhilJ woz ere
// https://forum.x-cart.com/showthread.php?t=75179&page=2

$manufacturers_limit = 12;
$manufacturers_sort = "random"; // random, name or orderby

//------------------------------------

if ($manufacturers_sort == "random") {
    $qry = "SELECT * FROM " . $sql_tbl["manufacturers"] . " WHERE avail='Y' ORDER BY RAND() LIMIT $manufacturers_limit";
} else if ($manufacturers_sort == "name") {
    $qry = "SELECT * FROM " . $sql_tbl["manufacturers"] . " WHERE avail='Y' ORDER BY manufacturer LIMIT $manufacturers_limit";
} else if ($manufacturers_sort == "orderby") {
    $qry = "SELECT * FROM " . $sql_tbl["manufacturers"] . " WHERE avail='Y' ORDER BY orderby LIMIT $manufacturers_limit";
}

$result = db_query($qry);

while ($result_row = db_fetch_array($result)) {
    $manufacturers[] = $result_row;
}

if ($manufacturers) {

    foreach($manufacturers as $key=> $manufacturer)
    $manufacturers[$key]["image_path"] = func_query_first_cell("SELECT image_path FROM $sql_tbl[images_M] WHERE id=".$manufacturer["manufacturerid"]);

    foreach($manufacturers as $key=> $manufacturer)
    $manufacturers[$key]["tmbn_x"] = func_query_first_cell("SELECT image_x FROM $sql_tbl[images_M] WHERE id=".$manufacturer["manufacturerid"]);

    foreach($manufacturers as $key=> $manufacturer)
    $manufacturers[$key]["tmbn_y"] = func_query_first_cell("SELECT image_y FROM $sql_tbl[images_M] WHERE id=".$manufacturer["manufacturerid"]);

}

$smarty->assign("featured_manufacturers",$manufacturers);

?>


In your PHP page...

Code:

if ($active_modules['Manufacturers']) {
    include './get_manufacturers.php';
}


Smarty...

Code:

{foreach from=$featured_manufacturers item=m name=m}

<p><a href="manufacturers.php?manufacturerid={$m.manufacturerid}"><img src="{$m.image_path|default:'default_icon.gif'}" alt="{$m.manufacturer}" width="{$m.tmbn_x}" height="{$m.tmbn_y}"></a></p>

{if $m.descr}<p>{$m.descr}</p>{/if}

<p><a href="manufacturers.php?manufacturerid={$m.manufacturerid}">{$m.manufacturer|amp}</a></p>

{if !$smarty.foreach.m.last}<hr>{/if}

{/foreach}


simetria 04-12-2017 05:59 PM

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

simetria 04-17-2017 12:39 PM

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

PhilJ 04-19-2017 01:17 PM

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);
Insert...
Code:

        if ($manufacturers) {
            foreach($manufacturers as $key=> $manufacturer)
            $manufacturers[$key]["image_path"] = func_query_first_cell("SELECT image_path FROM $sql_tbl[images_M] WHERE id=".$manufacturer["manufacturerid"]);
            foreach($manufacturers as $key=> $manufacturer)
            $manufacturers[$key]["image_x"] = func_query_first_cell("SELECT image_x FROM $sql_tbl[images_M] WHERE id=".$manufacturer["manufacturerid"]);
            foreach($manufacturers as $key=> $manufacturer)
            $manufacturers[$key]["image_y"] = func_query_first_cell("SELECT image_y FROM $sql_tbl[images_M] WHERE id=".$manufacturer["manufacturerid"]);
        }

Then in skin/common_files/modules/Manufacturers/customer_manufacturers_list.tpl

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.