How to print next line

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 the% s of the% database to appear next, so that it prints the same record in both quote-right and quote-left

Any Suggestions?

    
asked by anonymous 24.03.2015 / 15:54

4 answers

1

You can create a counter variable $ i for example if it is even you change the class to quote-right and do not quote-left.

    
24.03.2015 / 15:59
0

The best solution for this is to use the mysql_data_seek or similar function according to sgdb.

This function allows you to control the read pointer of the records in your SQL query.

link

    
30.06.2015 / 04:41
0

Márcio , you must pass the result to a array and then, with the numeric keys, find out the 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 testimonial, uncomment $row_anterior !

    
30.06.2015 / 07:14
0

So I understand you want to list everything by putting the first record in one format and the second one in another, so this is very simple, you just have to 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;
?>
    
31.07.2015 / 22:26