Is there any way to write this code in a more didactic way?

Asked

Viewed 87 times

-4

Basically the code loops through the database for posts, but it will only post if the scheduled date is less or equal to the current date. He searches the bank for all posts and lists, but those that are scheduled, he will not show. And when there are no scheduled posts or there is nothing in the bank, it says there are no posts.

There are two elses. Hence the question: There is how to use only one?

I tried several ways and I can’t. One is for when there is nothing in the bank, the other for when there are no scheduled posts. One He does not recognize the other. I would like one He only.

<div class="container">
    <% var valor = "No posts!"; %>

    <% if(noticias.length > 0) { %>
        <% for(var i = 0; i < noticias.length; i++) { %>
            <% if(noticias[i].publicacao_agendada <= moment(new Date()).format('YYYY-MM-DD[T]H:mm')) { %>
            <div class="row">
                <div class="col-md-12">
                    <div class="noticia_wrapper">
                        <%= noticias[i].publicacao_agendada %>
                        <span class="noticia_autor"><%=noticias[i].autor%></span>
                        <a href="noticia?id_noticia=<%= noticias[i].id_noticia %>" class="noticia_titulo"><%=noticias[i].titulo%></a>
                        <span class="noticia_data"><%= moment(noticias[i].data_noticia).format(data) %></span>
                        <br />
                        <p class="noticia_resumo">
                            <%=noticias[i].noticia%>
                        </p>
                    </div>
                </div>
            </div>
            <% } else { %>
                <h1><%= valor %></h1>
        <% } %>
    <% } %>
    <% } else { %>
                <h1><%= valor %></h1>
        <% } %>
</div>

For ease of understanding, I, @Leocaracciolo, put something simpler

if(noticias.length > 0) {
    for(var i = 0; i < noticias.length; i++) {
        if(noticias[i].publicacao_agendada <= moment(new Date()).format('YYYY-MM-DD[T]H:mm')) { 

        } else {

        }
    }
} else {

}
  • 6

    Why so many tags? After all, your question is with PHP, Javascript, Java, C# and Node.JS?

  • 2

    That’s definitely not php.....

  • 2

    The if internal seems to be wrong, because would say that has not posts in each one that does not meet the condition? I think should seek to know what the word didactic means, it does not fit in this context.

1 answer

1


I think it makes you feel better:

<div class="container">
<% 
var valor = "No posts!";

if (noticias.length <= 0) 
{
%>
    <h1><%= valor %></h1>
<%
} else {
    for(var i = 0; i < noticias.length; i++) {
        if (noticias[i].publicacao_agendada <= moment(new Date()).format('YYYY-MM-DD[T]H:mm'))
        {
        %>
            <div class="row">
                <div class="col-md-12">
                    <div class="noticia_wrapper">
                        <%= noticias[i].publicacao_agendada %>
                        <span class="noticia_autor"><%=noticias[i].autor%></span>
                        <a href="noticia?id_noticia=<%= noticias[i].id_noticia %>" class="noticia_titulo"><%=noticias[i].titulo%></a>
                        <span class="noticia_data"><%= moment(noticias[i].data_noticia).format(data) %></span>
                        <br />
                        <p class="noticia_resumo"><%=noticias[i].noticia%></p>
                    </div>
                </div>
            </div>
        <%
        }
        else
        {
        %>
            <h1><%= valor %></h1>
        <%   
        }
    }
} 
%>

What I did was basically invert the first if, playing the largest pro final code and making it more readable, and I used the keys on the same level, which makes it easier to use the tags (however I think it is not the correct PHP convention).

This code appears to be a template for rendering html in PHP, where you usually can’t get away from the tag mess much. Now if this is not code only to generate html, I advise you decouple vision control and separate this structure into methods, which makes it much easier to read.

Behold:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.