0

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.

5
  • Can you show your code, please? That'll be easier. Commented Mar 22, 2016 at 17:19
  • not too much code to add, but thats what i got Commented Mar 22, 2016 at 17:22
  • Why are you printing this $fila["Nombre"] thrice? Since this is within a loop, for a single row you will get the same name thrice. Commented Mar 22, 2016 at 17:41
  • I want to print $fila["Nombre"] in all the columns but not the same one. I know the code doesnt work. If I have 5 diferent $fila["Nombre"]`s i want them all to display in 3 columns but not repeat. Commented Mar 22, 2016 at 17:49
  • Well, stackoverflow is not for most of the assignment questions. Commented Mar 22, 2016 at 17:52

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

5 Comments

There is a possibility to be generated at most 2 unnecessary column. That problem can be fixed using the column into the conditional block.
that works, thanks, what about if im trying to display more than 1 column, for example, i want to display "nombre" and "apellido"
There are several ways to do that. 1. Use another array for "apellido" 2. Use a two dimensional array like this $tempFila[0]['nombre'] = $fila["Nombre"]; $tempFila[0]['apellido'] = $fila["apellido"]; $tempFila[1]['nombre'] = $fila["Nombre"]; $tempFila[1]['apellido'] = $fila["apellido"]; and so on..........
I think is not efficient to do 8 arrays just to save 8 different info.
Yes. So you can choose the second way. See this: $tempFila = array(); $i = 0; while($fila = mysqli_fetch_assoc($resultado)){ $tempFila[$i]["Nombre"] = $fila["Nombre"]; $tempFila[$i]["apellido"] = $fila["apellido"]; $i++; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.