1

I am having for loop which has around 100 rows. I want to create new columns after every 10 rows.

For example:

1   11
2   12
3   13
4   14
5   15
6   16
7   17
8   18
9   19
10  20 and so on 100 

Following is the PHP code to get my rows from Mysql Database. I want to break the after 10 records to create new and so on.

<ul>
 <?php foreach($row as $rw) { ?>
   <li><?php echo $rw['name']; ?></li>
 <?php } ?>
</ul>
brombeer
  • 8,716
  • 5
  • 21
  • 27
  • 1
    use css column property for this, not by PHP code. https://stackoverflow.com/questions/12332528/how-to-display-list-items-as-columns – Kevin Nov 15 '19 at 06:40
  • @kevin how do you make it start a new column after 10 rows? – Salman A Nov 15 '19 at 07:12

2 Answers2

1

Try this--

  <ul>
  <?PHP 
    $c = 1; // Our counter
    $n = 10; // Each Nth iteration would be a new table row
    %sql = //put here you sql query
    while ($row = mysqli_fetch_array($sql)) {


      if($c % $n == 0 && $c != 0) // If $c is divisible by $n...
      {
       for ($i=1; $i <= $n ; $i++) { 
          while ($row1 = mysqli_fetch_array($sql)) {
            echo "<li>".$row1['uid']."</li>";
          }
           echo "<br>";
       }


        echo "<br>";

      }
      $c++;
    }
  ?> <ul>
SAVe
  • 814
  • 6
  • 22
0

A quick and simple solution using html and css is added below

<html>
    <head>
        <title>Demo Html</title>
    </head>
    <style>
        .blocks{
            width:100%;
        }  
        .block-col{
            width:10%;
            float: left;
        }
        .block-col{
            padding: 0;
            margin: 0;
        }
        .block-col li{
            list-style: none;
        }
    </style>

    <body>
        <?php
            $row = array(
                1,2,3,4,5,6,7,8,9,10,
                11,12,13,14,15,16,17,18,19,20,
                21,22,23,24,25,26,27,28,29,30,
                31,32,33,34,35,36,37,38,39,40,
                41,42,43,44,45,46,47,48,49,50,
                51,52,53,54,55,56,57,58,59,60,
                61,62,63,64,65,66,67,68,69,70,
                71,72,73,74,75,76,77,78,79,80,
                81,82,83,84,85,86,87,88,89,90,
                91,92,93,94,95,96,97,98,99,100
            );
        ?>
        <div class="blocks">
            <ul class="block-col">
                <?php foreach($row as $key => $rw) { ?>
                    <?php 
                        if(($key%10 == 0) && ($key != 0) ) { // for new coloum on each 10th element
                            echo '</ul><ul class="block-col">'; 
                        }
                    ?>
                    <li><?php echo $rw; ?></li>
                <?php } ?>
            </ul>
        </div>
    </body>
</html>
julianstark999
  • 3,450
  • 1
  • 27
  • 41
Ajith
  • 2,476
  • 2
  • 17
  • 38