pensavo di aver risolto ma evidentemente non è così. Posto un po di codice:
Ho queste 2 tabelle:
codice:
CREATE TABLE IF NOT EXISTS `progetto_dettaglio` (
`id_dettagli` int(11) NOT NULL AUTO_INCREMENT,
`id_progetto` int(11) NOT NULL,
`id_cliente` int(11) NOT NULL,
`operatore` varchar(50) NOT NULL,
`azienda_proposta` varchar(50) NOT NULL,
`importo` int(11) NOT NULL,
`tempo_impiegato` int(11) NOT NULL,
PRIMARY KEY (`id_dettagli`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=48 ;
CREATE TABLE IF NOT EXISTS `progetti` (
`id_progetto` int(11) NOT NULL AUTO_INCREMENT,
`id_cliente` int(11) NOT NULL,
`data_ric` date NOT NULL,
`riferimento` varchar(150) NOT NULL,
`data_invio` date NOT NULL,
`data_scadenza` date NOT NULL,
`stato` varchar(150) NOT NULL DEFAULT 'Non Eseguito',
PRIMARY KEY (`id_progetto`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=14 ;
un modulo mi invia i dati per filtrare i risultati:
codice:
<table border="0" cellspacing="0" class="esterno">
<form id="frm_criteri_ricerca_pro" name="frm_criteri_ricerca_pro" method="post" action="ric_progetti2.php">
<tr>
<th width="252">Nome Azienda:</th>
<td width="250"><label for="id_cliente2"></label>
<label for="Nome_Azienda"></label>
<select name="Nome_Azienda" id="Nome_Azienda">
<option> ------------ Scegli ------------ </option>
<?php
do {
?>
<option value="<?php echo $row_rs_clienti['Nome_Azienda']?>"><?php echo $row_rs_clienti['Nome_Azienda']?></option>
<?php
} while ($row_rs_clienti = mysql_fetch_assoc($rs_clienti));
$rows = mysql_num_rows($rs_clienti);
if($rows > 0) {
mysql_data_seek($rs_clienti, 0);
$row_rs_clienti = mysql_fetch_assoc($rs_clienti);
}
?>
</select></td>
<td width="48"> </td>
</tr>
<tr>
<th width="252">Nazione:</th>
<td width="250"><label for="nazione2">
<select name="nazione" id="nazione2">
<option> ------------ Scegli ------------ </option>
<option value="%"></option>
<?php
do {
?>
<option value="<?php echo $row_rs_nazione['Nazione']?>"><?php echo $row_rs_nazione['Nazione']?></option>
<?php
} while ($row_rs_nazione = mysql_fetch_assoc($rs_nazione));
$rows = mysql_num_rows($rs_nazione);
if($rows > 0) {
mysql_data_seek($rs_nazione, 0);
$row_rs_nazione = mysql_fetch_assoc($rs_nazione);
}
?>
</select>
</label></td>
<td> </td>
</tr>
<tr>
<th width="252">ID Progetto:</th>
<td width="250"><input type="text" name="id_progetto" id="id_progetto" /></td>
<td> </td>
</tr>
<tr>
<th width="252">Azienda proposta:</th>
<td width="250"><select name="azienda_proposta" id="azienda_proposta">
<option> ------------ Scegli ------------ </option>
<option value="%"></option>
<?php
do {
?>
<option value="<?php echo $row_rs_az_prop['Azienda_Proposta']?>"><?php echo $row_rs_az_prop['Azienda_Proposta']?></option>
<?php
} while ($row_rs_az_prop = mysql_fetch_assoc($rs_az_prop));
$rows = mysql_num_rows($rs_az_prop);
if($rows > 0) {
mysql_data_seek($rs_az_prop, 0);
$row_rs_az_prop = mysql_fetch_assoc($rs_az_prop);
}
?>
</select></td>
<td width="48"> </td>
</tr>
<tr>
<th width="252">Nome operatore:</th>
<td width="250"><select name="nome_operatore" id="nome_operatore">
<option> ------------ Scegli ------------ </option>
<option value="%"></option>
<?php
do {
?>
<option value="<?php echo $row_rs_operatore['operatore']?>"><?php echo $row_rs_operatore['operatore']?></option>
<?php
} while ($row_rs_operatore = mysql_fetch_assoc($rs_operatore));
$rows = mysql_num_rows($rs_operatore);
if($rows > 0) {
mysql_data_seek($rs_operatore, 0);
$row_rs_operatore = mysql_fetch_assoc($rs_operatore);
}
?>
</select></td>
<td width="48"><input type="submit" name="button5" id="button5" value="Cerca" />
<input type="hidden" name="xxx" id="xxx" /></td>
</tr>
</form>
</table>
creo dei recordset per filtrare gli ID_Progetto:
codice:
$colname_RS_AZIENDA = "-1";
if ($id_cliente > 0) {
$colname_RS_AZIENDA = $id_cliente;
}
mysql_select_db($database_my_zotti, $my_zotti);
$query_RS_AZIENDA = sprintf("SELECT id_progetto FROM progetti WHERE id_cliente = %s", GetSQLValueString($colname_RS_AZIENDA, "int"));
$RS_AZIENDA = mysql_query($query_RS_AZIENDA, $my_zotti) or die(mysql_error());
$row_RS_AZIENDA = mysql_fetch_assoc($RS_AZIENDA);
$totalRows_RS_AZIENDA = mysql_num_rows($RS_AZIENDA);
$colname_rs_progetto = "-1";
if (isset($_POST['id_progetto'])) {
$colname_rs_progetto = $_POST['id_progetto'];
}
mysql_select_db($database_my_zotti, $my_zotti);
$query_rs_progetto = sprintf("SELECT id_progetto FROM progetti WHERE id_progetto = %s", GetSQLValueString($colname_rs_progetto, "int"));
$rs_progetto = mysql_query($query_rs_progetto, $my_zotti) or die(mysql_error());
$row_rs_progetto = mysql_fetch_assoc($rs_progetto);
$totalRows_rs_progetto = mysql_num_rows($rs_progetto);
$colname_rs_ap = "-1";
if (isset($_POST['azienda_proposta'])) {
$colname_rs_ap = $_POST['azienda_proposta'];
}
mysql_select_db($database_my_zotti, $my_zotti);
$query_rs_ap = sprintf("SELECT id_progetto FROM progetto_dettaglio WHERE azienda_proposta = %s", GetSQLValueString($colname_rs_ap, "text"));
$rs_ap = mysql_query($query_rs_ap, $my_zotti) or die(mysql_error());
$row_rs_ap = mysql_fetch_assoc($rs_ap);
$totalRows_rs_ap = mysql_num_rows($rs_ap);
$colname_rs_operatore = "-1";
if (isset($_POST['nome_operatore'])) {
$colname_rs_operatore = $_POST['nome_operatore'];
}
mysql_select_db($database_my_zotti, $my_zotti);
$query_rs_operatore = sprintf("SELECT id_progetto FROM progetto_dettaglio WHERE operatore = %s", GetSQLValueString($colname_rs_operatore, "text"));
$rs_operatore = mysql_query($query_rs_operatore, $my_zotti) or die(mysql_error());
$row_rs_operatore = mysql_fetch_assoc($rs_operatore);
$totalRows_rs_operatore = mysql_num_rows($rs_operatore);
$colname_rs_nazione = "-1";
if (isset($_POST['nazione'])) {
$colname_rs_nazione = $_POST['nazione'];
}
mysql_select_db($database_my_zotti, $my_zotti);
$query_rs_nazione = "SELECT progetti.id_progetto FROM progetti LEFT JOIN anagrafica ON progetti.id_cliente = anagrafica.id_cliente WHERE progetti.id_cliente = anagrafica.id_cliente AND anagrafica.Nazione = ('".$colname_rs_nazione."')";
$rs_nazione = mysql_query($query_rs_nazione, $my_zotti) or die(mysql_error());
$row_rs_nazione = mysql_fetch_assoc($rs_nazione);
$totalRows_rs_nazione = mysql_num_rows($rs_nazione);
quindi creo l'array:
codice:
$id = array();
if ($totalRows_RS_AZIENDA > 0) {
do {
$id[] = $row_RS_AZIENDA['id_progetto'];
}
while ($row_RS_AZIENDA = mysql_fetch_assoc($RS_AZIENDA));
}
if ($totalRows_rs_progetto > 0) {
do {
$id[] = $row_rs_progetto['id_progetto'];
}
while ($row_rs_progetto = mysql_fetch_assoc($rs_progetto));
}
if ($totalRows_rs_ap > 0) {
do {
$id[] = $row_rs_ap['id_progetto'];
}
while ($row_rs_ap = mysql_fetch_assoc($rs_ap));
}
if ($totalRows_rs_operatore > 0) {
do {
$id[] = $row_rs_operatore['id_progetto'];
}
while ($row_rs_operatore = mysql_fetch_assoc($rs_operatore));
}
if ($totalRows_rs_nazione > 0) {
do {
$id[] = $row_rs_nazione['id_progetto'];
}
while ($row_rs_nazione = mysql_fetch_assoc($rs_nazione));
}
ed in fine faccio la join fra le 2 tabelle che mi devono restituire i record con id_progetto presenti nell'array:
codice:
$sql = "SELECT progetti.id_progetto, progetti.id_cliente, progetti.data_ric, progetti.riferimento, progetti.data_invio, progetti.data_scadenza, progetti.stato, progetto_dettaglio.operatore, progetto_dettaglio.azienda_proposta, progetto_dettaglio.tempo_impiegato
FROM progetti INNER JOIN progetto_dettaglio ON progetti.id_progetto = progetto_dettaglio.id_progetto where progetti.id_progetto IN (" . implode(",", array_map("intval",$id)). ")";
mysql_select_db($database_my_zotti, $my_zotti);
$query_rs_ricerca_relazione = $sql;
$rs_ricerca_relazione = mysql_query($query_rs_ricerca_relazione, $my_zotti) or die(mysql_error());
$row_rs_ricerca_relazione = mysql_fetch_assoc($rs_ricerca_relazione);
$totalRows_rs_ricerca_relazione = mysql_num_rows($rs_ricerca_relazione);
se vado a visualizzare l'array in questo modo:
codice:
foreach($id as $elem){
echo $elem.'<br />';
}
mi ritrovo con più id_progetto rispetto a quelli estratti dalla query... bho
Spero di essere stato chiaro e dettagliato. Confido nel vostro aiuto...