Hello! help to understand: There is a table in the database:

id f_gdpec w_gdpec url_gdpec p_gdspec 1 Москва Красноярск www.test.ru 450 2 Москва Красноярск www.test.ru 900 3 Москва Красноярск www.test.ru 600 

It is necessary to pull data from the database using php and display it using smarty tools. that's what i did: module.php :

 <?php function mod_gdspec($module_id){ $inCore = cmsCore::getInstance(); // подключаем ядро $inDB = cmsDatabase::getInstance(); // подключаем базу $cfg = $inCore->loadModuleConfig($module_id); //код модуля //Формируем запрос: $sql = "SELECT f_gdpec, w_gdpec, url_gdpec, p_gdspec FROM cms_gdspec"; //Выполняем запрос: $result = $inDB->query($sql) ; /* Считаем количество выведенных записей */ if ($inDB->num_rows($result)){ $items = array(); //Получаем данные из ответа БД: while ($item=$inDB->fetch_assoc($result)){ $items[]=$item; } } $smarty = $inCore->initSmarty('modules', 'mod_gdspec.tpl'); $smarty->assign('items', $items); $smarty->display('mod_tags.tpl'); return true; } ?> 

template:

  {foreach item=item from=$items} <div class="mod_latest_entry"> <div class="mod_latest_f_gdpec"> {$item.f_gdpec} </div> <div class="mod_latest_w_gdpec" > {$item.w_gdpec} </div> </div> {/foreach} 

I can not understand what is wrong. errors would be displayed, maybe I could dig. The module does not see the template and accordingly the data is not transferred to the template I ask for your help.

  • one
    Semicolon after $ items = $ item is not .. And it turns out that I myself was sealed :) It is necessary to $ items [] = $ item; - Photon
  • it helped, only information is not displayed in the template. :( - intertex
  • how strange, for some reason, I have a different pattern defined. and even if I forcefully set the required template in the database, it still doesn’t show anything. - intertex

2 answers 2

PHP:

 ... if ($inDB->num_rows($result)){ $items = array(); //Получаем данные из ответа БД: while ($item=$inDB->fetch_assoc($result)){ $items[] = $item; } } ... $smarty->assign('items', $items); ... 

Smarty:

 {foreach item=item from=$items} <div class="mod_latest_entry"> <div class="mod_latest_f_gdpec"> {$item.f_gdpec} </div> <div class="mod_latest_w_gdpec" {$item.w_gdpec} </div> </div> {/foreach} 

Is it really so difficult to read the documentation? Or are you just used to doing everything for you?

  • I would be very grateful if you ate good documentation with me. - intertex
  • Here you used the third bracket in your PHP example, I understand correctly that it ends here: function mod_module_main ($ module_id)? And do not need to check for true and false? - intertex
  • No, the third bracket - I missed it :) Smarty documentation: smarty.net/manual/ru - Photon
  • In the description of the question, I wrote how I did it, and still it does not work. And if I connect this unit, then the rest of the page does not load. - intertex

here is the working version, finished :) <? php

 function mod_gdspec($module_id){ $inCore = cmsCore::getInstance(); // подключаем ядро $inDB = cmsDatabase::getInstance(); // подключаем базу $cfg = $inCore->loadModuleConfig($module_id); //код модуля //Формируем запрос: $sql = "SELECT * FROM cms_gdspec"; //Выполняем запрос: $result = $inDB->query($sql) ; /* Считаем количество выведенных записей */ if ($inDB->num_rows($result)){ $items = array(); //Получаем данные из ответа БД: while ($gdt = $inDB->fetch_assoc($result)){ $items[] = $gdt; } } $smarty = $inCore->initSmarty('modules', 'mod_gdspec.tpl'); $smarty->assign('items', $items); $smarty->display('mod_gdspec.tpl'); return true; } ?> 

template: {foreach key = id item = gdt from = $ items} <div class = "mod_latest_entry"> <div class = "mod_latest_f_gdpec"> {$ gdt.f_gdpec} $ gdt.w_gdpec} </ div> </ div> {/ foreach}

slightly different from the proposed :) but the essence is the same :)