|
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 |
whofarted75 Smarty Rookie
Joined: 15 Sep 2020 Posts: 6
|
Posted: Tue Sep 15, 2020 9:45 pm Post subject: How to loop through array |
|
|
Hello I'm new to Smarty & relatively new to php.
Can anyone show me how to loop this array properly?
Code: | // Pull all the post to display
$sql = "SELECT *
FROM complaint
ORDER BY c_time DESC
LIMIT 20";
$result = $db->sql_query($sql);
$complaints = array();
while($thisdata = $result->fetch_assoc())
{
$complaints['DAY'][date("M j, Y", $thisdata['c_time'])][] = array(
'ID' => $thisdata['id'],
'TIME' => date("g:i a", $thisdata['c_time']),
'COMPLAINT' => nl2br($thisdata['complaint']),
'COMPLAINT_EDIT'=> $thisdata['complaint']);
}
$smarty->assign('COMPLAINTS', $complaints);
$smarty->display('test.html'); |
partial sample of the array:
Code: |
Array
(
[DAY] => Array
(
[Sep 14, 2020] => Array
(
[0] => Array
(
[ID] => 63
[TIME] => 4:02 pm
[COMPLAINT] => test 7
[COMPLAINT_EDIT] => test 7
)
[1] => Array
(
[ID] => 62
[TIME] => 11:10 am
[COMPLAINT] => test 6 here we go!
[COMPLAINT_EDIT] => test 6 here we go!
)
[2] => Array
(
[ID] => 61
[TIME] => 10:58 am
[COMPLAINT] => test 5, yup, yet again man!
[COMPLAINT_EDIT] => test 5, yup, yet again man!
)
)
[Sep 13, 2020] => Array
(
[0] => Array
(
[ID] => 60
[TIME] => 10:30 pm
[COMPLAINT] => test 4
[COMPLAINT_EDIT] => test 4
)
[1] => Array
(
[ID] => 59
[TIME] => 8:26 pm
[COMPLAINT] => test 3
[COMPLAINT_EDIT] => test 3
)
etc...... |
I've been looking through the documentation & searching google for help & none of the examples seem to work for me. I don't understand what's going on. TIA |
|
Back to top |
|
whofarted75 Smarty Rookie
Joined: 15 Sep 2020 Posts: 6
|
Posted: Wed Sep 16, 2020 5:35 pm Post subject: |
|
|
Wow, I thought this place was busier than this. No one here? |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Wed Sep 16, 2020 7:08 pm Post subject: |
|
|
To start the first level iteration, we have $COMPLAINTS['DAY']. The 'DAY' key will never change, so we can go straight to it:
{foreach $COMPLAINTS.DAY as $date => $events}
Now we are working with each set of daily events. We can use $date as a header, or whatever, as we iterate through the second level, the set of $events.
{foreach $events as $incident}
We are at the main detail level.
{$incident.ID}, {$incident.TIME}, $incident.COMPLAINT}, and {$incident.COMPLAINT_EDIT}
Then, to close out the looping:
{/foreach}
{/foreach} |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Thu Sep 24, 2020 9:53 am Post subject: |
|
|
Quick way to see the array structure in template is to run Code: | {$array|print_r:true} |
|
|
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
|