Home > OS >  How to show each element in the HTML page?
How to show each element in the HTML page?

Time:01-13

Requirement

Hey there, I am trying to show the comments on an HTML page. Although it prints every comment in the terminal but does not show every comment on an HTML page. Instead, it shows only the first row.

Code info

After finding the data of comments in the database, I printed every comment in the terminal using the multidimensional array but it was difficult to write x and y each time. That's why I created two for-loops and a third loop to assign numbers to the values variable.

mdArray := [][]string{
    values[0:4],
    values[4:8],
        // x:y        
}

I used mdArray in the CommentData{} structure to assign values to the variables. After printing the data, it shows every comment that is inserted but when I return this function to be executed on the HTML page, it only prints the first row.

Code

type CommentData struct {
    Fname   string
    Lname   string
    Email   string
    Message string
    Date    string
    Time    string
}

func SendData(w http.ResponseWriter, r *http.Request) CommentData {
    note := models.AddComment{
        Fname:   r.FormValue("fname"),
        Lname:   r.FormValue("lname"),
        Email:   r.FormValue("email"),
        Message: r.FormValue("message"),
    }
    dt := time.Now()
    date := dt.Format("02-Jan-2006")
    time := dt.Format("15:04:05")
    values1 := [6]string{note.Fname, note.Lname, note.Email, note.Message, date, time}

    _, match := database.FindAccount(note.Fname, note.Lname, note.Email)
    if match {
        database.InsertComment(values1)
        values2 := database.FindComment(note.Fname, note.Lname, note.Email)
        var store1, store2 []int
        for i := 0; i <= len(values2); i   {
            if i%6 == 0 {
                store1 = append(store1, i)
            }
        }
        for j := 6; j <= len(values2); j   {
            if j%6 == 0 {
                store2 = append(store2, j)
            }
        }
        for i := 0; i < len(store2); i   {
            mdArray := [][]string{
                values2[store1[i]:store2[i]],
            }
            // fmt.Println(mdArray[0][3])
            hello := CommentData{
                Fname:   mdArray[0][0],
                Lname:   mdArray[0][1],
                Email:   mdArray[0][2],
                Message: mdArray[0][3],
                Date:    "On "   mdArray[0][4],
                Time:    "At "   mdArray[0][5],
            }
            fmt.Println(hello)
            return hello
        }
    } else {
        http.Redirect(w, r, "/login", http.StatusFound)
    }
    return CommentData{}
}

func FirstBlog(w http.ResponseWriter, r *http.Request) error {
    if r.Method == "GET" {
        return FirstBlogTmpl.Execute(w, nil)
    } else if r.Method == "POST" {
        Newsletter(w, r)
        hello := SendData(w, r)
        return FirstBlogTmpl.Execute(w, hello)
    }
    return nil
}

HTML

<div>                                   
    {{.}}
</div>

CodePudding user response:

Put resultatos in a slice:

    … ​
   ​var hellos []CommentData
   ​for i := 0; i < len(store2); i   {
       ​mdArray := [][]string{
           ​values2[store1[i]:store2[i]],
       ​}
       ​// fmt.Println(mdArray[0][3])
       ​hello := CommentData{
           ​Fname:   mdArray[0][0],
           ​Lname:   mdArray[0][1],
           ​Email:   mdArray[0][2],
           ​Message: mdArray[0][3],
           ​Date:    "On "   mdArray[0][4],
           ​Time:    "At "   mdArray[0][5],
       ​}
       ​fmt.Println(hello)
       ​hellos = append(hellos, hello)
   ​}
   ​return hellos
   ​…

Change function return type return type []CommentData.

Range over the resultatos in the template

{{range .}}
<div>
{{.}}
</div>
  •  Tags:  
  • Related