nmoa Posted January 28, 2016 at 03:25 PM Report Share #592720 Posted January 28, 2016 at 03:25 PM boa tarde vou ver se me consigo explicar da melhor forma 🙂 a ideia é saber se uma variável está vazia ou não e ir acrescentando numa raiz total $total = $destritos.$concelhos.$reslocal.$resnatu.$resneg; ou seja preciso saber se a var $destritos está vazia...se não tiver aparece, e assim sucessivamente. depois de verificar se estão vazias ou não necessito que ele me conte quantas são se for uma a var $total= a uma unica variavel e for mais que uma $total = 'var'+" AND " + "var" a ideia é esta informação ir complementar uma chamada à base de dados, na parte do WHERE $local =mysql_query("SELECT *, i.id_imovel as im, i.id_energetico as ener FROM imovel as i INNER JOIN`imovel_localizacao` as l ON l.`id_imovel`=i.`id_imovel` INNER JOIN`imovel_caracteristicas` as caract ON caract.`id_imovel`=i.`id_imovel` INNER JOIN `destritos` as d ON d.`id_destritos`=l.`id_destritos` INNER JOIN `concelhos` as c ON c.`id_concelhos`=l.`id_concelhos` INNER JOIN `freguesia` as f ON f.`id_freguesia`=l.`id_freguesia` INNER JOIN `imovel_valor` as valor ON valor.`id_imovel`=i.`id_imovel` INNER JOIN `tipo_negocio` as tipo ON tipo.`id_tipo_negocio`=valor.`id_tipo_negocio` WHERE $total AND i.ativo=1 LIMIT $page_position, $item_per_page "); não sei se foi muito confuso :| obrigado pela ajuda Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted January 28, 2016 at 05:13 PM Report Share #592725 Posted January 28, 2016 at 05:13 PM explicaste mal, mas acho que deu para perceber o que queres. diz-me só uma coisa, essa informação (os dados das variáveis) está guardada nos parâmetros da chamada ($_POST ou $_GET) ? se não está, o que determina quais os dados que estão nas variáveis que descreves ? ps : depois de responderes, vais ver que a solução é muito simples IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
nmoa Posted January 28, 2016 at 05:29 PM Author Report Share #592729 Posted January 28, 2016 at 05:29 PM ois sim vem de um $_GET obrigado Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted January 28, 2016 at 05:43 PM Report Share #592731 Posted January 28, 2016 at 05:43 PM $where = array(); foreach ($_GET as %key => $value) $where[$key] = $value; $sql = " ... where " . implode(" and ", $where) . " ... "; IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
nmoa Posted January 29, 2016 at 04:38 PM Author Report Share #592777 Posted January 29, 2016 at 04:38 PM obrigado vou testar 🙂 Link to comment Share on other sites More sharing options...
tiago.f Posted January 29, 2016 at 09:08 PM Report Share #592790 Posted January 29, 2016 at 09:08 PM $where = array(); foreach ($_GET as %key => $value) $where[$key] = $value; $sql = " ... where " . implode(" and ", $where) . " ... "; Deveria ser $key em vez de %key acho eu... Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted January 29, 2016 at 11:32 PM Report Share #592794 Posted January 29, 2016 at 11:32 PM (edited) Deveria ser $key em vez de %key acho eu... escrever à pressão é o que dá, mas acho que o criador do tópico deverá ter corrigido por si só ------------------------------------------------ além disso, o código está completamente errado, estava a confundir com um código semelhante para o problema diferente. o código que tencionava apresentar seria $where = array(); foreach ($_GET as %key => $value) $where[] = $key . " = " . $value; $sql = " ... where " . implode(" and ", $where) . " ... "; Edited January 29, 2016 at 11:45 PM by HappyHippyHippo IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
nmoa Posted February 5, 2016 at 05:02 PM Author Report Share #593205 Posted February 5, 2016 at 05:02 PM a minha ideia final é esta $where = array(); foreach ($_GET as $key => $value) if ($key=="f"){ $key = "l.id_freguesia"; } if ($key=="neg"){ $key = " valor.id_tipo_negocio"; } if ($key =="d"){ $key = " l.id_destritos"; } if ($key=="c"){ $key = " l.id_concelhos"; } if ($key=="natureza"){ $key= "i.id_natureza"; } $total = $destritos.$concelhos.$reslocal.$resnatu.$resneg; $where[] =$key . " = " . $value; $sql = implode(" and ", $where) . ""; mas os key está a ser substituÃdo e eu quero que seja acrescentado se realmente houver a condição Link to comment Share on other sites More sharing options...
tiago.f Posted February 5, 2016 at 09:12 PM Report Share #593209 Posted February 5, 2016 at 09:12 PM (edited) Meu, tens que por esse codigo dentro do foreach 🙂 $where = array(); foreach ($_GET as $key => $value) { if ($key=="f"){ $key = "l.id_freguesia"; } if ($key=="neg"){ $key = " valor.id_tipo_negocio"; } if ($key =="d"){ $key = " l.id_destritos"; } if ($key=="c"){ $key = " l.id_concelhos"; } if ($key=="natureza"){ $key= "i.id_natureza"; } $where[] =$key . " = " . $value; } $total = $destritos.$concelhos.$reslocal.$resnatu.$resneg; $sql = implode(" and ", $where) . ""; Edited February 15, 2016 at 12:45 AM by apocsantos Link to comment Share on other sites More sharing options...
Solution HappyHippyHippo Posted February 6, 2016 at 10:53 AM Solution Report Share #593220 Posted February 6, 2016 at 10:53 AM (edited) o php permite que a condição da estrutura switch ... case seja uma string foreach ($_GET as $key => $value) { switch ($key) { case "f" : $where[] = " l.id_freguesia = '{$value}'"; break; case "neg" : $where[] = " valor.id_tipo_negocio = '{$value}'"; break; case "d" : $where[] = " l.id_destritos = '{$value}'"; break; case "c" : $where[] = " l.id_concelhos = '{$value}'"; break; case "natureza" : $where[] = " i.id_natureza = '{$value}'"; break; } } Edited February 6, 2016 at 10:54 AM by HappyHippyHippo IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
nmoa Posted February 6, 2016 at 12:16 PM Author Report Share #593225 Posted February 6, 2016 at 12:16 PM #HappyHippyHippo deu certo 🙂 obrigado, sempre a aprender Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now