Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Carcleo

for no list

Recommended Posts

Pessoal.

 

Tenho o codigo abaixo:

<?php
 for($i=0;$i<18;$i++)
 {
        $sql_busca_imovel_string.$i=
              "
		     select 
			   imoveiscadastrovenda.id_imovel, 
			   imoveiscadastrovenda.imovel_tipo_imovel, 
			   imoveiscadastrovenda.bairro_imovel, 
			   (select fotos.nome_fotos from fotos where fotos.id_imovel_fotos = imoveiscadastrovenda.id_imovel limit 1) as foto
			 from 
			   imoveiscadastrovenda
			 where 
			   imoveiscadastrovenda.imovel_tipo_imovel=$imoves[$i] and imoveiscadastrovenda.baixa_imovel='n'   
                group by                 
                  imoveiscadastrovenda.id_imovel
			 limit 0,5
			";
        $sql_busca_imovel.$i=$MySQL->query($sql_busca_imovel_string.$i);
        while(list($id_imovel.$i, $imovel_tipo_imovel.$i, $bairro_imovel.$i, $foto.$i) = mysql_fetch_row($sql_busca_imovel.$i))
        {
		 print $id_imovel.$i."<br>";
	 }
 }
?>  

Preciso implementar ao list, o contador $i em cada registro. Só que da erro.

Como fazer isso?

 

Essa varivel $imoveis[$i], é um array que é implementado no bloco anterior com array_puch()

 

Outro problema é que mandei imprimir a variavel $sql_busca_imovel_string.$i e nela tem uma chamada à array $imoveis em seu indice $i. Só lista o indice 1 e depois pula para o indice 10 até o 18, são 18 ítens

Compartilhar este post


Link para o post
Compartilhar em outros sites

Que tipo de dado recebe imovel_tipo_imovel?

 

Dependendo dá para remover este FOR que está/irá consumindo muito processamento usando a cláusula IN do MySQL

 

Exemplo:

<?php

$tipo_imovel = range( 1, 8 );

$sql = sprintf( 'SELECT * FROM `table` WHERE `imovel_tipo_imovel` IN ( %s )', implode( ', ', $tipo_imovel ) );

var_dump( $sql );

 

Saída:

string 'SELECT * FROM `table` WHERE `imovel_tipo_imovel` IN ( 1, 2, 3, 4, 5, 6, 7, 8 )' (length=78)

 

Em uma única consulta, você terá todos os registros.

Compartilhar este post


Link para o post
Compartilhar em outros sites

é int(3).

 

Mas o que esta pegando é o agrupamento de $i na lista do while.

 

Alterei o codigo para:

<?php
 for($i=0;$i<mysql_num_rows($imoveis_tipo)-1;$i++)
 {
        $sql_busca_imovel_string.$i=
              "
		     select 
			   imoveiscadastrovenda.id_imovel, 
			   imoveiscadastrovenda.imovel_tipo_imovel, 
			   imoveiscadastrovenda.bairro_imovel, 
			   (select fotos.nome_fotos from fotos where fotos.id_imovel_fotos = imoveiscadastrovenda.id_imovel limit 1) as foto
			 from 
			   imoveiscadastrovenda
			 where 
			   imoveiscadastrovenda.imovel_tipo_imovel=$imoveis[$i] and imoveiscadastrovenda.baixa_imovel='n'   
                group by                 
                  imoveiscadastrovenda.id_imovel
			 limit 0,5
			";
	 print $sql_busca_imovel_string.$i."<br>";
        $sql_busca_imovel.$i=$MySQL->query($sql_busca_imovel_string.$i);
        while(list($id_imovel, $imovel_tipo_imovel, $bairro_imovel, $foto) = mysql_fetch_row($sql_busca_imovel.$i))
        {
		 print $id_imovel."<br>";
	 }
 }
?>  

Mas, continuo tendo problemas

Compartilhar este post


Link para o post
Compartilhar em outros sites

O que eu quero é o seguinte:

