简体   繁体   中英

Split mysql data into 3 bootstrap md columns with php

im trying to divide some info retrieved from a mysql db to 3 columns ("col-md-4") without repeating the info. I have been have trouble with mysqli_fecth_assoc() beacuse it replicates the info in all columns. Any idea? Thanks in advance

<div class="container-fluid">
<div class="row">
    <?php while($fila = mysqli_fetch_assoc($resultado)){ ?>
    <div class="col-md-4">
        <p><?php echo $fila["Nombre"];?></p>
        <HR WIDTH="50%" SIZE="3"> 
    </div>
    <div class="col-md-4">
        <p><?php echo $fila["Nombre"];?></p>
        <HR WIDTH="50%" SIZE="3"> 
    </div>
    <div class="col-md-4">
        <p><?php echo $fila["Nombre"];?></p>
        <HR WIDTH="50%" SIZE="3"> 
    </div>
    <?php } ?>
</div>

the problem with thath code is that it repeats the same $fila["Nombre"] in all columns.

I think this code will help to get the expected result.

<?php 
    $tempFila = array();

    while($fila = mysqli_fetch_assoc($resultado)){ 
        $tempFila[] = $fila["Nombre"];
    } 

    $count = count($tempFila);
?>

<div class="container-fluid">
    <div class="row">
        <?php for ($i = 0; $i < $count;) { ?>
        <div class="col-md-4">
            <p><?php echo (!empty($tempFila[$i])) ? $tempFila[$i] : ''; $i++; ?></p>
            <HR WIDTH="50%" SIZE="3"> 
        </div>
        <div class="col-md-4">
            <p><?php echo (!empty($tempFila[$i])) ? $tempFila[$i] : ''; $i++; ?></p>
            <HR WIDTH="50%" SIZE="3"> 
        </div>
        <div class="col-md-4">
            <p><?php echo (!empty($tempFila[$i])) ? $tempFila[$i] : ''; $i++; ?></p>
            <HR WIDTH="50%" SIZE="3"> 
        </div>
        <?php } ?>
    </div>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM