英文:
How to show each element in the HTML page?
问题
需求
嗨,我正在尝试在HTML页面上显示评论。尽管它在终端中打印出了每个评论,但在HTML页面上却没有显示每个评论。相反,它只显示第一行。
代码信息
在找到数据库中的评论数据后,我使用多维数组在终端中打印了每个评论,但是每次都写x
和y
很困难。这就是为什么我创建了两个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("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>
答案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 < 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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论