php - Twig - Loop returns only 1 result -
i'm trying make loop in php while using twig , inside loop, creating parameter contains record database query.
the problem when use parameter in html file, return 1 record while loop, if there 3 or 4 or more..
this php code have:
public function getwidgetsbyname($name) { global $params; $get = $this->db->query("select * profile_items category = 'widget' , username = '". $name ."'"); if($get) { while($key = $get->fetch()) { $params["profile_widget_name"] = $key['name']; } } }
and html parameter in html twig rendered file:
{{ profile_widget_name }}
the paremeters rendered how supposed rendered:
echo $twig->render('app/views/'. $_request['p'] .'.html', $params);
and yes $params variable array, in config file first gets used $params = array("..." => "..."); , add things array doing $params["..."] = "...";
so, hope can me.
thanks in advance, best regards.
at moment, value of $params["profile_widget_name"]
1 string. every time go through while
loop, overwrite previous value of key current value.
so when pass $params
twig, value of profile_widget_name
value of name
in last row of database selected.
i think want instead value of profile_widget_name
array. every time go through loop, current value of name
added array, instead of overwriting it.
you doing like:
$params["profile_widget_names"][] = $key['name'];
now in twig template, you'll need like:
{% profile_widget_name in profile_widget_names %} {{ profile_widget_name }} {% endfor %}
using multiple parameters
if want multiple parameters on there, can this:
$params["profile_widgets"][] = [ 'pos_x' => $key['pos_x'], 'name' => $key['name'], ];
and in twig:
{% profile_widget in profile_widgets %} name: {{ profile_widget.name }} pos x: {{ profile_widget.pos_x }} {% endfor %}
Comments
Post a Comment