为什么我的总价未显示在网页上?

时间:2018-11-07 15:16:46

标签: html go

除了总价(totaalprijs)以外,所有这些代码都可以工作。我只是似乎无法使其显示在我的网页上。我认为问题出在if语句周围:“如果aantalpizzas!= 0”,但是我不知道问题所在。总价保存在json文件中,但未在网页上显示。

处理程序:

func bestellingHandler(writer http.ResponseWriter, request *http.Request) {
log.Println("Viewing bestellingen")
bestellingTemplate, _ := template.ParseFiles("./templates/Bestellingen.htm")

// laad de pizza's uit de data file

fmt.Println(request.Method)
fmt.Println(request.Form)
fmt.Println(request.PostFormValue("name"))

filepizza := "./data/pizzas.json"
Readpizza, err := ioutil.ReadFile(filepizza)

var Pizzaslice []Pizza

err = json.Unmarshal(Readpizza, &Pizzaslice)
if err != nil {
    panic(err)
}

Pizzamap := make(map[string]PizzaOrder)
var totaalprijs float64
for i := 0; i < (len(Pizzaslice)); i++ {

    index := strconv.Itoa(i)
    aantalpizzas, _ := strconv.Atoi(request.FormValue("aantal_" + Pizzaslice[i].Name))
    prijspizzas, _ := strconv.ParseFloat(request.FormValue("price"+Pizzaslice[i].Name), 64)
    if aantalpizzas != 0 {

        Pizzamap[index] = PizzaOrder{
            Naam:   request.FormValue("name" + Pizzaslice[i].Name),
            Price:  prijspizzas,
            Aantal: aantalpizzas,
        }

        totaalprijs += float64(aantalpizzas) * prijspizzas
    }
}

filebestelling := "./data/deck.json"
Readbestelling, err := ioutil.ReadFile(filebestelling)

var klantmap map[string]Klant

err = json.Unmarshal(Readbestelling, &klantmap)
if err != nil {
    panic(err)
}
index := strconv.Itoa(len(klantmap))
klantmap[index] = Klant{
    Naam:           request.FormValue("naam"),
    Tussenvoegsel:  request.FormValue("tussenv"),
    Achternaam:     request.FormValue("lname"),
    Adres:          request.FormValue("adress"),
    Telefoonnummer: request.FormValue("phone"),
    Postcode:       request.FormValue("zipcode"),
    Email:          request.FormValue("email"),
    Status:         "Je bestelling wordt bereid",
    Totaalprijs:    totaalprijs,
    Order:          Pizzamap,
}

Resultaat, err := json.MarshalIndent(klantmap, "", "\t")
if err != nil {
    panic(err)
}

ioutil.WriteFile(filebestelling, Resultaat, 0644)
fmt.Sprintln(klantmap[index])
bestellingTemplate.Execute(writer, klantmap[index])

}

HTML:

<fieldset>
               <legend>Je bestelling:</legend>
                <ul>

                    <li>
                        <p>Naam: {{ .Naam }}<br>
                        Tussenvoegsel: {{ .Tussenvoegsel }}<br>
                        Achternaam: {{ .Achternaam }}<br>
                        E-mail: {{ .Email }}<br>
                        Telefoonnummer: {{ .Telefoonnummer }}<br>
                        Status: {{ .Status }}<br>
                        </p>
                        <fieldset>
                            <ul>
                        {{ range $key, $value := .Order }}
                        <li>
                         Pizzanaam: {{ $value.Naam }}  <br>
                         Prijs: &euro;{{ $value.Price | printf "%.2f" }}  <br>
                         Aantal: {{ $value.Aantal }} <br>
                        </li>
                        {{ end }} 
                        </ul>
                        </fieldset>
                        </p>
                    </li>
                    <li>Totaal: &euro;{{ .totaalprijs | printf "%.2f" }}</li>


                </ul>
            </fieldset>

json文件:

{
"0": {
    "Naam": "Lucas",
    "Tussenvoegsel": "de",
    "Achternaam": "Groot",
    "Adres": "Margrietlaan 37",
    "Postcode": "6713PL",
    "Telefoonnummer": "0617033981",
    "Email": "keeperluuk@gmail.com",
    "Status": "Je bestelling wordt bereid",
    "Totaalprijs": 0,
    "Order": {
        "0": {
            "Naam": "salami",
            "Price": 0,
            "Aantal": 2
        }
    }
}

pizzas.json文件:

{
        "name":"margherita",
        "price": 8.50,
        "desc":"tomaten saus, mozzarella en oregano",
        "image":"./templates/margherita.png"
    },
    {
        "name":"pepperoni",
        "price": 9.25,
        "desc":"tomatensaus, mozzarella en pepperoni",
        "image":"./templates/pepperoni.png"
    }

0 个答案:

没有答案