Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

How to loop through array

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
whofarted75
Smarty n00b


Joined: 15 Sep 2020
Posts: 2

PostPosted: Tue Sep 15, 2020 9:45 pm    Post subject: How to loop through array Reply with quote

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
View user's profile Send private message
whofarted75
Smarty n00b


Joined: 15 Sep 2020
Posts: 2

PostPosted: Wed Sep 16, 2020 5:35 pm    Post subject: Reply with quote

Wow, I thought this place was busier than this. No one here? Confused
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 270
Location: West Coast

PostPosted: Wed Sep 16, 2020 7:08 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP