Preparati per il compito in classe Modulo 5 Un grande magazzino utilizza un database per gestire le vendite dei prodotti. La tabella PRO- DOTTI contiene le informazioni relative ai prodotti in vendita nel supermercato (codice articolo, descrizione, prezzo, quantità giacente, genere), la tabella VENDITE contiene le informazioni relative alle vendite (codice articolo, data vendita, quantità venduta) mentre la tabella GE- NERE indica il tipo di prodotto (genere, descrizione, note di conservazione). A partire da una pagina contenente i vari tipi di generi presenti nel database l utente deve avere la possibilità di ottenere: 1 con un click sul genere una pagina con l elenco dei prodotti; 2 con un click sul prodotto una pagina con i dati relativi alle vendite del prodotto nell ultimo trimestre. Analisi. La soluzione proposta prevede tre pagina dinamiche: la prima, CC_ChiediGenere.php, presenta una casella di riepilogo (Cmbgenere) dove sono visualizzati i generi dei prodotti presenti nella tabella Genere. Nella seconda pagina, Prodotti.php, che riceve come parametro il nome del genere richiesto, viene visualizzato l elenco dei prodotti del genere richiesto, all interno della casella di riepilogo CmbProd. Infine la terza pagina, Vendite.php visualizza i dati delle vendite del prodotto richiesto. Codifica PHP Codifica pagina PHP: CC_ChiediGenere.php <html><head><title>magazzino </title> <p align= center ><font size= 5 ><strong><u>generi dei prodotti presenti in magazzino </u> </font> <center> $Dat = Date( d M Y ); Print $Dat </strong></p> ; Print <strong> SCEGLI IL GENERE </strong><br> ; Print <form action= prodotti.php? method= post > ; if(!$conn) // Estrae i generi $query = select * from genere order by idg ; print errore nel comando ; exit(); Print <select name= CmbGenere > <br> ; $descrizione = $riga[ desc ]; Print <Option value= $descrizione > $descrizione </OPTION> ; Print </select> ; Print <INPUT TYPE= SUBMIT name= submit VALUE = Seleziona > ;
Codifica pagina PHP: Prodotti.php. $gen = $_POST[ CmbGenere ]; if(!$conn) // Estrae i prodotti del genere richiesto $query = select ida, prodotti.desc from prodotti inner join genere ; $query = $query. on prodotti.codg = genere.idg where genere.desc = $gen ; $query = $query. order by ida ; die ( errore nel comando ); Print <form action= vendite.php? method= post > ; print <font size= 5 ><center><strong>genere : <I><u> $gen </strong> ; print </font><br> <br> ; Print <select name= CmbProd > <br> ; $cod = $riga[ ida ]; $prodo = $riga[ desc ]; Print <Option value= $cod > $prodo </OPTION> ; Print </select> ; Print <INPUT TYPE= SUBMIT name= submit VALUE = Seleziona > ; Codifica pagina PHP: Vendite.php if(! $conn) // estrae i prodotti del genere prescelto $prod = $_POST[ CmbProd ]; $query = Select coda,datav,qtav from vendite where coda = $prod ; die ( errore nel comando ); print <font size= 5 ><center><strong>prodotto : <I> $prod </strong> ; print </font></u><br> <br> ; if (!$riga) print <font size=5><strong><u>attenzione non ci sono vendite per il prodotto richiesto </u></strong></font> ; else
print <table border = 1 > ; print <TR> ; print <td align = center ><strong>data VENDITA</strong></td> ; print <td align = center ><strong>quantita VENDUTA</strong></td> ; print </tr> ; $codice = $riga[ datav ]; $q = $riga[ qtav ]; print <tr> ; print <td><center> $codice </center></td> ; print <td><center>$q </center></td> ; print </tr> ; print </table> ; Analisi. La soluzione proposta prevede tre pagina dinamiche: la prima, VisGenere.aspx, presenta un bottone e una GridView (con il controllo AutoGenerateSelectButtom impostato a True) dove sono visualizzati i generi dei prodotti presenti nella tabella Genere. Nella seconda pagina, VisProd.aspx, che riceve come parametro il nome del genere richiesto, viene visualizzato l elenco dei prodotti del genere richiesto, all interno della GridView (GrdProdotti). Infine la terza pagina, VisVendite.aspx visualizza i dati delle vendite del prodotto richiesto. Codifica ASP.NET Codifica pagina: VisGenere.aspx <%@ Page Language= VB AutoEventWireup= false CodeFile= VisGenere.aspx.vb Inherits= _Default %> <!DOCTYPE html PUBLIC -//WC//DTD XHTML 1.0 Transitional//EN http://www.w.org/tr/xhtml1/dtd/xhtml1-transitional.dtd > <html xmlns= http://www.w.org/1999/xhtml > <head runat= server > <form id= form1 runat= server > <asp:button ID= Btnvisualizza runat= server Text= visualizza genere /> <asp:label ID= Label1 runat= server Text= generi ></asp:label> <asp:gridview ID= Grdgenere runat= server AutoGenerateSelectButton= True > </div> Codifica pagina: VisGenere.aspx.vb Partial Class _Default
Protected Sub Btnvisualizza_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Btnvisualizza.Click Dim percorsodb As String = C:\Documenti-Dati\LIBRI\commerciale2008\Sito-Guida\ 1-Soluzione Compiti In classe proposti\volume\databasem5.accdb stringaconndb = provider= Microsoft.ACE.OLEDB.12.0;Data Source= & percorsodb & ; comandosql = select * from genere; Grdgenere.DataSource = tabelladb Grdgenere.DataBind() Protected Sub Grdrisultati_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Grdgenere.SelectedIndexChanged Response.Redirect( visprod.aspx?nomevar= & Grdgenere.SelectedRow.Cells(1).Text) End Class Codifica pagina: VisProd.aspx <%@ Page Language= VB AutoEventWireup= false CodeFile= visprod.aspx.vb Inherits= visprod %> <!DOCTYPE html PUBLIC -//WC//DTD XHTML 1.0 Transitional//EN http://www.w.org/tr/xhtml1/dtd/xhtml1-transitional.dtd > <html xmlns= http://www.w.org/1999/xhtml > <head runat= server > <form id= form1 runat= server > <asp:label ID= Label1 runat= server Text= prodotti ></asp:label> <asp:gridview ID= Grdprodotti runat= server AutoGenerateSelectButton= True > </div> Codifica pagina: VisProd.aspx.vb Partial Class visprod Protected Sub Grdrisultati_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Grdprodotti.SelectedIndexChanged Response.Redirect( visvendite.aspx?nomevar= & Grdprodotti.SelectedRow.Cells(1).Text) Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim percorsodb As String = C:\Documenti-Dati\LIBRI\commerciale2008\Sito-Guida\
1-Soluzione Compiti In classe proposti\volume\databasem5.accdb stringaconndb = provider= Microsoft.ACE.OLEDB.12.0;Data Source= & percorsodb & ; comandosql = select * from prodotti where genere = & Request.QueryString( nomevar ) & ; Grdprodotti.DataSource = tabelladb Grdprodotti.DataBind() End Class Codifica pagina: VisVendite.aspx <!DOCTYPE html PUBLIC -//WC//DTD XHTML 1.0 Transitional//EN http://www.w.org/tr/xhtml1/dtd/xhtml1-transitional.dtd > <html xmlns= http://www.w.org/1999/xhtml > <head runat= server > <form id= form1 runat= server > <asp:label ID= Label1 runat= server Text= vendite ></asp:label> <asp:gridview ID= Grdvendite runat= server > </div> Codifica pagina: VisVendite.aspx.vb Partial Class visvendite Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim percorsodb As String = C:\Documenti-Dati\LIBRI\commerciale2008\Sito-Guida\1- Soluzione Compiti In classe proposti\volume\databasem5.accdb stringaconndb = provider= Microsoft.ACE.OLEDB.12.0;Data Source= & percorsodb & ; comandosql = select * from vendita where codart = & Request.QueryString( nomevar ) & ; Grdvendite.DataSource = tabelladb Grdvendite.DataBind()