Using value from previous entry within a repeater

What is the best way to include a value from a previous entry within a repeater?

I have a repeater that pulls a list of entries from a DB which include a DATETIME.

Per entry I want to calculate the time difference with the previous entry (* in code)

Can this be done within the repeater? Or should I recall the SC and pull the previous entry using index etc.?

This is my code:

        <div dmx-repeat:repeatlog="get_log.data.get_log.where('userid', userselection.value, '==')">
            <div class="d-flex flex-column mb-2 bg-secondary p-1">
                <div class="d-flex p-0 m-0 justify-content-between">
                    <p class="m-0 pt-0 pb-0 ps-0 pe-4">userid: {{userid}}</p>
                    <p class="text-light m-0">entry time: {{time}}</p>
                    <p class="text-light m-0">time since last entry: {{time}} * </p>
                </div>
                <p class="text-light m-0 p-0">action: {{action}}</p>
            </div>
        </div>

I would do this in a DB view that you query or a repeat server-side. Deliver the time difference to the client as a value in each of the entries in the array

2 Likes

Yes, I would agree… an app with Wappler is waaaaay faster if you use database views to deliver all the info you need and use the front end just to display it!

1 Like

Thanks both, that makes sense. I am still struggling a bit with the server side repeat. This is what I have so far, suggestions where I go wrong?

(the condition is based on security provider (which I took out in this screen shot))

Not exactly sure what your desired result is but I can tell you what you’ve done:

Your condition says, only do this for the logged in user if their primary id is 1…so only one possible user.

You are then doing a query of your database and then attempting to repeat only the first row. I’m assuming you actually want to repeat get_log instead as it will be an array of values. Once you do that you’ll be able to select the value you are looking for inside the repeat.

Thanks for your comment @mebeingken

The condition is there so only the admin (user 1) can engage with the server connect (extra security step) – its not the point here, should have taken it out for this example.

What I want to achieve is as described in my initial post:

  1. I have a repeater that pulls a list of entries from a DB which include a DATETIME.

  2. Per entry I want to calculate the time difference with the previous entry.

This is very easy to do in JS with a FOR loop etc.
But wanted to do this purely in Wappler, and in the server-side (as kindly suggested by @bpj and @Antony).

They made it sound easy ;), but I can’t seem to get the logic of the repeater and how I can call back a previous entry.

I am a bit lost here… so any suggestions (or existing tutorials that deal with similar issues) very welcome!

Hey @ant… (Great name!)
I do this kinda thing in a server action using a repeat and a session variable to store the time from then previous loop… I hope that helps!

Thanks, you too @Antony :wink:

Make sense, but could you clarify what do you mean by “previous loop” ?

Sure! Something like this…

create $_SESSION s_last_datetime;
s_last_datetime = '';
repeat on your data
   compare s_last_date_time with this_datetime
      Do what you need to do;
   s_last_datetime = this_datetime;

The session variable value is global so will be retained for the next run around the loop.

Hope that makes sense!

Antony.