sql >> Base de Datos >  >> RDS >> Mysql

Dividir datos en 3 columnas

Podría hacer esto usando el operador de módulo, sin embargo, en realidad es posible solo con CSS.

Usando display: inline-block , puede obtener un buen efecto de columna. Eche un vistazo a este JSFiddle aquí . Solo estoy usando JavaScript porque soy perezoso; el <div> la lista sería generada por PHP en su caso. Si desea limitarlos a un cierto ancho, simplemente colóquelos en un contenedor <div> con un ancho fijo.

Se me ocurrió una solución usando tablas, que es realmente lo que deberías estar haciendo (no has dado ningún caso de uso especial). El código está debajo, así como una demostración funcional aquí .

$columns = 4;       // The number of columns you want.

echo "<table>";     // Open the table

// Main printing loop. change `30` to however many pieces of data you have
for($i = 0; $i < 30; $i++)
{
    // If we've reached the end of a row, close it and start another
    if(!($i % $columns))
    {
        if($i > 0)
        {
            echo "</tr>";       // Close the row above this if it's not the first row
        }

        echo "<tr>";    // Start a new row
    }

    echo "<td>Cell</td>";       // Add a cell and your content
}

// Close the last row, and the table
echo "</tr>
</table>";

Y para terminar, tenemos nuestro diseño centrado en columnas, esta vez volviendo a div s. Aquí hay algo de CSS; esto debe colocarse en un archivo separado, no dejarlo en línea .

<?php
$rows = 10;     // The number of columns you want.
$numItems = 30;     // Number of rows in each column

// Open the first div. PLEASE put the CSS in a .css file; inline used for brevity
echo "<div style=\"width: 150px; display: inline-block\">";

// Main printing loop.
for($i = 0; $i < $numItems; $i++)
{
    // If we've reached our last row, move over to a new div
    if(!($i % $rows) && $i > 0)
    {
        echo "</div><div style=\"width: 150px; display: inline-block\">";
    }

    echo "<div>Cell $i</div>";      // Add a cell and your content
}

// Close the last div
echo "</div>";
?>