Algorithm to display an image on a webpage every third week

I am working on a Grails project where I need to display team icons on the home page based on the current week. There are three different images for three different teams, and the teams rotate every 3 weeks. For example, Team 1's logo will show on the first (1) and last (4) weeks of the month, and so on. I initially attempted to store all 52 weeks along with team numbers in a database table and retrieve them using sessions.

Here is the code snippet from my service:

    sql.query("SELECT * FROM tbl_ImageForTeamUser where weekno=?",[wkno]) 
    { rs ->             
        while (rs.next()) {
            def userinf = new UserInfo()
            userinf.weekno= rs.getInt("weekno")
            userinf.teamname= rs.getString("teamname")
            userinf.teamno= rs.getInt("teamno")
            items.add(userinf)
        }
      }

And then saving it in the session:

session.weekno= items[0].teamno()

The current UI implementation includes logic like this:

<tr>
            <g:if test="${session.weekno == "1" }">
                 <td class="team1" align="right"  nowrap></br></br></td>
             </g:if>
            <g:if test="${session.weekno == "2"}">
                <td class="team2" align="right"  nowrap></br></br></td>
            </g:if>
            <g:if test="${session.weekno == "3" }">
                 <td class="team3" align="right"  nowrap></br></br></td>
             </g:if>

However, I feel that this solution is not optimal. I am seeking better coding suggestions to achieve the same functionality without relying on the database and handling it more efficiently on the UI side.

Answer â„–1

Utilizing the cal.get(Calendar.WEEK_OF_MONTH) method will provide you with a number between 1 and 5 representing the week of the month.

This can be used to display images in the correct sequence without the need for a database table, as there are only 3 unique orderings based on your query.

Calendar cal = Calendar.getInstance()
def weekOfMonth = cal.get(Calendar.WEEK_OF_MONTH)
def teamImageCountShown = weekOfMonth % 3
if(teamImageCountShown==1){
    //Display 1 2 3
}
if(teamImageCountShown==2){
    //Display 2 3 1
}
if(teamImageCountShown==0){
    //Display 3 1 2
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Identifying improper encoding in a MySQL database using Chardet through JDBC

Recently, our mysql database was transferred from Latin1 to UTF8. Despite trying multiple methods to convert it, none seemed to work without causing data loss or producing undesirable results. This led me to speculate that we might have various encodings i ...

Show text upon hovering using jQuery

I am currently exploring jquery and trying to implement a functionality where text is displayed upon hovering over a div element. My basic html page consists of squares, with one of them rotating when a button is clicked. I now want to show some text withi ...

Utilizing jQuery time intervals within a jQuery click event: A step-by-step guide

For my dropdown menu project, I am facing an issue where the menu bar is not reappearing after collapsing the dropdown. It seems that the jQuery function responsible for toggling classes within a time interval is not executing properly. Specifically, the " ...

Eliminating the save password prompt upon form submission in Firefox with the use of JavaScript

Is there a way to submit a form without triggering the browser's save password popup? This issue seems to be affecting Firefox version 59. I am attempting to log in to a form that includes two password input fields: <input type="password" name="l ...

Receiving HTTP POST data using Classic ASP script

I'm currently working on a project and have come across an area where I am facing some challenges. Despite my best efforts, I haven't been able to find a solution using Google. In my ASP web application, I've added an HTML canvas that I nee ...

Suddenly, both the web element and driver are marked with squiggly lines underneath

While working with Selenium and Java in Eclipse, I suddenly noticed squiggly lines under the driver and webelements in my code. I attempted to hover over them and see if any suggestions would pop up, but unfortunately, nothing seemed to provide a clear sol ...

Express.js in nodejs has encountered an issue known as Error [ERR_HTTP_HEADERS_SENT], which occurs when attempting to set headers after they have already been

An issue with setting headers after they have been sent to the client is causing the Error [ERR_HTTP_HEADERS_SENT]. Despite attempting to handle it with a try-catch block, the error persists. Can someone provide insights on how to resolve this and its unde ...

Can image data be extracted from a canvas without needing to physically render the canvas?

I am currently working on an Angular application that utilizes Chart.js to create dynamic charts in various views. Additionally, the application has the capability to export data to a PDF format using the PDFMake JavaScript library, which is a basic tool a ...

"Using a triangular background shape in JavaScript instead of a traditional circular

I want to add a unique effect to my site inspired by the AnimatedHeaderBackgrounds demo available at this link. My twist on this effect involves using upward-moving triangles instead of circles. I've explored various resources, including Stack Overfl ...

What could be the issue with my JSON file?

I am currently utilizing the jQuery function $.getJson. It is successfully sending the desired data, and the PHP script generating the JSON is functioning properly. However, I am encountering an issue at this stage. Within my $.getJSON code, my intention ...

Implement Google Apps Script Array in HTML form for autocompleting datalist suggestions

I have successfully created a Google Scripts function that generates an array from a column in a Google Sheet by filtering out any spaces. However, I'm facing a challenge in figuring out how to pass this variable to my HTML form so that I can set up a ...

The proper method for updating data on a backend API using Axios and Vue

I am working on a Vue application that includes several form fields. I want to ensure that any changes made by the user are saved in real-time to a backend database using a REST API with Axios, without requiring the user to click a save button. I have two ...

What are the NSL KDD Characteristics Extracted from Real-Time Packet Data?

element, I am looking to retrieve unprocessed data from pcap and wincap. As I plan to validate it against a neural network trained with the NSLKDD dataset, I am curious about the process of obtaining the 41 attributes from the raw data. Alternatively, I a ...

Display a div when hovering over another div

I have a menu that looks like this: https://i.sstatic.net/WqN33.png and I want to create a hover effect where another div shows up over each item, similar to this: https://i.sstatic.net/JRaoF.png However, I can't seem to figure out how to implemen ...

Debounce function fails to properly function when the state is modified within the same function

I've been working on implementing a search-as-you-type feature and I want to debounce the function search that handles API calls. Everything works well when I only call the debounced_search function within the event handler, but I also need to update ...

Locating the element idNumber within the following HTML snippet

<div id="rightContent" style="display: inline-block; width: 700px; vertical-align: top;"> <div> <h1><em class="SomethingHeading">Something</em></h1> </div> <d ...

How can I locate the object labeled as "Current" in Selenium?

I am looking to use Selenium to navigate a menu using arrow keys—beginning with clicking the top menu item and then pressing "DOWN," "DOWN," ... The issue is that you always need to provide a specific element to send the "DOWN" key to. Is there a way t ...

Mongoose search operation coming up with a blank array

Whenever I utilize $search in mongoose, it returns an empty array. Data Model const mongoose = require('mongoose'); const studentSchema = new mongoose.Schema({ name: { type: String }, }); studentSchema.index({ name: 'text' }); con ...

Retrieve the JSON object with an AJAX request that contains three arrays, then transfer these arrays to JavaScript

A web page I designed generates the following content: <script> var JSONObject = { "groups":['1210103','1210103','1210103','1210405'], "prices":['279,00','399,00',&ap ...

Obtaining the text content of a <div> element when clicked using Selenium

I am trying to extract the email address from the code snippet below, but I am unsure of how to do it. Any assistance would be greatly appreciated! <div class="taLnk hvrIE6 fl" onclick="ta.trackEventOnPage('Listing', 'Email', 774169 ...