How do I add id values from a total array

Asked

Viewed 179 times

0

I take 2 results from an array, for example: 1 and 9, these numbers are ID’s that I will use for a query in the database and print these results on the screen, I can even print, but for example if it is found in bd 20 results with these ID’s it prints separately and does not sum the total like this in the image below:

inserir a descrição da imagem aqui

I put a post like this but one of the moderators said that was not clear my doubt, and I am for weeks trying to solve this and can not, see the excerpt php

    <?PHP
$negocio = strip_tags( $_POST['negocio'] );
    $tipo   = strip_tags( $_POST['tipo'] );
    $cidade  = $_POST['cidade'];
    $bairro = $_POST['bairro'] ;

     echo $negocio;
     echo $tipo;
    foreach($cidade as $cidades_2){
    	
    	echo $cidades_2;
    	
    	
    }

    foreach($bairro as $bairros_2){
    	

    	


    $id_bairros=$bairros_2;


   


AQUI É ONDE SOMA OS VALORES VINDO DO ARRAY PARA PEGAR SOMENTE OS IDS SELECIONADOS NO SELECT
    /* MONTA CRITERIOS DE BUSCA */              
    $where = "i.ativo ='1'";
    if ( !empty( $negocio) ) {
    $where .=" AND i.id_negocio_tipo='".$negocio."'";
    if ( !empty( $tipo ) ) {
    $where .=" AND i.id_tipo_imovel='".$tipo."'";
    }
    }
    if ( !empty( $cidades_2 ) ) {
    $where .=" AND i.id_cidade='".$cidades_2."'";
    }
    if ( !empty( $id_bairro ) ) {
    $where .=" AND i.bairro='".$id_bairro."'";
    }


AQUI É SELECT PARA FAZER A PAGINAÇÃO
    ////////
    $sql = $MySQLi->query("SELECT i.*, t.tipo_nome, n.tipo, c.cidade FROM imoveis
    i LEFT JOIN negocio_tipo n ON (n.id = i.id_negocio_tipo)
    LEFT JOIN imoveis_tipo t ON (t.id = i.id_tipo_imovel)
    LEFT JOIN cidades c ON (c.id = i.id_cidade)
    WHERE ".$where."") or print(mysqli_error());
    if (!isset( $_GET["pagina"] ) )
    $pagina = 1;
    else
    $pagina = strip_tags( $_GET["pagina"] );
    $max=8;
    $inicio = $pagina - 1;
    $inicio = $max * $inicio;
    $total = mysqli_num_rows($sql);
    /* calcula a quantidade de produtos sendo exibidos no momento */
    $pgs = ceil($total / $max);
    $de = $max * $pagina; 
    if($pagina == $pgs) $de = $total;
    $temp = $inicio + 1;



AQUI TERIA QUE SOMAR O TOTAL DOS IDS VINDOS DO ARRAY 1 E 9 E MOSTRAR NUM SÓ RESULTADO

    if (!empty( $total )==1 ) {
    echo "<br /><br /><div class='codigo_busca'><p><b>Resultado da Busca de Imóveis</b></p><p>Foram encontrados <b>".$total."</b> imóveis.</p>"."<br />";
    echo "<p><span style='font-weight:bold; font-size:14px; color:#FF0;'><b>Página:</b></span> <span style='font-weight:bold; font-size:14px;'>".$pagina." de ".$pgs."</span></p></div><br /><br />";



AQUI É O SELECT PARA FAZER A BUSCA E IMPRIMIR O RESULTADO NA TELA
    //////////
    $sql =$MySQLi->query("SELECT i.id, i.valor, i.foto_exibicao, i.quartos, i.garagem, i.codigo, t.tipo_nome, n.tipo AS negocio, c.cidade, c.uf, b.bairro FROM imoveis i
    LEFT JOIN negocio_tipo n ON (n.id = i.id_negocio_tipo)
    LEFT JOIN imoveis_tipo t ON (t.id = i.id_tipo_imovel)
    LEFT JOIN cidades c ON (c.id = i.id_cidade)
    LEFT JOIN bairros b ON (b.id = i.bairro)
    WHERE ".$where." ORDER BY valor ASC LIMIT ".$inicio.", ".$max."") or print(mysqli_error());
    while( $linha = mysqli_fetch_array( $sql ) ) {
    $foto=$linha['foto_exibicao'];
    $tipos=$linha['tipo_nome'];	
    $tipo = $tipos;
    $tipo  = strtolower( str_replace(" ", "-", strtr(utf8_decode(trim($tipo)), utf8_decode("áàãâéêíóôõúüñçÁÀÃÂÉÊÍÓÔÕÚÜÑÇ"),"aaaaeeiooouuncAAAAEEIOOOUUNC-")) );
    $bairros=$linha['bairro'];	
    $bairro = $bairros;
    $bairro = strtolower( str_replace(" ", "-", strtr(utf8_decode(trim($bairro)), utf8_decode("áàãâéêíóôõúüñçÁÀÃÂÉÊÍÓÔÕÚÜÑÇ"),"aaaaeeiooouuncAAAAEEIOOOUUNC-")) );
    $cidades=$linha['cidade'];	
    $cidade = $cidades;
    $cidade = strtolower( str_replace(" ", "-", strtr(utf8_decode(trim($cidade)), utf8_decode("áàãâéêíóôõúüñçÁÀÃÂÉÊÍÓÔÕÚÜÑÇ"),"aaaaeeiooouuncAAAAEEIOOOUUNC-")) );
    ?>
    

So people the code is all commenting for better understanding, and sorry for the big text, and if anyone can help me would be grateful ..

  • You want to show the amount of results returned from the database ?

1 answer

0

It’s kind of hard to understand. But from what I understand you want to add $temp + $total.

echo ($temp+$total);
  • Thanks for answering, I can get the searched values but instead of showing everything in a single result shows separately, although I am aware that this happens because it is inside the foreach loop but if I take out of the loop it only takes the last id coming from the array. Just look at the image I posted the first search is id 1 and the second search is id 9 but wanted these results in one search , understood, instead of showing so shows in the image 8 searched properties.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.