Populate record in select with group


(Hardeen L) #1

Untitled

Out put will be like this. and the record from database.


(Lutz Horn) #2

Is this a <select> in HTML? Do you build the HTML using Go templates? How is your question related to Go?


(Hardeen L) #3

Yes that is . I want to put the record from database in a group


(Lutz Horn) #4

How do you use Go for this?


(Hardeen L) #5

This is the code. How can i display in template

 func Fill(res http.ResponseWriter, req *http.Request) {
userName:= getUserName(req)
if userName != "" {
	
	UserEmail := userName

	car :=car{}
	records :=[]car{}

	selDB, err :=config.DB.Query("SELECT Car FROM tblcars")
	if err != nil {
		panic(err.Error())
	}
	for selDB.Next() {
		
		var car string
		err = selDB.Scan(&car)
		if err != nil {
			panic(err.Error())
		}	
		car.car = car
		
 		records = append(records, car)				
    }
		data:=Page{
			Title:  "Dashboard",
			UserEmail: UserEmail,
			car: records,
	
		}
		fmt.Println(data)	
		templating.Display(res, "index",data)
} else {
	http.Redirect(res, req, "/", 302)
	return
}

}

	 <select>
	  <optgroup label="Swedish Cars">
		<option value="volvo">Volvo</option>
		<option value="saab">Saab</option>
	  </optgroup>
	  <optgroup label="German Cars">
		<option value="mercedes">Mercedes</option>
		<option value="audi">Audi</option>
	  </optgroup>
	</select>

(Lutz Horn) #6

Which library is templating?


(Hardeen L) #7

“html/template”


(Hardeen L) #8

How to do it in json


(Hardeen L) #9

can i do it this way. How can i make like this from database?

	cars{
	  "name": "Swedish Cars",
	  "car": {
		"Nameofcar" : "Volvo",
		"Nameofcar" : "Saab"
	  },
	  "name": "German Cars",
	  "car": {
		"Nameofcar" : "Mercedes",
		"Nameofcar" : "Audi"
	  }
	}

(Sunny Karira) #10

(system) #11

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