Salve a tutti,
espongo il problema al quale sto tentando di trovare una soluzione. Nella pagina catalogo.php ho il seguente codice:
Codice PHP:
<div class="entry">
<script type="text/javascript">
function AjaxFunction(cat_id)
{
var httpxml;
try
{
// Firefox, Opera 8.0+, Safari
httpxml=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
httpxml=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
httpxml=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
function stateck()
{
if(httpxml.readyState==4)
{
var myarray=eval(httpxml.responseText);
// Before adding new we must remove previously loaded elements
for(j=document.testform.subcat.options.length-1;j>=0;j--)
{
document.testform.subcat.remove(j);
}
for (i=0;i<myarray.length;i++)
{
var optn = document.createElement("OPTION");
optn.text = myarray[i];
//optn.value = myarray[i];
document.testform.subcat.options.add(optn);
}
}
}
var url="dd.php";
url=url+"?cat_id="+cat_id;
url=url+"&sid="+Math.random();
httpxml.onreadystatechange=stateck;
httpxml.open("GET",url,true);
httpxml.send(null);
}
</script>
<form name="testform" method='POST' action='mainck.php'>
Categoria
<select name=cat onChange="AjaxFunction(this.value);">
<option value=''><-- Seleziona --></option>
<?php
require "config.php";// connection to database
$q=mysql_query("select * from hcat_it ");
while($n=mysql_fetch_array($q)){
echo "<option value=$n[idcat]>$n[categoria]</option>";
}
}
?>
</select>
<select name=subcat>
</select><input type=submit value=CERCA>
</form>
seleziono il valore della categoria e ottengo il valore della sottocategoria associata nel file mainck.php :
Codice PHP:
<?php
include ("../config.inc.php");
include ("../connect.php");
$ndthumb = "anteprima_thumb.jpg";
// esecuzione prima query
$count = mysql_query("SELECT COUNT(idart) FROM $tabarticoli");
$res_count = mysql_fetch_row($count);
// numero totale di records
$tot_records = $res_count[0];
// risultati per pagina(secondo parametro di LIMIT)
$per_page = 5;
// numero totale di pagine
$tot_pages = ceil($tot_records / $per_page);
// pagina corrente
$current_page = (!$_GET['page']) ? 1 : (int)$_GET['page'];
// primo parametro di LIMIT
$primo = ($current_page - 1) * $per_page;
/*if(isset($_POST['posted'])) {
$idcat = $_POST['idcat'];
$categoria = $_POST['categoria'];
$subcat2 = $_POST['subcategoria'];*/
while (list ($key,$val) = each ($_POST)) {
//echo "\$$key = $val";
$categoria = $val[0];
$subcat = $val[1];
//$idsubcat = $val[3];
echo $categoria;
echo $subcat;
//echo $idsubcat;
echo "
";
// esecuzione seconda query con LIMIT
$query_limit = mysql_query("SELECT * FROM harticoli_it WHERE idcat ='$categoria' LIMIT $primo, $per_page");
while($i = mysql_fetch_array($query_limit)) {
$id = $i['idart'];
$dettaglio = "dettaglio.php?id=$id";
//$query = "SELECT * FROM demoarticoli ORDER BY idart";
//$result = mysql_query($query, $db);
//while ($i = mysql_fetch_array($result)) {
// gestione tabulazione contenuto
$descrizione = $i['descrizione'];
$acapo = wordwrap($descrizione,90);
$acapo1 = nl2br($acapo);
$immagine = $i['thumb'];
$predefimmagine = $ndthumb;
if(empty($i['thumb'])) {
$immagine = $ndthumb;
}
;
//if ($immagine == "" ) { echo [img]$ndthumb[/img] }else{ echo [img]thumbs/$immagine[/img] }
$box = "<table width='800' border='0'>
<tr>
<input name='posted' type='hidden' value='$i[idart]' />
<td width='20%' bgcolor='white'><div align='justify'><span class='Stile1'><span class='Stile2'><span class='Stile3'></span></span></span>[img]thumbs/$immagine[/img]</div></td>
<td width='20%' bgcolor='#669999'><div align='justify'><span class='Stile1'><span class='Stile2'><span class='Stile3'></span></span></span><center>[b]$i[articolo][/b]</center></div></td>
<td width='40%' bgcolor='#669999'><div align='justify'><span class='Stile1'><span class='Stile2'><span class='Stile3'></span></span></span>[b]$acapo1</div></td>
<td width='10%' bgcolor='#669999'><div align='justify'><span class='Stile1'><span class='Stile2'><span class='Stile3'></span></span></span><center><font color='black'>[b]<H4>€ $i[prezzo]</H4>[/b]</font></center></div></td>
<td width='10%' bgcolor='#669999'><div align='justify'><span class='Stile1'><span class='Stile2'><span class='Stile3'></span></span></span><center>[url='$dettaglio'][img]iconeTBASE/dettaglio.png[/img][/url]</center></div></td>
</tr>
</table>";
//"<td width='22%' height='110'> </td>
// <td valign='top' class='detailText' nowrap='nowrap'>
[url='javascript:;'][/url]
// Prezzo: </td>";
//<td width='20%' bgcolor='#CCCCCC'><div align='justify'><span class='Stile1'><span class='Stile2'><span class='Stile3'><input //name='list[]' type='checkbox' value='$i[idart]' /></span></span></span></div></td>
echo $box;
}
}
}
//}
// includiamo uno dei files contenenti la paginazione, commentate l'altro ovviamente
include("paginazione.php");
//include("paginazione_2.php");
// in questa cella inseriamo la paginazione
echo "
";
echo " <tr>\n <td height=\"50\" valign=\"bottom\" align=\"center\"><h3>[b]$paginazione[/b]</h3></td>\n";
echo " </tr>\n</table>\n</div>";
?>
il file dd.php invece contiene il seguente codice:
Codice PHP:
<?php
$cat_id=$_GET['cat_id'];
require "config.php";
$q=mysql_query("select * from hsubcat_it where idcat='$cat_id'");
echo mysql_error();
$myarray=array();
$str="";
while($nt=mysql_fetch_array($q)){
$str=$str . "\"$nt[subcategoria]\"".",";
}
$str=substr($str,0,(strLen($str)-1)); // Removing the last char , from the string
echo "new Array($str)";
?>
Dovrei riuscire a passare dal file dd.php non soltanto il valore "subcategoria" che identifica e stampa correttamente la sottocategoria associata, ma anche un secondo valore (un campo nascosto? ) per far sì che selezionando categoria mi restituisca sottocategorie e selezionato sottocategorie mi stampi soltanto gli articoli della sottocategoria selezionata e non di tutte le sottocategorie come sta stampando con questo codice. Ho provato diverse soluzioni ma senza ottenere il risultato desiderato.
I campi in comune tra categorie, sottocategorie e articoli sono: idcat e ID. Le tabelle sono relazionate con InnoDB.
Qualche suggerimento? Grazie mille.