|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
Wajpas Smarty Rookie
Joined: 04 Nov 2020 Posts: 9
|
Posted: Sun Dec 27, 2020 3:14 pm Post subject: foreach with more than 1 array? |
|
|
HEy folks,
another foreach-question:
got {foreach} already working for ONE listing. But now i read out a mysql-table and put it in more than one array. For sure i looked at thoses arrays and it works, there is some text inside.
Working like this:
Code: |
$mess = "SELECT * FROM ".$pre"_notice`WHERE `start_time` <= '$today' ";
$mess_out = mysqli_query($link, $mess);
while($row = mysqli_fetch_assoc($mess_out)) {
$titel_arr[] = $row['titel'];
$note_arr[] = $row['note'];
} |
Brings out both arrays as it should do!
And now i'm trying to shwo thoses arrays in a table like this:
Code: |
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="70%">
<tr>
<td width="20%"><h2>Titel</h2></td>
<td width="50%"><h2>Notitz</h2></td>
</tr>
{foreach from=$titel_arr item=$titel}
{foreach from=$note_arr item=$note}
<tr>
<td width="20%">{eval var=$titel}{/foreach}</td>
<td width="50%">{eval var=$note}{/foreach}</td>
</tr>
</table>
|
but that's realy no view of the data, that i wanna have^^...
So hope you coould help me once more in this case
|
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Mon Dec 28, 2020 8:40 pm Post subject: |
|
|
I would recommend using the association that is the array.
Code: | $my_rows = mysqli_fetch_all($mess_out); |
Then, in the template:
Code: | {foreach $my_rows as $row}{$row.titel}:{$row.note}{/foreach} |
|
|
Back to top |
|
Wajpas Smarty Rookie
Joined: 04 Nov 2020 Posts: 9
|
Posted: Mon Dec 28, 2020 9:42 pm Post subject: |
|
|
ok, first thx for your help.
Tried this now but it dosn't work - shows nothing now:
PHP: Code: |
$mess = "SELECT * FROM ".$pre."_notice WHERE `start_time` <= '$today' ";
$mess_out = mysqli_query($link, $mess);
$note_rows = mysqli_fetch_all($mess_out);
$smarty->assign('note_rows',$note_rows); |
TPL: Code: |
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="70%">
<tr>
<td width="20%"><h2>Titel</h2></td>
<td width="50%"><h2>Notitz</h2></td>
<td width="30%"><h2>ID</h2></td>
</tr>
{foreach $note_rows as $row}
<tr>
<td width="20%">{$row.titel}</td>
<td width="50%">{$row.note}</td>
<td width="30%">{$row.id}</td>
</tr>
{/foreache}
</table> |
|
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Mon Dec 28, 2020 10:18 pm Post subject: |
|
|
Please double-check the spelling of {/foreach}.
If there is doubt that the $note_rows has any data, have PHP do a var_dump() of it. |
|
Back to top |
|
Wajpas Smarty Rookie
Joined: 04 Nov 2020 Posts: 9
|
Posted: Tue Dec 29, 2020 12:40 pm Post subject: |
|
|
bsmither wrote: | Please double-check the spelling of {/foreach}.
If there is doubt that the $note_rows has any data, have PHP do a var_dump() of it. |
ok... damn writing
just foreache without "ending-e" ....
Thanks for that - i just didn't see...
and thankt for the hint about print_r... I just wanted to look, if there is anything in, but in that way i saw the keys ([0],[1],[2], ...) and not those column-titels... thats why i changed it in my TPL to thoses "numberkeys" and now i works as it should!!!!
Thousend thanks^^ |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Tue Dec 29, 2020 6:16 pm Post subject: |
|
|
According to this page:
https://www.php.net/manual/en/mysqli-result.fetch-all.php
we need to add an option to the command:
Code: | $note_rows = mysqli_fetch_all($mess_out, MYSQLI_ASSOC); |
This will give you the column names instead of a numerical index. |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|