index imprimir for array php foreach smarty

php - imprimir - Mostrar elementos de matriz en smarty



smarty foreach count (3)

No dio un resultado de ejemplo de lo que quería, así que lo adivinaré. Suponiendo que está buscando el siguiente resultado para el conjunto de arriba:

0: Petric Naughton<br /> 1: Nitish Dolakasharia<br /> 2: Martin Rom<br /> 3: Steve Wough<br /> 4: Liz Hurley<br />

Podrías hacer esto:

{foreach from=$rand_pro item=pro key=pro_key} {$pro_key}: {$pro.bz_pro_first_name} {$pro.bz_pro_last_name}<br /> {/foreach}

Si estoy asumiendo incorrectamente, actualice su pregunta con el resultado que espera sea más claro.

He fusionado dos resultados de mysql:

while($rs_1 = mysql_fetch_array($r1)) { $arr1[] = $rs_1; } while($rs_2 = mysql_fetch_array($r2)) { $arr2[] = $rs_2; } $resN = array_merge($arr1,$arr2);

var_dump($resN) muestra el siguiente resultado:

array(5) { [0] => array(4) { [0] => string(6) "Petric" ["bz_pro_first_name"] => string(6) "Petric" [1] => string(8) "Naughton" ["bz_pro_last_name"] => string(8) "Naughton" } [1] => array(4) { [0] => string(6) "Nitish" ["bz_pro_first_name"] => string(6) "Nitish" [1] => string(12) "Dolakasharia" ["bz_pro_last_name"] => string(12) "Dolakasharia" } [2] => array(4) { [0] => string(6) "Martin" ["bz_pro_first_name"] => string(6) "Martin" [1] => string(3) "Rom" ["bz_pro_last_name"] => string(3) "Rom" } [3] => array(4) { [0] => string(5) "Steve" ["bz_pro_first_name"] => string(5) "Steve" [1] => string(5) "Wough" ["bz_pro_last_name"] => string(5) "Wough" } [4] => array(4) { [0] => string(3) "Liz" ["bz_pro_first_name"] => string(3) "Liz" [1] => string(6) "Hurley" ["bz_pro_last_name"] => string(6) "Hurley" } }

Se supone que debo mostrarlos en smarty así que:

assign_values ​​(''rand_ad'', $ resN);

Ahora traté de mostrar en smarty de esta manera:

{foreach name=outer item=pro from=$rand_pro} {foreach key=key item=item from=$pro} {$key}: {$item}<br /> {/foreach} {/foreach}

Muestra los resultados pero en serie. Necesito extraer los valores en algunas posiciones. Entonces, ¿cómo puedo extraer los valores, por ejemplo, nombre, apellido, etc.?


Usa {foreach} para mostrar la matriz en smarty. y para la conexión de la base de datos use ADODB .

En el archivo php:

type $smarty->assign->("arrname",$aodDBconn->Execute($sql)).

Funcionará.


esto podría ser muy útil para acceder a elementos multidimensionales en smarty sin necesidad de controlador

{foreach from = $ all_categories item = cat} {foreach from = $ cat.children item = categ} {if {$ categ.id_category! = 187 && $ categ.id_category! = 188 && $ categ.id_category! = 189 && $ categ.id_category! = 190}} $ show_count} class = "simplemenu-container {$ menu.custom_class} menu-parent cat-link-orther" style = "display: none" {else} class = "simplemenu-container {$ menu.custom_class} menu-parent {$ style.second_background_hover} {$ style.first_color} {$ style.second_color_hover} "{/ if}> getCategoryLink ($ categ.id_category, $ categ.link_rewrite) | escape: ''html'': ''UTF-8''} "href =" {$ link-> getCategoryLink ($ categ.id_category, $ categ.link_rewrite) | escape: ''html'': ''UTF-8''} "title =" {$ categ.name | escape: ''html'': ''UTF-8''} "> {$ categ.name | escape: ''html'': ''UTF-8''}

    {foreach from = $ categ.children item = subcat}
  • getCategoryLink ($ subcat.id_category, $ subcat.link_rewrite) | escape: ''html'': ''UTF-8''} "href =" {$ link-> getCategoryLink ($ subcat.id_category, $ subcat.link_rewrite) | escape: '' html '':'' UTF-8 ''} "title =" {$ subcat.name | escape:'' html '':'' UTF-8 ''} "> {$ subcat.nombre | escape:'' html '':'' UTF-8 '' }
  • {/para cada}

</li> {/if} {/foreach} {/foreach}

Aquí muestro tres elementos tridimensionales en una lista con alguna condición