cseverino 0 Denunciar post Postado Setembro 18, 2015 Bom dia Pessoal, Preciso gerar o resultado de um SQL em 3 colunas e 4 linhas sem repetir os resultado, hoje faço isso com 3 sql distintos, porém tenho o problema que repete os resultados. Se que é possível fazer isso vial WHILE, porém não sei como fazer, é possível vocês me auxiliarem? Segue SQL <table width="180" height="180" border="0" cellspacing="0" cellpadding="5"><?php $sql = "select a.id, a.codigo_imovel, a.forma, b.tipo, a.dormitorios, a.cidade, a.bairro, CONCAT('R$ ', REPLACE(REPLACE(REPLACE(FORMAT(REPLACE(a.valor,',','.'), 2),'.',';'),',','.'),';',',')) from imoveis a, tipo b where b.id = a.tipo and a.situacao ='Aprovado' order by RAND() limit 20"; $res_imoveis = mysql_query($sql, $conexao); if($res_imoveis){ while ($linha_imoveis = mysql_fetch_row($res_imoveis)){ $sql1 = "select * from fotos where id_imovel=$linha_imoveis[0]"; $res_imoveis_fotos = mysql_query($sql1, $conexao); $linha_imoveis_fotos = mysql_fetch_row($res_imoveis_fotos); ?> <tr> <td width="110" align="left" height="180" class="varios_conteudo" style="max-height:180px"> <a href="abre_imoveis.php?&id=<?=$linha_imoveis[0]?>&offset=0" target="_parent"> <img src="fotos/<?=str_replace("p_", "m_", strtolower($linha_imoveis_fotos[2])); ?>" border="0" width="180" style="max-width:180px; max-height:180px"></a> <br><br> <a href="abre_imoveis.php?&id=<?=$linha_imoveis[0]?>&offset=0" target="_parent" class="conteudo_n" > Cod: <?=$linha_imoveis[1]?> <br> <?=$linha_imoveis[2]?> <br> Dormitórios: <?=$linha_imoveis[4]?> <br> <?=$linha_imoveis[6]?>, <?=$linha_imoveis[5]?> <br> <?=$linha_imoveis[7]?> <br> </a> </td> </tr> <? } } ?> </table> Compartilhar este post Link para o post Compartilhar em outros sites
Beraldo 864 Denunciar post Postado Setembro 18, 2015 A ideia geral é inserir tag <tr> quando já houver 3 resultados numa linha. Isso pode ser feito com o operador "%". Por exemplo: $data = range(1,12); for ($i = 0, $total = count($data); $i < $total; $i++) { if ($i % 3 == 0) { echo PHP_EOL; } echo $data[$i] . ' '; } Saída: 1 2 3 4 5 6 7 8 9 10 11 12 Compartilhar este post Link para o post Compartilhar em outros sites