如何在HTML页面中显示每个元素?

huangapple go评论93阅读模式
英文:

How to show each element in the HTML page?

问题

需求

嗨,我正在尝试在HTML页面上显示评论。尽管它在终端中打印出了每个评论,但在HTML页面上却没有显示每个评论。相反,它只显示第一行。

代码信息

在找到数据库中的评论数据后,我使用多维数组在终端中打印了每个评论,但是每次都写xy很困难。这就是为什么我创建了两个for循环和一个第三个循环来为values变量分配数字的原因。

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

我在CommentData{}结构中使用了mdArray来为变量赋值。在打印数据之后,它显示了插入的每个评论,但是当我将此函数返回以在HTML页面上执行时,它只打印第一行。

代码

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>
英文:

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(&quot;fname&quot;),
Lname:   r.FormValue(&quot;lname&quot;),
Email:   r.FormValue(&quot;email&quot;),
Message: r.FormValue(&quot;message&quot;),
}
dt := time.Now()
date := dt.Format(&quot;02-Jan-2006&quot;)
time := dt.Format(&quot;15:04:05&quot;)
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 &lt;= len(values2); i++ {
if i%6 == 0 {
store1 = append(store1, i)
}
}
for j := 6; j &lt;= len(values2); j++ {
if j%6 == 0 {
store2 = append(store2, j)
}
}
for i := 0; i &lt; 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:    &quot;On &quot; + mdArray[0][4],
Time:    &quot;At &quot; + mdArray[0][5],
}
fmt.Println(hello)
return hello
}
} else {
http.Redirect(w, r, &quot;/login&quot;, http.StatusFound)
}
return CommentData{}
}
func FirstBlog(w http.ResponseWriter, r *http.Request) error {
if r.Method == &quot;GET&quot; {
return FirstBlogTmpl.Execute(w, nil)
} else if r.Method == &quot;POST&quot; {
Newsletter(w, r)
hello := SendData(w, r)
return FirstBlogTmpl.Execute(w, hello)
}
return nil
}

HTML

&lt;div&gt;									
{{.}}
&lt;/div&gt;

答案1

得分: 1

将结果放入一个切片中:

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

将函数的返回类型更改为[]CommentData

在模板中遍历结果:

{{range .}}
<div>
{{.}}
</div>
英文:

Put resultatos in a slice:

    … ​
​var hellos []CommentData
​for i := 0; i &lt; 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:    &quot;On &quot; + mdArray[0][4],
​Time:    &quot;At &quot; + 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 .}}
&lt;div&gt;
{{.}}
&lt;/div&gt;

huangapple
  • 本文由 发表于 2022年1月12日 00:19:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/70670038.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定