How to print next line

Asked

Viewed 80 times

0

I have the following code:

<?
$result = $connection -> query("SELECT * FROM testemunhos") or die($connection -> error);
while($row = $result -> fetch_array(MYSQLI_ASSOC)){
?>
<div class="item">
    <div class="quote-left">
        <img src="<?=$row['testemunho_icon']?>" alt=""/>
        <div class="ql-content">
            <p><?=$row['testemunho_texto']?></p>
            <cite>-- <?=$row['testemunho_cliente']?>, <span><?=$row['testemunho_empresa']?></span></cite>
        </div>
    </div>
    <div class="quote-sep"></div>
    <div class="quote-right">
        <img src="<?=$row['testemunho_icon']?>" alt=""/>
        <div class="qr-content">
            <p><?=$row['testemunho_texto']?></p>
            <cite>-- <?=$row['testemunho_cliente']?>, <span><?=$row['testemunho_empresa']?></span></cite>
        </div>
    </div>
</div>
<?
}
?>

I want in the quote-right the next record of the database appears, thus he prints the same record on both sepradores quote-left and quote-right

Some Suggestion?

  • There will always be an even number in this consultation?

  • It may or may not depend on the testimony that will enter.

4 answers

1

You can create a variable counter $i for example if it’s pair you change the class to quote-right and if it doesn’t quote-left.

  • How can I do that?

  • I added a generic example in Lucas' reply. ?

0

0

Marcio, it is necessary to pass the result to a array and then, with the numeric keys, find out which is next $row:

<?
$result = $connection -> query("SELECT * FROM testemunhos") or die($connection -> error);
$rows = [];

// -- passa o resultado para $rows
while($row = $result->fetch_array(MYSQLI_ASSOC))
{
    $rows[] = $row;
}

// -- agora sim, para cada linha, com a chave ($key) numérica e ordenada...
foreach ($rows as $key => $row) 
{
    //$row_atual = $row;
    //$row_anterior     = ($key == 0 ? $rows[count($rows) - 1] :  $rows[$key - 1]);
    $row_proxima    = ($key == count($rows) - 1 ? $rows[0] : $rows[$key + 1]);
?>
    <div class="item">
    <div class="quote-left">
        <img src="<?=$row['testemunho_icon']?>" alt=""/>
        <div class="ql-content">
            <p><?=$row['testemunho_texto']?></p>
            <cite>-- <?=$row['testemunho_cliente']?>, <span><?=$row['testemunho_empresa']?></span></cite>
        </div>
    </div>
    <div class="quote-sep"></div>
    <div class="quote-right">
        <img src="<?=$row_proxima['testemunho_icon']?>" alt=""/>
        <div class="qr-content">
            <p><?=$row_proxima['testemunho_texto']?></p>
            <cite>-- <?=$row_proxima['testemunho_cliente']?>, <span><?=$row_proxima['testemunho_empresa']?></span></cite>
        </div>
    </div>
</div>
<?
}
?>

If you need to find out what the previous testimony is, $row_anterior!

0

From what I understand you want to list everything putting the first record in one format and the second in another, for this is very simple, just check the rest of the division:

<?php
$result = $connection->query("SELECT * FROM testemunhos") or die($connection->error);
$collection = $result->fetch_array(MYSQLI_ASSOC);
$i = 0;
foreach ($collection as $key => $value):
?>
<div class="item">
 <?php
$class = ($i % 2 == 0) ? "quote-left" : "quote-right";
$close_div = ($i % 2 == 0) ? ' <div class="quote-sep"></div>' : '</div>'."\n";
?>
    <div class="<?=$class?>">
        <img src="<?=$value['testemunho_icon']?>" border="" alt=""/>
        <div class="ql-content">
            <p><?=$value['testemunho_texto']?></p>
            <cite>-- <?=$value['testemunho_cliente']?>, <span><?=$value['testemunho_empresa']?></span></cite>
        </div>
    </div>

<?php
 echo $close_div;
 $i++;

endforeach;
?>

Browser other questions tagged

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