How to populate the data from the database


(Hardeen L) #1

Dear Expert, How can populate the data from the database mixed with other variable. thanks

go file

type User struct {
			UserEmail  string
		}
		type Menu struct{
			MenuName string
		}
		type Page struct {
			Title string
			Email User
			UserMenu []Menu
		}

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

				UserMenu :=Menu{}
				records :=[]Menu{}
				
				selDB, err :=config.DB.Query("SELECT menuname FROM tblmenu	")
				if err != nil {
					panic(err.Error())
				}
				for selDB.Next() {
					var menuname string
					err = selDB.Scan(&menuname)
					if err != nil {
						panic(err.Error())
					}	
					UserMenu.MenuName = menuname
					records = append(records, UserMenu)				
				}
					data:=Page{
						Title:  "Dashboard",
						Email:User{UserEmail:UserEmail},
						UserMenu: []Menu{
								{MenuName:UserMenu.MenuName},
						},
					}
					fmt.Println(data)	
					templating.Display(res, "index",data)
			} else {
				http.Redirect(res, req, "/", 302)
				return
			}
		}

html file

{{.Title}} <br>
{{.Email.UserEmail}}<br>


{{range .Menu}}
	<li>{{.MenuName}}</li>
{{end}}

Thanks in advance.


(Lutz Horn) #2

I am not quite sure what your question is about. Do you get any error when running this code? Do you get the output you expect? If not, what do you expect?


(Sunny Karira) #3

What do you mean by mixed?. You just take out the data you need with the help of the driver no?
You need to know the structure of the data.


(Hardeen L) #4

i mean populate category and sub category.


(Sibert) #5

Using JOIN or UNION in the sql query?


(Hardeen L) #6

thanks


(Hardeen L) #7
func Dashboard(res http.ResponseWriter, req *http.Request) {
userName:= getUserName(req)
if userName != "" {
	
	UserEmail := userName

	UserMenu :=Menu{}
	records :=[]Menu{}


	selDB, err :=config.DB.Query("SELECT menuname,icon,level FROM tblmenu order by mainmenu")
	if err != nil {
		panic(err.Error())
	}
	for selDB.Next() {
		var level int
		var menuname,icon string
		err = selDB.Scan(&menuname,&icon,&level)
		if err != nil {
			panic(err.Error())
		}	
		UserMenu.MenuName = menuname
		UserMenu.Icon = icon
		UserMenu.Level = level
 		records = append(records, UserMenu)				
    }
		data:=Page{
			Title:  "Dashboard",
			UserEmail: UserEmail,
			UserMenu: records,
	
		}
		fmt.Println(data)	
		templating.Display(res, "index",data)
} else {
	http.Redirect(res, req, "/", 302)
	return
}
 }

In submenu how can i display. thanks

 Submenu
              <li class="treeview">
                  <a href="#"><i class="fa {{.Icon}}"></i> <span>{{.MenuName}}</span>
                    <span class="pull-right-container">
                        <i class="fa fa-angle-left pull-right"></i>
                      </span>
                  </a>
                  <ul class="treeview-menu">
                      <li><a href="#">{{.MenuName}}</a></li>
                  </ul>
                </li>
              
 Main Menu
             <li class="active"><a href="#"><i class="fa {{.Icon}}"></i> <span>{{.MenuName}}</span>

(Sibert) #8

I interpret this as three languages or levels / layers involved. Go, SQL and HTML. In which level is the problem? Fetching data or displaying data?


(Hardeen L) #9

How can i arrive into a main menu and submenu in one query if im going to handle.

selDB, err :=config.DB.Query("SELECT menuname,icon,level FROM tblmenu order by mainmenu")
	if err != nil {
		panic(err.Error())
	}
	for selDB.Next() {
		var level int
		var menuname,icon string
		err = selDB.Scan(&menuname,&icon,&level)
		if err != nil {
			panic(err.Error())
		}	
		UserMenu.MenuName = menuname
		UserMenu.Icon = icon
		UserMenu.Level = level
 		records = append(records, UserMenu)				
    }
		data:=Page{
			Title:  "Dashboard",
			UserEmail: UserEmail,
			UserMenu: records,
	
		}
		fmt.Println(data)	
		templating.Display(res, "index",data)

(Sibert) #10

To get a better answer, you have to add structs how your data is saved. Tables and columns.

It might not be a Golang problem. Your question is a bit unclear.


(Hardeen L) #11

Thank you sibert. This is my struct

			type Menu struct{
			MenuName string
			Icon string
			Level int
		}

		type Page struct {
			Title string
			UserEmail string
			UserMenu []Menu

		}

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

				UserMenu :=Menu{}
				records :=[]Menu{}

				selDB, err :=config.DB.Query("SELECT menuname,icon,level FROM tblmenu order by mainmenu")
				if err != nil {
					panic(err.Error())
				}
				for selDB.Next() {
					var level int
					var menuname,icon string
					err = selDB.Scan(&menuname,&icon,&level)
					if err != nil {
						panic(err.Error())
					}	
					UserMenu.MenuName = menuname
					UserMenu.Icon = icon
					UserMenu.Level = level
					records = append(records, UserMenu)				
				}
					data:=Page{
						Title:  "Dashboard",
						UserEmail: UserEmail,
						UserMenu: records,
				
					}
					fmt.Println(data)	
					templating.Display(res, "index",data)
			} else {
				http.Redirect(res, req, "/", 302)
				return
			}
		}


		 {{range .UserMenu}}     
				 {{if eq .Level 10}}
					  <li class="treeview">
						  <a href="#"><i class="fa {{.Icon}}"></i> <span>{{.MenuName}}</span>
							<span class="pull-right-container">
								<i class="fa fa-angle-left pull-right"></i>
							  </span>
						  </a>
						  <ul class="treeview-menu">
							  <li><a href="#">SUBMENU GOES HERE</a></li>
						  </ul>
						</li>
					  
				 {{else}}
					 <li class="active"><a href="#"><i class="fa {{.Icon}}"></i> <span>{{.MenuName}}</span></a></li>              
				{{end}}
			{{end}}

(Sibert) #12

The struct you present, shows only the parts you want to show in result. Not the entire database structs.

So a wild guess this may only give you a clue how to fetch the data from the database:

SELECT "MenuName","Title","Email" etc FROM Page
LEFT JOIN Menu ON "Menu_ID"="Page_Menu_ID"
WHERE "Menu_ID"=1

A complete data struct should show both unique ids and foreign keys together with relevant columns. Then you can get better answers.

As said before. This could be a SQL question. Or a HTML question. With my limited knowledge of Golang, I think this is not a Golang question. Correct me if I am wrong.


(system) #13

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