ciao a tutti sono riuscito a creare tramite la tecnica ajax e php un motore di ricerca per i titoli (videogiochi) che recensiamo su un sito che ho con un amico.
Lo script funziona bene ma c'è un problema che non riesco a capire... l'ho impostato per cominciare la ricerca appena si immette la seconda lettera... es se metto si dovrebbe trovarmi tutti i giochi che iniziano con si invece mi trova tutti i giochi con la s ma anche che iniziano con se,sp,sn ecc ecc
volevo chiedermi se si può ricercare solo quelli che iniziano per quelle lettere... vi posto il codice sia javscript che php... penso che il problema sia come richiamo la query...
codice:
<script type="text/javascript">
var xmlhttp;
function mostraInfo(str)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
if (str.length > 1) {
var url="cerca.php";
url=url+"?key="+str;
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("info").innerHTML=xmlhttp.responseText;
}
}
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
}
</script>
<form>
cerca titolo:
<input type="text" name="users" onkeypress="mostraInfo(this.value)" />
</form>
<div id="info"></div>
questo invece è lo script php che interroga il db
Codice PHP:
<?php
include "config.php";
include "connessione.php";
$colore = 1;
$key = $_GET['key'];
if(!isset($key)) die('PARAMETRO key MANCANTE');
$key = mysql_real_escape_string($key);
$sql = "SELECT id, titolo, trama, vototot FROM recensioni WHERE titolo LIKE '".$key. "%'";
$res = mysql_query($sql, $db) or die('QUERY SQL FALLITA');
echo "<table border='1' cellpadding='0' cellspacing='0'>
<tr bgcolor='#FFFF00'>
<th>Titolo</th>
<th>Trama</th>
<th>voto</th>
<th>Contenuto</th>
<th>link</th>
</tr>";
while ($row = @mysql_fetch_array($res)){
// recensione
if ($colore == 1){
echo "<tr bgcolor='#00FF00'>";
echo "<td>" . $row['titolo'] . "</td>";
$stringainiziale = $row['trama'];
$max = '80';
$stringa = substr($stringainiziale, 0, $max); //Tronca la stringa al carattere $max
$stringa = $stringa.'...'; //Aggiunge i puntini
echo "<td>$stringa</td>";
echo "<td>" . $row['vototot'] . "</td>";
echo "<td> Recensione</td>";
$id = $row['id'];
echo "<td>[url='../recensioni.php?recen=$id']Qui[/url]</td>";
echo "</tr>";
$colore = 2;
}else{
echo "<tr bgcolor='#00FFFF'>";
echo "<td>" . $row['titolo'] . "</td>";
$stringainiziale = $row['trama'];
$max = '80';
$stringa = substr($stringainiziale, 0, $max); //Tronca la stringa al carattere $max
$stringa = $stringa.'...'; //Aggiunge i puntini
echo "<td>$stringa</td>";
echo "<td>" . $row['vototot'] . "</td>";
echo "<td> Recensione</td>";
$id = $row['id'];
echo "<td>[url='../recensioni.php?recen=$id']Qui[/url]</td>";
echo "</tr>";
$colore = 1;
}
}
$sqlant = "SELECT id, titolo, descrizione FROM anteprime WHERE titolo LIKE '".$key. "%'";
$resant = mysql_query($sqlant, $db) or die('QUERY SQL FALLITA');
while ($rowa = @mysql_fetch_array($resant)){
// anteprima
if ($colore == 1){
echo "<tr bgcolor='#00FF00'>";
echo "<td>" . $rowa['titolo'] . "</td>";
$stringainizialea = $rowa['descrizione'];
$maxa = '80';
$stringaa = substr($stringainizialea, 0, $maxa); //Tronca la stringa al carattere $max
$stringaa = $stringaa.'...'; //Aggiunge i puntini
echo "<td>$stringaa</td>";
echo "<td></td>";
echo "<td> Anteprima</td>";
$ida = $rowa['id'];
echo "<td>[url='../anteprime.php?antep=$ida']Qui[/url]</td>";
echo "</tr>";
$colore = 2;
}else{
echo "<tr bgcolor='#00FFFF'>";
echo "<td>" . $rowa['titolo'] . "</td>";
$stringainizialea = $rowa['descrizione'];
$maxa = '80';
$stringaa = substr($stringainizialea, 0, $maxa); //Tronca la stringa al carattere $max
$stringaa = $stringaa.'...'; //Aggiunge i puntini
echo "<td>$stringaa</td>";
echo "<td></td>";
echo "<td> Anteprima</td>";
$ida = $rowa['id'];
echo "<td>[url='../anteprime.php?antep=$ida']Qui[/url]</td>";
echo "</tr>";
$colore = 1;
}
}
echo "</table>";
mysql_close($db);
?>
Qualcuno può aiutarmi nel capire come posso far funzionare lo script come voglio io??
grazie ciao