<?php
	 print_r($imoveis);

 for($i=0;$i<mysql_num_rows($imoveis_tipo)-1;$i++)
 {
....

Esse mysql_num_rows($imoveis_tipo) retorna 18 registros.

Com esse print_r($imoveis); eu tenho 17 ítens

Array
(
   [0] => 1
   [1] => 2
   [2] => 3
   [3] => 4
   [4] => 5
   [5] => 6
   [6] => 7
   [7] => 8
   [8] => 9
   [9] => 10
   [10] => 11
   [11] => 12
   [12] => 13
   [13] => 14
   [14] => 15
   [15] => 16
   [16] => 17
   [17] => 18
)

Mas, quando eu jogo $imoveis[$i], implementando o $i contador denro do for, eu só recebo isso:

select imoveiscadastrovenda.id_imovel, imoveiscadastrovenda.imovel_tipo_imovel, imoveiscadastrovenda.bairro_imovel, (select fotos.nome_fotos from fotos where fotos.id_imovel_fotos = imoveiscadastrovenda.id_imovel limit 1) as foto from imoveiscadastrovenda where imoveiscadastrovenda.imovel_tipo_imovel=1 and imoveiscadastrovenda.baixa_imovel='n' group by imoveiscadastrovenda.id_imovel limit 0,5 
- IIIIIIIIIIIII
select imoveiscadastrovenda.id_imovel, imoveiscadastrovenda.imovel_tipo_imovel, imoveiscadastrovenda.bairro_imovel, (select fotos.nome_fotos from fotos where fotos.id_imovel_fotos = imoveiscadastrovenda.id_imovel limit 1) as foto from imoveiscadastrovenda where imoveiscadastrovenda.imovel_tipo_imovel=12 and imoveiscadastrovenda.baixa_imovel='n' group by imoveiscadastrovenda.id_imovel limit 0,5 
- IIIIIIIIIIIII
select imoveiscadastrovenda.id_imovel, imoveiscadastrovenda.imovel_tipo_imovel, imoveiscadastrovenda.bairro_imovel, (select fotos.nome_fotos from fotos where fotos.id_imovel_fotos = imoveiscadastrovenda.id_imovel limit 1) as foto from imoveiscadastrovenda where imoveiscadastrovenda.imovel_tipo_imovel=13 and imoveiscadastrovenda.baixa_imovel='n' group by imoveiscadastrovenda.id_imovel limit 0,5 
- IIIIIIIIIIIII
select imoveiscadastrovenda.id_imovel, imoveiscadastrovenda.imovel_tipo_imovel, imoveiscadastrovenda.bairro_imovel, (select fotos.nome_fotos from fotos where fotos.id_imovel_fotos = imoveiscadastrovenda.id_imovel limit 1) as foto from imoveiscadastrovenda where imoveiscadastrovenda.imovel_tipo_imovel=14 and imoveiscadastrovenda.baixa_imovel='n' group by imoveiscadastrovenda.id_imovel limit 0,5 
- IIIIIIIIIIIII
select imoveiscadastrovenda.id_imovel, imoveiscadastrovenda.imovel_tipo_imovel, imoveiscadastrovenda.bairro_imovel, (select fotos.nome_fotos from fotos where fotos.id_imovel_fotos = imoveiscadastrovenda.id_imovel limit 1) as foto from imoveiscadastrovenda where imoveiscadastrovenda.imovel_tipo_imovel=15 and imoveiscadastrovenda.baixa_imovel='n' group by imoveiscadastrovenda.id_imovel limit 0,5 
- IIIIIIIIIIIII
select imoveiscadastrovenda.id_imovel, imoveiscadastrovenda.imovel_tipo_imovel, imoveiscadastrovenda.bairro_imovel, (select fotos.nome_fotos from fotos where fotos.id_imovel_fotos = imoveiscadastrovenda.id_imovel limit 1) as foto from imoveiscadastrovenda where imoveiscadastrovenda.imovel_tipo_imovel=16 and imoveiscadastrovenda.baixa_imovel='n' group by imoveiscadastrovenda.id_imovel limit 0,5 
- IIIIIIIIIIIII
select imoveiscadastrovenda.id_imovel, imoveiscadastrovenda.imovel_tipo_imovel, imoveiscadastrovenda.bairro_imovel, (select fotos.nome_fotos from fotos where fotos.id_imovel_fotos = imoveiscadastrovenda.id_imovel limit 1) as foto from imoveiscadastrovenda where imoveiscadastrovenda.imovel_tipo_imovel=17 and imoveiscadastrovenda.baixa_imovel='n' group by imoveiscadastrovenda.id_imovel limit 0,5 
- IIIIIIIIIIIII

Ou seja, não recebo dntro do for todos os indicesa do array.

 

Omnde esta o erro?

 

Bom, o que pode então ser melhorado aqui para resolver?

Alterei de for para foreach e ficou assim:

<?php
    foreach ($imoveis as &$value) 
 {
        $sql_busca_imovel_string=
              "
		     select 
			   imoveiscadastrovenda.id_imovel, 
			   imoveiscadastrovenda.imovel_tipo_imovel, 
			   imoveiscadastrovenda.bairro_imovel, 
			   (select fotos.nome_fotos from fotos where fotos.id_imovel_fotos = imoveiscadastrovenda.id_imovel limit 1) as foto
			 from 
			   imoveiscadastrovenda
			 where 
			   imoveiscadastrovenda.imovel_tipo_imovel=$value and imoveiscadastrovenda.baixa_imovel='n'   
                group by                 
                  imoveiscadastrovenda.id_imovel
			 limit 0,5
			";
        $sql_busca_imovel=$MySQL->query($sql_busca_imovel_string);
        while(list($id_imovel, $imovel_tipo_imovel, $bairro_imovel, $foto) = mysql_fetch_row($sql_busca_imovel))
        {
		 print $id_imovel."-----<br>";
	 }
 }
?>  

Porem, o print $id_imovel do while list só imprime uma vez em vez de 18 que é o numero de indices do array.

 

Como acertar?

 

Tenho um foreach que varre os indices de um array.

Nesse foreach, gostaria de colocar uma consulta que iria se repetir para cada value do for each. Para cada value do foreach, terei uma pesquisa e como resultado dessa pesquisa, um while list.

 

Com,o faz isso?

 

Tentei assim(mas só lista os dados do ultimo registro):

<?php
 foreach ($imoveis as &$value) 
 {
	 $sql_busca_imovel_string.$value=
		   "
			 select 
			   imoveiscadastrovenda.id_imovel, 
			   imoveiscadastrovenda.bairro_imovel, 
			   (select fotos.nome_fotos from fotos where fotos.id_imovel_fotos = imoveiscadastrovenda.id_imovel limit 1) as foto
			 from 
			   imoveiscadastrovenda
			 where 
			   imoveiscadastrovenda.imovel_tipo_imovel=$value and imoveiscadastrovenda.baixa_imovel='n'   
			 group by				 
			   imoveiscadastrovenda.id_imovel
			 limit 0,5
			";
	 $sql_busca_imovel.$value=$MySQL->query($sql_busca_imovel_string.$value);

	 while(list($id_imovel, $bairro_imovel, $foto) = mysql_fetch_row($sql_busca_imove.$value))
	 {
		 print $bairro_imovel."<br>";
		 print $foto."<br>";
	 }
 }
?>

 

Pessoal, ainda não consegui.

 

É assim que faz isso:

${$sql_busca_imovel_string.$value}

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.