hello,

I have trouble with mustache php template engine.

I have this:

$get2 = mysqli_query($link,"SELECT * FROM users_table"); 
while($row =  mysqli_fetch_assoc($get2)) { 
$values3 = array('repo' => array( 
   'name3'=>$row['user_name'],
),);  
}

And in template file i try to catch all with:

<select>
{{#repo}}
<option value="{{ name3 }}">{{ name3 }}</option>
{{/repo}}
</select>

Its shows only first name.
I have 2 names in table 'lqlqlq' and 'lqlqlq2', it shows only lqlqlq2.
Maybe i dont understand the way to work with while cycles and array's.

Can someone with experience with this help me ?
PS:
I try with {{ . }} too.

Member Avatar
diafol

Seems you're overwriting your array in the loop on every iteration, so you'll only get the last value in the resultset.

commented: yup +9

Exactly what diafol said. In your loop you're creating the structure each pass through. You want to create your array outside of your loop and then add the new data to the array with the loop.

$values3 = array('repo' => array());
while($row =  mysqli_fetch_assoc($get2)) {
    $values3['repo'][] = array('name3'=>$row['user_name']);
}

The $values3 array will return back a structure like

Array
(
    [repo] => Array
        (
            [0] => Array
                (
                    [name3] => name1
                )

            [1] => Array
                (
                    [name3] => name2
                )

        )

)

Thanks guys! Mustache has a function about this "ArrayIterator" and i read about it after i post topics (~20 minutes after) and i fixed my things with this function.
Is simply to use (read about it in google)

Topic is solved.