Visualizzazione dei risultati da 1 a 8 su 8

Discussione: Ricerca su + campi DB

  1. #1
    Utente di HTML.it L'avatar di Giubass
    Registrato dal
    Oct 2003
    Messaggi
    169

    Ricerca su + campi DB

    Ciao Ragazzi sto provando a realizzare uno script per la ricerca di alcuni prodotti in un db Access.

    La ricerca avviene tramite un campo di testo, il mio intento sarebbe quello di cercare uno o più termini sia nel campo "Titolo" che "Descrizione" ed elencare tutti i prodotti che abbiano in comune tale termine.
    Ecco il codice che esegue la ricerca:
    codice:
    <%
    ' Estrazione chiave di ricerca
    Testo = Request.Form("Cerca_Desc")
    Testo = Replace(Testo,"'","''")
    Sost_t = "%' AND Titolo LIKE '%"
    Sost_d = "%' AND Descrizione LIKE '%"
    Testo_t = Replace(Testo, " ",  Sost_t)
    Testo_d = Replace(Testo, " ",  Sost_d)
    strSQL = "SELECT * FROM Articoli WHERE (Titolo LIKE '%" & Testo_t & "%') OR (Descrizione LIKE '%" & Testo_d & "%') ORDER BY Id "
    Set objRS = Server.CreateObject("ADODB.Recordset")
    Set objRS = objConn.Execute(strSQL)
      	
    If Not objRs.EOF And ((Testo_t <> "") Or (Testo_d <> "")) Then
    %>
    ..........
    Cosa c'è di sbagliato nella query?
    Se provo a fare una ricerca con un determinato termine mi restituisce solo un prodotto anche se in realtà ne sono presenti altri.
    Spero di essere stato abbastanza chiaro!

  2. #2
    Utente di HTML.it L'avatar di albis
    Registrato dal
    May 2002
    Messaggi
    912
    a me sembresebbe giusta
    prova a farti restituire il valore del campo strSQL e provala nel db
    Forza la magica ROMA

  3. #3
    Utente di HTML.it L'avatar di Giubass
    Registrato dal
    Oct 2003
    Messaggi
    169
    Originariamente inviato da albis
    a me sembresebbe giusta
    prova a farti restituire il valore del campo strSQL e provala nel db
    In questo momento non posso accedere direttamente al DB!
    Proverò la query più tardi.


    Ti allego uno screenshot del DB:


    Come puoi vedere il termine "Ceriotti" è comune a due articoli ma se provo a fare la ricerca mi visualizza solo il prodotto "Phon Ego Ceriotti", l'altro lo ignora.
    Stessa cosa se provo a cercare per Descrizione, se ad esempio scrivo "Linea naturica" mi estrapola un solo prodotto e nello specifico quello con id più piccolo, ovviamente se aggiungo all'ORDER BY il DESC mi estrae quello con il più grande mentre tutti glia altri li ignora.


  4. #4
    Utente di HTML.it L'avatar di albis
    Registrato dal
    May 2002
    Messaggi
    912
    non è che c'è un errore nel ciclo
    Forza la magica ROMA

  5. #5
    Utente di HTML.it L'avatar di Giubass
    Registrato dal
    Oct 2003
    Messaggi
    169
    Originariamente inviato da albis
    non è che c'è un errore nel ciclo
    Sicuramente sto facendo un errore da fagiano

    Ti allego lo script completo:
    codice:
    <%
    ' Estrazione chiave di ricerca
    Testo = Request.Form("Cerca_Desc")
    Testo = Replace(Testo,"'","''")
    Sost_t = "%' AND Titolo LIKE '%"
    Sost_d = "%' AND Descrizione LIKE '%"
    Testo_t = Replace(Testo, " ",  Sost_t)
    Testo_d = Replace(Testo, " ",  Sost_d)
    strSQL = "SELECT * FROM Articoli WHERE (Titolo LIKE '%" & Testo_t & "%') OR (Descrizione LIKE '%" & Testo_d & "%') ORDER BY Id "
    Set objRS = Server.CreateObject("ADODB.Recordset")
    Set objRS = objConn.Execute(strSQL)
      	
    If Not objRs.EOF And ((Testo_t <> "") Or (Testo_d <> "")) Then
    %>
    <div align="center">
    <table width="495" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td height="20" colspan="2" background="images/hr.gif"></td>
        </tr>
        <tr>
          <td width="125" rowspan="3"><div align="left">">[img]<%=objRS([/img]" alt="Dettagli prodotto" width="120" height="90" border="0"  class="Foto" />
    
              <table width="60" height="30" border="0" align="center" cellpadding="0" cellspacing="0">
                <tr>
                  <td>')">[img]images/zoom.gif[/img]</td>
                </tr>
            </table>
          </div></td>
          <td width="370" height="22"><div align="left"><span class="Titolo"><%=objRS("Titolo")%></span></div></td>
        </tr>
        
        <tr>
          <td height="22"><div align="left">" class="Dettagli">dettagli...</div></td>
        </tr>
        <tr>
          <td valign="bottom"><div align="right">
            <table width="150" height="22" border="0" cellpadding="0" cellspacing="0" class="Prezzo">
              <tr>
                <td width="52">Prezzo:</td>
                <td width="98">€ <%=formatNumber(objRS("Prezzo_pubblico"),2)%>  I.C. </td>
              </tr>
            </table>
    		<% If (Session("Username")<>"") Or (Session("User")<>"") Then %>
    		<table width="216" height="22" border="0" cellpadding="0" cellspacing="0" class="Prezzo">
              <tr>
                <td width="118">Prezzo scontato:</td>
                <td width="98">€ <%=formatNumber(objRS("Prezzo"),2)%>  I.C. </td>
              </tr>
            </table>
    		<% End If %>
          </div></td>
        </tr>
    </table>
    </div>
    <%Else%>
    <table width="95%" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td><div align="center"><span class="Titolo">Non &egrave; 
          stato trovato nessun articolo! </span></div></td>
      </tr>
    </table>
    <%End if%>

  6. #6
    Utente di HTML.it L'avatar di albis
    Registrato dal
    May 2002
    Messaggi
    912
    Codice PHP:

    <%
    ' Estrazione chiave di ricerca
    Testo = Request.Form("Cerca_Desc")
    Testo = Replace(Testo,"'","''")
    Sost_t = "
    %' AND Titolo LIKE '%"
    Sost_d = "
    %' AND Descrizione LIKE '%"
    Testo_t = Replace(Testo, " ",  Sost_t)
    Testo_d = Replace(Testo, " ",  Sost_d)
    strSQL = "
    SELECT FROM Articoli WHERE (Titolo LIKE '%" & Testo_t & "%') OR (Descrizione LIKE '%" & Testo_d & "%'ORDER BY Id "
    Set objRS = Server.CreateObject("
    ADODB.Recordset")
    Set objRS = objConn.Execute(strSQL)
          
    If Not objRs.EOF And ((Testo_t <> "") Or (Testo_d <> "")) Then
    while objRs.EOF = false
    %>
    <div align="
    center">
    <table width="
    495" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td height="
    20" colspan="2" background="images/hr.gif"> </td>
        </tr>
        <tr>
          <td width="
    125" rowspan="3"><div align="left">[url="catalogo.asp?sezione=Dettagli_Prodotto&articolo=<%=objRs("]">[img]<%=objRS([/img]" alt="Dettagli prodotto" width="120" height="90" border="0"  class="Foto" />[/url]

              <table width="
    60" height="30" border="0" align="center" cellpadding="0" cellspacing="0">
                <tr>
                  <td>[url="
    java-script:ApriFoto('inc_catalog/zoom.asp?codice=<%=objRS("]')">[img]images/zoom.gif[/img][/url]</td>
                </tr>
            </table>
          </div></td>
          <td width="
    370" height="22"><div align="left"><span class="Titolo"><%=objRS("Titolo")%></span></div></td>
        </tr>
        
        <tr>
          <td height="
    22"><div align="left">[url="catalogo.asp?sezione=Dettagli_Prodotto&articolo=<%=objRs("]" class="Dettagli">dettagli...[/url]</div></td>
        </
    tr>
        <
    tr>
          <
    td valign="bottom"><div align="right">
            <
    table width="150" height="22" border="0" cellpadding="0" cellspacing="0" class="Prezzo">
              <
    tr>
                <
    td width="52">Prezzo:</td>
                <
    td width="98">€ <%=formatNumber(objRS("Prezzo_pubblico"),2)%>  I.C. </td>
              </
    tr>
            </
    table>
            <% If (
    Session("Username")<>"") Or (Session("User")<>""Then %>
            <
    table width="216" height="22" border="0" cellpadding="0" cellspacing="0" class="Prezzo">
              <
    tr>
                <
    td width="118">Prezzo scontato:</td>
                <
    td width="98">€ <%=formatNumber(objRS("Prezzo"),2)%>  I.C. </td>
              </
    tr>
            </
    table>
            <% 
    End If %>
          </
    div></td>
        </
    tr>
    </
    table>
    </
    div>
    <%
    objRs.MoveNext
    wend
    %>
    <%Else%>
    <
    table width="95%" border="0" align="center" cellpadding="0" cellspacing="0">
      <
    tr>
        <
    td><div align="center"><span class="Titolo">Non è 
          stato trovato nessun articolo
    ! </span></div></td>
      </
    tr>
    </
    table>
    <%
    End if%> 
    Forza la magica ROMA

  7. #7
    Utente di HTML.it L'avatar di Giubass
    Registrato dal
    Oct 2003
    Messaggi
    169
    Che fagiano che sono stato
    E pensare che prima di modificare la stringa sql ed alcune tabelle html fungeva bene, il copia e incolla a volte crea dei casini. Mi ero fissato che c'era qualcosa che non andasse nella stringa ed invece... bastava controllare bene tutto il resto!

    Grazie Albis




  8. #8
    Utente di HTML.it L'avatar di albis
    Registrato dal
    May 2002
    Messaggi
    912
    di nulla
    Forza la magica ROMA

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.