How can i make like this result from query


(Hardeen L) #1
var data = &a{
Title: []string{"One", "Two", "Three"},
Article: [][]string{
	[]string{"a", "b", "c","b"},
	[]string{},
	[]string{"f", "g", "h", "i"}},
	}

(Jakob Borg) #2

You need to post more context and an actual question.


(Hardeen L) #3
		package main

		import (
			"html/template"
			"os"
			"fmt"
		)

		type a struct {
			Title   []string
			Article [][]string
		}

		var data = &a{
			Title: []string{"One", "Two", "Three"},
			Article: [][]string{
				[]string{"a", "b", "c","b"},
				[]string{},
				[]string{"f", "g", "h", "i"}},
		}


		var tmplSrc = `
		{{range $i, $a := .Title}}
		  Title: {{$a}}
		  {{range $article := index $.Article $i}}
			Article: {{$article}}.
		  {{end}}
		{{end}}`

		func main() {
		fmt.Println(data)
			tmpl := template.Must(template.New("test").Parse(tmplSrc))
			tmpl.Execute(os.Stdout, data)
		}

I want to replace this

var data = &a{
			Title: []string{"One", "Two", "Three"},
			Article: [][]string{
				[]string{"a", "b", "c","b"},
				[]string{},
				[]string{"f", "g", "h", "i"}},
		}

to sql query the table is like this
Untitled

Thanks in advance


(Norbert Melzer) #4

So if I understand you correctly, you have entries in table 1 which have a one to many relationship into table 2.

Table 2 uses the title to reference into table 1 and has no unique identifier on its own?

Also you want have a slice of things from table1 directly aside to a list of things from table2 and use the same index to access things that belong together?

You should stop right here and rething your database.

  1. Use a proper unique id in table2, even though it might not feel as if it were necessary right now.
  2. Use the id from table1 as a foreignkey in table2 instead of the title
  3. Use a struct for a single entry (eg. type Table1 struct {Title string; Article []Table2} (and a proper definition of Table2 as well of course)
  4. If you want to query mutliple things from Table1, then return a []Table1 instead of your originally proposed thing.

(Hardeen L) #5

so this is the changes on the fields

Untitled

Im not sure sir but im trying to arrive is like this…

Title: One

Article: a.

Article: b.

Article: c.

Article: b.

Title: Two

Title: Three

Article: f.

Article: g.

Article: h.

Article: i.


(Norbert Melzer) #6

Sorry, but I do not understand your example.

Can you add some formatting that makes it easier to read? Some code that is equivalent to what you expect, how you tried to get it into that structure? etc?


(Hardeen L) #7
package main

import (
	"html/template"
	"os"
	"fmt"
)

type a struct {
	Title   []string
	Article [][]string
}

var data = &a{
	Title: []string{"One", "Two", "Three"},
	Article: [][]string{
		[]string{"a", "b", "c","b"},
		[]string{},
		[]string{"f", "g", "h", "i"}},
}


var tmplSrc = `
{{range $i, $a := .Title}}
  Title: {{$a}}
  {{range $article := index $.Article $i}}
	Article: {{$article}}.
  {{end}}
{{end}}`

func main() {
fmt.Println(data)
	tmpl := template.Must(template.New("test").Parse(tmplSrc))
	tmpl.Execute(os.Stdout, data)
}

(system) #8

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.