Pagina 1 di 4 1 2 3 ... ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 34
  1. #1
    Utente di HTML.it
    Registrato dal
    Nov 2003
    Messaggi
    156

    Estrarre dati solo da categoria specifica!

    Ciao, sto utilizzando uno script per la gestione di news.
    Ciascuna news consiste in titolo, testo dipresentazione e testo completo e nel database oltre a questi campi vi sono quello ID e quello relativo alla data.
    Vorrei aggiungere un altro campo che funga da categoria. Fin qui tutto ok, inserendo la news specificherò una categoria in questo campo.
    Vorrei sapere però come faccio a specificare che la pagina deve estrarre dal database solo le news relative a una specifica categoria.
    In questo modo posso creare + pagine dove in ciascuna visualizzo solo news relative alla stessa categoria senza moltiplicare tabelle e database!

    La connessione della pagina di default è
    codice:
    ' Open Connection to the database
    set conn = Server.CreateObject("ADODB.Connection")
    conn.Open xDb_Conn_Str
    
    ' Build Query
    strsql = "select * from [accouncements1]"
    
    If dbwhere <> "" Then
    	strsql = strsql & " WHERE " & dbwhere
    End If
    
    if OrderBy <> "" then 
    		strsql = strsql & " ORDER BY [" & OrderBy & "] " & Session("accouncements_OT")
    end if	
    
    'response.write strsql
    
    set rs = Server.CreateObject("ADODB.Recordset")
    rs.Open strsql, conn, 1, 2 
    totalRecs = rs.RecordCount
    %>
    Grazie tante per l'eventuale aiuto!

  2. #2
    Moderatore di ASP e MS Server L'avatar di Roby_72
    Registrato dal
    Aug 2001
    Messaggi
    19,559
    Una cosa del genere:

    codice:
    cat = "pincopallino"
    ' Build Query
    strsql = "select * from [accouncements1]"
    
    If dbwhere <> "" Then
    	strsql = strsql & " WHERE cat = '"& cat &"' AND " & dbwhere
    End If
    Non capisco bene però se quella session nel codice più in basso la usi o meno.

    Roby

  3. #3
    Utente di HTML.it
    Registrato dal
    Nov 2003
    Messaggi
    156
    Infatti se metto gli apici in quella sessione succede che la pagina visualizza per primi i records relativi alla categoria da me scelta poi tutti gli altri. Se tolgo gli apici anche specificando categoria non succede niente.

    Il codice della pagina è
    codice:
    <% 'no security checking %>
    <%
    Response.expires = 0
    Response.expiresabsolute = Now() - 1
    Response.addHeader "pragma", "no-cache"
    Response.addHeader "cache-control", "private"
    Response.CacheControl = "no-cache"
    %>
    
    
    <% 
    displayRecs = 5
    recRange = 5
    %>
    
    <%
    ' Get table name
    tablename = "[accouncements]"
    dbwhere = ""
    a_search = ""
    b_search = ""
    %>
    
    
    
    <%
    ' Get search criteria for basic search
    pSearch = Request.QueryString("psearch")
    If pSearch <> "" Then
    	pSearch = replace(pSearch,"'","''")
    	pSearch = replace(pSearch,"[","[[]")
    	b_search = b_search & "[Title] LIKE '%" & pSearch & "%' OR "
    	b_search = b_search & "[SmallNotes] LIKE '%" & pSearch & "%' OR "
    	b_search = b_search & "[DetailedNotes] LIKE '%" & pSearch & "%' OR "
    End If
    If len(b_search) > 4 Then
    	b_search = mid(b_search,1,len(b_search)-4)
    	b_search = "(" & b_search & ")"
    End If
    %>
    
    
    
    <%
    'Build search criteria
    If a_search <> "" Then
    	dbwhere = dbwhere &  a_search 'advance search
    ElseIf b_search <> "" Then
    	dbwhere = dbwhere & b_search 'basic search
    End If
    
    'Save search criteria
    If dbwhere <> "" Then
    	Session("tablename") = tablename
    	Session("dbwhere") = dbwhere
    	'reset start record counter
    	startRec = 1
    	Session("accouncements_REC") = startRec
    Else
    	If tablename = Session("tablename") Then
    		dbwhere = Session("dbwhere")
    	Else
    		'reset search criteria
    		dbwhere = ""
    		Session("dbwhere") = dbwhere
    	End If
    End If
    
    'Get clear search cmd
    If Request.QueryString("cmd").Count > 0 then
    	cmd=Request.QueryString("cmd")
    	If ucase(cmd) = "RESET" Then
    		'reset search criteria
    		dbwhere = ""
    		Session("dbwhere") = dbwhere
    	End If
    End If
    
    %>
    
    
    <%
    ' Load Default Order
    DefaultOrder = "date"
    DefaultOrderType = "DESC"
    
    ' Check for an Order parameter
    OrderBy = ""
    If Request.QueryString("order").Count > 0 Then
    	OrderBy = Request.QueryString("order")
    	' Check if an ASC/DESC toggle is required
    	If Session("accouncements_OB") = OrderBy Then
    		If Session("accouncements_OT") = "ASC" Then
    			Session("accouncements_OT") = "DESC"
    		Else
    			Session("accouncements_OT") = "ASC"
    		End if
    	Else
    		Session("accouncements_OT") = "ASC"
    	End If
    	Session("accouncements_OB") = OrderBy
    	Session("accouncements_REC") = 1
    Else
    	OrderBy = Session("accouncements_OB")
    	if OrderBy = "" then
    		OrderBy = DefaultOrder
    		Session("accouncements_OB") = OrderBy
    		Session("accouncements_OT") = DefaultOrderType
    	End If
    End If
    
    ' Check for a START parameter
    If Request.QueryString("start").Count > 0 Then
    	startRec = Request.QueryString("start")
    	Session("accouncements_REC") = startRec
    Else
    	startRec = Session("accouncements_REC")
    	if not isnumeric(startRec) or startRec = "" then
    		'reset start record counter
    		startRec = 1
    		Session("accouncements_REC") = startRec
    	End If
    End If
    
    ' Open Connection to the database
    set conn = Server.CreateObject("ADODB.Connection")
    conn.Open xDb_Conn_Str
    
    cat = "arte"
    ' Build Query
    strsql = "select * from [accouncements1]"
    
    
    If dbwhere <> "" Then
    	strsql = strsql & " WHERE cat" '"& cat &"' AND " & dbwhere
    End If
    
    if OrderBy <> "" then 
    		strsql = strsql & " ORDER BY [" & OrderBy & "] " & Session("accouncements_OT")
    end if	
    
    'response.write strsql
    
    set rs = Server.CreateObject("ADODB.Recordset")
    rs.Open strsql, conn, 1, 2 
    totalRecs = rs.RecordCount
    %>
    <p align="center">
    
    Our Recent News 
    
    
    <p align="center" style="margin-top: 0; margin-bottom: 0">
    
    <div align="center">
      <center>
    <table border="0" cellspacing="0" cellpadding="4" style="border-collapse: collapse" bordercolor="#111111">
    	<tr>
    		<td>		
    		<form action="default.asp">
    			<input name="psearch" size=20>
    			<input type="submit" name="Submit" value="Search">
    			
    		</td>
    		<td><font size="-1">Show All
            Amministrazione</font></td>
    	</tr>
    </table>
      </center>
    </form>
    <center>
    <%
    if totalRecs > 0 then
    
    	' Find out if there should be Backward or Forward Buttons on the table.
    	If 	startRec = 1 Then
    		isPrev = False
    	Else
    		isPrev = True
    		PrevStart = startRec - displayRecs
    		If PrevStart < 1 Then PrevStart = 1 %>
    	<a href="default.asp?start=<%=PrevStart%>"><font size="-1">
    [&lt;&lt;INDIETRO]</font></a>
    	<%
    	End If
    	
    	' Display Page numbers
    	If (isPrev OR (NOT rs.EOF)) Then
    		If (NOT isPrev) Then Response.Write ""
    		x = 1
    		y = 1
    	
    		dx1 = ((startRec-1)\(displayRecs*recRange))*displayRecs*recRange+1
    		dy1 = ((startRec-1)\(displayRecs*recRange))*recRange+1
    		If (dx1+displayRecs*recRange-1) > totalRecs then
    			dx2 = (totalRecs\displayRecs)*displayRecs+1
    			dy2 = (totalRecs\displayRecs)+1
    		Else
    			dx2 = dx1+displayRecs*recRange-1
    			dy2 = dy1+recRange-1
    		End If
    	
    		While x <= totalrecs
    			If x >= dx1 and x <= dx2 Then
    				If Clng(startRec) = Clng(x) Then %>
    	<font size="-1"><%=y%></font>
    	<% 			Else %>
    	<font size="-1"><%=y%></font>
    	<%			End If
    				x = x + displayRecs
    				y = y + 1
    			elseif x >= (dx1-displayRecs*recRange) and x <= (dx2+displayRecs*recRange) then
    				if x+recRange*displayRecs < totalRecs then %>
    	<font size="-1"><%=y%>-<%=y+recRange-1%></font>
    	<%			else
    					ny=(totalRecs-1)\displayRecs+1
    						if ny = y then %>
    	<font size="-1"><%=y%></font>
    	<%					else %>
    	<font size="-1"><%=y%>-<%=ny%></font>
    	<%					end if
    				end if
    				x=x+recRange*displayRecs
    				y=y+recRange
    			else
    				x=x+recRange*displayRecs
    				y=y+recRange
    			End If
    		Wend
    	End If
    	
    	' Next link
    	If NOT rs.EOF Then
    		NextStart = startRec + displayRecs
    		isMore = True %>
    	<a href="default.asp?start=<%=NextStart%>"><font size="-1">
    [AVANTI&gt;&gt;]</font></a>
    	<% Else
    		isMore = False
    	End If %>
    	
    
    	<font size="2">News</font><% If stopRec > recCount Then stopRec = recCount %>
    	<font size="-1"><%= startRec %> to <%= stopRec %> of <%= totalRecs %></font>
    
    <% Else %>
    
    
    
    <font size="-1">Nessuna pagina presente!</font>
    
    
    
    <% End If %>
    <form>
    <table border="0" cellspacing="3" cellpadding="5" width="95%">
    <%
    'Avoid starting record > total records
    if clng(startRec) > clng(totalRecs) then
    	startRec = totalRecs
    end if
    'Set the last record to display
    stopRec = startRec + displayRecs - 1
    
    'Move to first record directly for performance reason
    recCount = startRec - 1
    if not rs.eof then
    	rs.movefirst
    	rs.move startRec - 1
    end if
    
    recActual = 0
    Do While (NOT rs.EOF) AND (recCount < stopRec)
    	recCount = recCount + 1
    	If Clng(recCount) >= Clng(startRec) Then 
    		recActual = recActual + 1 %>
    
    <%
    	'set row color
    	bgcolor="#Ffffff"
    %>
    
    <%	
    	' Display alternate color for rows
    	If recCount mod 2 <> 0 Then
    		bgcolor="#ffffff"
    	End If
    %>
    
    <%
    	x_Title = rs("Title")
    	x_SmallNotes = rs("SmallNotes")
    	x_date = rs("date")
    %>
    
    
    <tr>
    <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="242" id="AutoNumber1" height="39">
      <tr>
    <td width="100%" style="border-left-width: 1; border-right-width: 1; border-top-style: solid; border-top-width: 1; border-bottom-width: 1"><font size="-1">"><% response.write x_Title %></font></td>
        </tr>
      <tr>
        <td width="30%"><font size="-1">
    <%= replace(x_SmallNotes & "",chr(10),"
    ") %>
    </font></td>
      </tr>
    </table>
    </tr>
    <tr>
    </tr>
    
    
    <%
    	end if
    
    	rs.MoveNext
    Loop 
    %>
    
    
    </table>
    </form>
    
    </div>
    
    
    <%
    ' Close recordset and connection
    rs.Close
    Set rs = Nothing
    conn.Close
    Set conn = Nothing %>
    ciao e grazie!

  4. #4
    Utente di HTML.it
    Registrato dal
    Nov 2003
    Messaggi
    156
    Poi, ho creato una campo nella tabella, chiamata appunto "categoria" dove specifico una parola es "arte" ecc ecc. nel momento in cui inserisco la news. E' corretto?

  5. #5
    Moderatore di ASP e MS Server L'avatar di Roby_72
    Registrato dal
    Aug 2001
    Messaggi
    19,559
    Sarebbe più corretto creare una tabella delle categorie e collegarla alla cartella della news tramite l'id della categoria invece che scrivere trecento volte nella tabella la categoria "arte" per esempio.

    Roby

  6. #6
    Utente di HTML.it
    Registrato dal
    Nov 2003
    Messaggi
    156
    Non ho capito! Però, non sarebbe un problema specificare un testo categoria da inserire nel campo categoria, pazienza se riempie un pò il dtatabase.
    La tabella è una, contiene il campo id, data, titolo, abbreviazione, testo completo, categoria.
    La soluzione che mi hai dato sembra non avere effetto e la session di cui parli credo sia relativa alla funzione di ricerca che non funziona + se metto gli apici.
    L'esempio è qui http://www.design-shop.it/arte/default.asp
    In pratica la pagina deve visualizzare solo i records che contengono una determinata parola nel campo categoria.

    Ciao!

  7. #7
    Esempio:
    codice:
    Tabella_Categorie
    id_Categoria (contatore)   Titolo_Categoria
         1                          Sport
         2                          Gossip
         3                          Cronaca
    
    
    Tabella_Notizie
    id_Notizia    Titolo_Notizia              ....   id_Categoria (numerico)
        1         Qualificazioni Mondiali                   1
        2         Rapina in villa                           3
        3         Incidente sulla A1                        3
        4         Le foto della Arcuri                      2
        5         Davis: Italia - Spagna 1 - 1              1
    Vediamo se mi sai dire delle 5 notizie a quale categoria ognuno appartiene...

  8. #8

  9. #9
    Utente di HTML.it
    Registrato dal
    Nov 2003
    Messaggi
    156
    Aò non m'ammazzate
    Allora creo una tabella nel database e la chiamo "categoria" inserisco il campo id_categoria che è il contatore e quello tiolo, va bene semplice text?
    E poi, non so + come andare avanti. Come collego la news alla categoria? E come specifico nella home che deve visualizzare solo le news con quella categoria?
    Se prima era difficile mò è impossibile per me da risolvere.
    Spero in un vostro aiuto sennò devo creare una nuova tabella per ogni categoria con tutti i campi ecc ecc!

  10. #10
    La news la leghi alla categoria in base al valore id_categoria che è presente in entrambe le tabelle.
    I campi testuali li fai di tipo testo o memo a seconda della mole di dati che devono contenere (testo in Access ha il limite di 255 caratteri, mentre memo arriva a 65535).
    Date queste tabelle:
    codice:
    tabella_categorie
    id_categoria	titolo_categoria
    1	        Cronaca
    2	        Sport
    3	        Gossip
    
    tabella_notizie
    id_notizia	titolo_notizia	        sommario_notizia	                        testo_notizia	                     data_notizia	   id_categoria
    1	        Incidente sulla A1	Grave incidente sulla A1	                Testo sull'incidente sulla A1	     25/09/2005	           1
    2	        Rapina in villa	        Rubati 300.000 euro in una villa del nord-est	Testo sulla rapina in villa	     25/09/2005	           1
    3	        Calendario della Arcuri	In anteprima il calendario 2018 della Arcuri	Testo sul calendario della Arcuri    25/09/2005	           3
    4	        Coppa Davis	        In corso il terzo incontro Italia Spagna 1 a 1.	Testo sulla Coppa Davis	             25/09/2005	           2
    5	        5^ giornata di camp.	Oggi in gara Inter e Milan	                Testo sulla giornata di campionato   25/09/2005	           2
    Abbiamo sta pagina:
    codice:
    <%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
    <%
    thisPage = request.serverVariables("PATH_INFO")
    
    id_categoria = request.queryString("id_categoria")
    	if isNumeric(id_categoria) then 
    	id_categoria = cLng(id_categoria)
    	else
    	id_categoria = 0
    	end if
    	
    id_notizia = request.queryString("id_notizia")
    	if isNumeric(id_notizia) then 
    	id_notizia = cLng(id_notizia)
    	else
    	id_notizia = 0
    	end if
    	
    set conn = server.createObject("ADODB.Connection")
    conn.open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & server.mapPath("\samples\database\test.mdb")
    %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Documento senza titolo</title>
    </head>
    
    <body>
    <table cellpadding="2" cellspacing="0" border="1" align="center" width="680">
      <tr>
        <td align="center" colspan="2">
    <%
    sql = "SELECT * FROM tabella_categorie ORDER BY titolo_categoria"
    set rs = conn.execute(sql)
    	if not rs.eof then
    		do until rs.eof
    			if id_categoria = rs("id_categoria") then
    %>
    | <%=rs("titolo_categoria")%>
    <%
    			else
    %>
    | "><%=rs("titolo_categoria")%>
    <%
    			end if
    		rs.moveNext
    		loop
    	end if
    rs.close
    set rs = nothing
    %> |
        </td>
      </tr>
      <tr>
        <td width="250" valign="top">
    	<%
    		if id_categoria > 0 then
    		
    			sql = "SELECT * FROM tabella_notizie WHERE id_categoria = " & id_categoria
    			set rs = conn.execute(sql)
    				if not rs.eof then
    					do until rs.eof
    						if id_notizia = rs("id_notizia") then
    	%>
    	
    
    <%=rs("titolo_notizia")%>
    	<%
    						else
    	%>
    	
    
    &id_notizia=<%=rs("id_notizia")%>"><%=rs("titolo_notizia")%>
    	<%			
    						end if
    	%>
    	
    <%=rs("sommario_notizia")%></p>
    	<%
    					rs.moveNext
    					loop
    				end if
    			rs.close
    			set rs = nothing
    		
    		end if
    	%>
    	</td>
    	<td valign="top" width="430">
    	<%
    		if id_notizia > 0 then
    		
    			sql = "SELECT * FROM tabella_notizie WHERE id_notizia = " & id_notizia & " AND id_categoria = " & id_categoria
    			set rs = conn.execute(sql)
    				if not rs.eof then
    	%>
    	
    
    <%=rs("titolo_notizia")%> - <%=rs("data_notizia")%>
    
    	<%=rs("sommario_notizia")%></p>
    	
    
    <%=rs("testo_notizia")%></p>
    	<%
    				else
    	%>
    	Nessuna notizia disponibile in base ai parametri selezionati.
    	<%			
    				end if
    		
    		end if
    	%>
    	</td>
      </tr>
    </table>
    Fai una prova.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.