Generate a row after 2 columns in bootstrap

0

I got a way to generate a row after two columns in the bootstrap, but the result was not as accurate.

I have this:

<?php
$sql_3 = mysql_query("SELECT id, razao_social, cep, cidade, uf, rua, numero, bairro, complemento, logo, CodCli FROM tb_empresas") or die(mysql_error());

if (@mysql_num_rows($sql_3) <= '0')
    {
    echo "";
    }
  else
    {
    while ($r_sql_3 = mysql_fetch_array($sql_3))
        {
        $id_empresa = $r_sql_3[0];
        $razao_social = $r_sql_3[1];
        $cep = $r_sql_3[2];
        $cidade = $r_sql_3[3];
        $uf = $r_sql_3[4];
        $rua = $r_sql_3[5];
        $numero = $r_sql_3[6];
        $bairro = $r_sql_3[7];
        $complemento = $r_sql_3[8];
        $logo = $r_sql_3[9];
        $cod_cli = $r_sql_3[10];
        $endereco = $rua . ", " . $numero . ", " . $bairro . ", " . $cidade . " - " . $uf;
        $endereco_exibe = substr($endereco, 0, 14);
        $sql_3_1 = mysql_query("SELECT telefone FROM tb_empresas_tel WHERE CodCli = '$cod_cli' AND id_emp = '$id_empresa' LIMIT 1") or die(mysql_error());
        if (@mysql_num_rows($sql_3_1) <= '0')
            {
            echo "";
            }
          else
            {
            while ($r_sql_3_1 = mysql_fetch_array($sql_3_1))
                {
                $telefone = $r_sql_3_1[0];
                $telefone_exibe = substr($telefone, 0, 9);
                }
            }

?>

    <div class="vc_col-lg-6">
        <article class="eltd-listing-list-item" id="<?php
        echo $id_empresa; ?>">
            <div class="eltd-listing-item-content">
                <a class="eltd-listing-item-image-link" href="#" title="#">
                                                            <img width="800" height="600" src="images/logos_clientes/<?php
        echo $logo; ?>" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="<?php
        echo $razao_social; ?>" title="<?php
        echo $razao_social; ?>" />

                                                            <div class="icone_empresas_wrapper_mapa icone_empresas">
                                                                <a class="icone_empresas" href="#" style="background-color:#ff6936 !important;">
                                                                    <i class="flaticon-food item_opc_1_emp"></i>                
                                                                </a>
            </div>
            </a>

            <div class="eltd-listing-title-holder">
                <a href="#" title="#">
                    <h3 class="eltd-listing-title" style="font-family: 'Radikal-Bold'; text-transform: uppercase; margin: 10px 0 0 0;">
                        <?php
        echo $razao_social; ?>
                    </h3>
                </a>
            </div>

            <div class="categoria_exp">
                <a href="#">Categoria > Saúde > Clínica ondontológica</a>
            </div>

            <div class="empre_tel">
                <a href="#"><span><?php
        echo $telefone_exibe; ?>...</span> Ver telefone</a>
            </div>

            <div class="categoria"></div>
            <span class="eltd-listing-item-address">
                                                            <a href="#"><span><?php
        echo $endereco_exibe; ?> </span> ... Ver endereço</a>
            </span>
    </div>
    </article>
    </div>
    <?php
        }
    }

?>

It works correctly. But I need to generate a row after two columns, because the blocks are misaligned because they do not contain the same information.

Has anyone seen anything, or do you have any ideas for help?

    
asked by anonymous 25.01.2017 / 13:56

1 answer

0

If you need a row every two elements just count the amount of element.

In case, if it is only every 2 elements you can use the comparator using the $X % 2 === 0 operation. The % operator gets the rest of the division, so we can use it to know if it is even or is odd .

When we divide by 2 we will have the rest. If the remainder is 0 it is because it is declivable by 2 and therefore is even . While odd will subtract% with%. As in mathematics the division of 0 by any number is always% cos_de% so the same applies, it will be considered even our system. ; P

To exemplify the operation of 1 , before you get it wrong:

14 % 3 => 2 (12, do 3*4, é o maior alcançado, sobrando 2)
2 % 2 => 0 (2, do 2*1, é o maior alcançado, sobrando 0)
7 % 2 => 1 (6, do 2*3, é o maior alcançado, sobrando 1)
29 % 5 => 4 (25, do 5*5,é o maior alcançado, sobrando 4)

Logica:

If the current element is even it starts 0 ( % ). In the condition that it is odd or the last pair should close the row ( <div class="row"> ).

Then just add:

if($i %2 === 0){    
 echo '<div class="row">';
}

//...

if($i %2 !== 0 || count($array) === $i + 1){    
    echo '</div>';
}

See this by clicking here.

In this way, if pair it will add to row and if not it will close, however if pair / p>

Your code is too long to make so few changes, so it would be imperceptible, so just add:

A </div> at the beginning of the code:

<?php
// Acrescente um $i:
$i = 0;

At the end of the while add:

   <?php
   // Adicione:
   $i++;
}

Then in HTML you can add:

<?= ($i % 2 === 0) ? '<div class="row">' : '' ?>
   <div class="vc_col-lg-6">
   //....
   </div>
<?= ($i % 2 !== 0 || count($array) === $i + 1) ? '</div>' : '' ?>
  

The div in PHP 5.4+ is active by default, it is equal to $i , ternary comparator does the same as <?= shown above with less code.

Notes:

This is not in the question asked, but I think it is important to say.

Do not use <? echo migrate to if , here's why .

Plus you're always using:

$id_empresa = $r_sql_3[0];
$razao_social = $r_sql_3[1];
//...

I do not see the point to this if you can just use: mysql_ and mysqli_ , if you really need all the content in a variable, you could just use #

while ($r_sql_3 = mysql_fetch_row($sql_3)){

  list($id_empresa, $razao_social, $cep, $cidade, $uf, $rua, $numero, $bairro, $complemento, $logo, $cod_cli) = $r_sql_3;

  //...

}
    
26.01.2017 / 09:32