glenskie16 Posted January 24, 2014 Share Posted January 24, 2014 I need to output in product-list.tpl file the result of a query from within classes/Search.php in Prestashop. The query in classes/Search.php is: $sql = 'SELECT * FROM `'._DB_PREFIX_.'category_group` cg INNER JOIN `'._DB_PREFIX_.'category_product` cp ON cp.`id_category` = cg.`id_category` INNER JOIN `'._DB_PREFIX_.'category` c ON cp.`id_category` = c.`id_category` INNER JOIN `'._DB_PREFIX_.'product` p ON cp.`id_product` = p.`id_product` INNER JOIN `'._DB_PREFIX_.'product_part_number` ON p. `id_product` = `ID` '.Shop::addSqlAssociation('product', 'p', false).' WHERE c.`active` = 1 AND product_shop.`active` = 1 AND product_shop.`visibility` IN ("both", "search") AND product_shop.indexed = 1 AND cg.`id_group` '.(!$id_customer ? '= 1' : 'IN ( SELECT id_group FROM '._DB_PREFIX_.'customer_group WHERE id_customer = '.(int)$id_customer.' )'); $results = $db->executeS($sql); This is outputting a series of "GREKA" values in the `'._DB_PREFIX_.'product_part_number` table at the top of the search results page from the query above: $eligible_products = array(); foreach ($results as $row){ $eligible_products[] = $row['id_product']; echo $row['GREKA']; } What I need it to do is output the "GREKA" value within the tpl file themes/product-list.tpl for that product. This tpl file is using Smarty to output the results of the product search, I need to know how to call the "GREKA" value from the query to the foreach loop. I imagine it would be something like this: {foreach from=$products item=product name=products} {$GREKA} {/foreach} I have tried to put this line of code inside the foreach on the product-list.tpl and it gives me no output , it gives me an error: Code: {$product.GREKA} Error: Notice: Undefined index: GREKA Link to comment Share on other sites More sharing options...
prestashopninja Posted January 24, 2014 Share Posted January 24, 2014 Hello, - This is a programming (development) related question, so I hope the moderators will move it to the appropriate place. - At a first glance, the query doesn't seem to select anything from your product_part_number table, though I didn't run the query on my own. Could you please comfirm that $results already contains those "GREKA" values right in the Search::find() method, before being sent to the template? Can you see them when you add a print_r() or var_dump() on your Search override class? If so, the next step would probably be to check the variables passed to Smarty. Link to comment Share on other sites More sharing options...
glenskie16 Posted January 24, 2014 Author Share Posted January 24, 2014 Where are the variables passed to smarty ? and yes it is being outputed at the very top of the page ... i just need to know where the variables a being set so i could get it on the tpl file . Link to comment Share on other sites More sharing options...
prestashopninja Posted January 24, 2014 Share Posted January 24, 2014 Hi again, The results are passed to Smarty in controllers/front/SearchController.php file, in your case. Look for $this->context->smarty->assign(array( ... within the initContent() method. I hope this will solve your problem. Link to comment Share on other sites More sharing options...
glenskie16 Posted January 24, 2014 Author Share Posted January 24, 2014 (edited) Thank you for all your help , but now how do i assign that to the array ? GREKA ? this array correct ? { $this->context->smarty->assign(array( 'products' => array(), 'search_products' => array(), 'pages_nb' => 1, 'nbProducts' => 0)); } Edited January 24, 2014 by glenskie16 (see edit history) Link to comment Share on other sites More sharing options...
prestashopninja Posted January 24, 2014 Share Posted January 24, 2014 (edited) If I understand your question well, below is the actual code from the search controller. $this->context->smarty->assign(array( 'products' => $search['result'], // DEPRECATED (since to 1.4), not use this: conflict with block_cart module 'search_products' => $search['result'], 'nbProducts' => $search['total'], 'search_query' => $query, 'instant_search' => $this->instant_search, 'homeSize' => Image::getSize(ImageType::getFormatedName('home')))); and the value for $search comes from the Search::find() method, a few lines upper, as below: $search = Search::find($this->context->language->id, $query, 1, 10, 'position', 'desc'); So, you will probably want to extend the find() method's result to cover your GREKA values, and then, pass it thru Smarty by adding another item to the array such as: $this->context->smarty->assign(array( 'products' => $search['result'], // DEPRECATED (since to 1.4), not use this: conflict with block_cart module 'search_products' => $search['result'], 'nbProducts' => $search['total'], 'search_query' => $query, 'instant_search' => $this->instant_search, 'greka' => $search['greka'], 'homeSize' => Image::getSize(ImageType::getFormatedName('home'))));...or so. Edited January 24, 2014 by prestashopninja (see edit history) Link to comment Share on other sites More sharing options...
glenskie16 Posted January 24, 2014 Author Share Posted January 24, 2014 (edited) $search = Search::find($this->context->language->id, $query, 1, 10, 'position', 'desc', 'greka'); That is what im using , and it still says that it is undefined ? Edited January 24, 2014 by glenskie16 (see edit history) 1 Link to comment Share on other sites More sharing options...
prestashopninja Posted January 24, 2014 Share Posted January 24, 2014 (edited) To be more clear: - If you want, you can start first by trying to pass something static to Smarty. For example: $this->context->smarty->assign(array( 'products' => $search['result'], // DEPRECATED (since to 1.4), not use this: conflict with block_cart module 'search_products' => $search['result'], 'nbProducts' => $search['total'], 'search_query' => $query, 'instant_search' => $this->instant_search, 'greka' => 'test value', 'homeSize' => Image::getSize(ImageType::getFormatedName('home')))); and try to get it the greka value as 'test value' from within Smarty template. Once you do it, next will be to get this greka value dynamic. To do that, return back to the Search class where you can manipulate the result. from: return array('total' => $total,'result' => $result_properties); to (for example) : return array('total' => $total,'result' => $result_properties , 'grekaValues' => $grekaValues); where $grekaValues should be populated using your query result. This way, when you call $search = Search::find($this->context->language->id, $query, 1, 10, 'position', 'desc'); You will now find the greka values as $search['grekaValues'] and you can pass them thru Smarty with ease. PS: I assume you will be doing all these, not altering the core code, but overriding them. Edited January 24, 2014 by prestashopninja (see edit history) Link to comment Share on other sites More sharing options...
glenskie16 Posted January 24, 2014 Author Share Posted January 24, 2014 it wont even do it with a straight value ... im doing this on the product-list.tpl {$product.greka} Link to comment Share on other sites More sharing options...
prestashopninja Posted January 24, 2014 Share Posted January 24, 2014 it wont even do it with a straight value ... im doing this on the product-list.tpl {$product.greka} Following my example above, the static greka value should be accessed as {$greka}, not {$product.greka} . You can see what's passed to Smarty by adding {debug} at the top of your .tpl file. Once you can see the static value of greka, then you can itertate and add the relevant greka values to each $product and then acces them as {$product.greka} Link to comment Share on other sites More sharing options...
glenskie16 Posted January 24, 2014 Author Share Posted January 24, 2014 Undefined index: grekaValues in C:\AllData\websites\Content Management\swaimandsonscart\override\controllers\front\SearchController.php on line 78 line 78 is 'greka' => $search['grekaValues'], do we need to define it in the $search ?? Link to comment Share on other sites More sharing options...
prestashopninja Posted January 24, 2014 Share Posted January 24, 2014 As I clearly stated above, this should be defined as follows: Once you do it, next will be to get this greka value dynamic. To do that, return back to the Search class where you can manipulate the result. from: return array('total' => $total,'result' => $result_properties); to (for example) : return array('total' => $total,'result' => $result_properties , 'grekaValues' => $grekaValues); I think you now have everything to sort out this issue. Please feel free to ask anything if you won't already find the response within what is written above. Link to comment Share on other sites More sharing options...
glenskie16 Posted January 24, 2014 Author Share Posted January 24, 2014 now it is saying it is undefined on this line : return array('total' => $total,'result' => $result_properties , 'grekaValues' => $grekaValues); when i do this code it gives me the GREKA values at the top of the screen ... how would i set this to the return array ? echo $row['GREKA']; Link to comment Share on other sites More sharing options...
glenskie16 Posted January 24, 2014 Author Share Posted January 24, 2014 I know the template file is working because it will get the static "test value" , but when we set it to the $search it is blank because grekaValues is undefined on line 337: return array('total' => $total,'result' => $result_properties , 'grekaValues' => $grekaValues); Link to comment Share on other sites More sharing options...
Recommended Posts