Search code examples
phpcsvfgetcsv

displaying csv data in four columns in php


I have complaint.csv file in which data is like below:

1,complaint of health
2,complaint of money
.
.
.
71,complaint of bill

I want to show above data in four columns in PHP such that each column will have equal numbers of rows.

I have tried below code in which I couldn't able to get success.

           <?php 

  $fp = file('../complaint.csv', FILE_SKIP_EMPTY_LINES);
$total_rows = count($fp);

  $count = $total_rows;
$first_col = ceil($count/ 4); 
$count -= $first_col;
$second_col = ceil($count/ 3);
$count -= $second_col ;
$third_col = ceil($count/ 2);
$forth_col = $count - $third_col ;

while (!feof($fp)) {

$lines[] = fgetcsv($fp, 1024);

}
fclose($fp);

                ?>
           <div class="added">
           <div class="column-left">
                <?php
                for ($i = 0; $i < $first_col; $i++)
                {
                    foreach ( $lines as $line):
                    ?>
                        <label class="checkbox" for="<?php print 'checkbox'.$line[$i][0]; ?>" style="font-size:20px;">
                        <input type="checkbox" name="complaint" value="<?php print $line[$i][0]; ?>" id="<?php print 'checkbox'.$line[$i][0]; ?>" data-toggle="checkbox">
                        <?php print $line[$i][1]; ?>
                        </label>
                    <?php
                    endforeach;
                }
                ?>
           </div>

           <div class="column-center">
            <?php
             $k = $i;
                for ($j = 0; $j < $second_col; $j++)
                {
                    foreach ( $lines as $line):
                    ?>
                        <label class="checkbox" for="<?php print 'checkbox'.$line[$j][0]; ?>" style="font-size:20px;">
                        <input type="checkbox" name="complaint" value="<?php print $line[$j][0]; ?>" id="<?php print 'checkbox'.$line[$j][0]; ?>" data-toggle="checkbox">
                        <?php print $line[$j][1]; ?>
                        </label>
                    <?php
                    endforeach;
                    $k++;
                }
                ?>
          </div>

            <div class="column-center-right">
            <?php
             $m = $k;
                for ($l = 0; $l < $third_col; $l++)
                {
                    foreach ( $lines as $line):
                    ?>
                        <label class="checkbox" for="<?php print 'checkbox'.$line[$l][0]; ?>" style="font-size:20px;">
                        <input type="checkbox" name="complaint" value="<?php print $line[$l][0]; ?>" id="<?php print 'checkbox'.$line[$l][0]; ?>" data-toggle="checkbox">
                        <?php print $line[$l][1]; ?>
                        </label>
                    <?php
                    endforeach;
                    $m++;
                }
                ?>
           </div>

            <div class="column-right">
            <?php
             $n = $k;
                for ($p = 0; $p < $forth_col; $p++)
                {
                    foreach ( $lines as $line):
                    ?>
                        <label class="checkbox" for="<?php print 'checkbox'.$line[$p][0]; ?>" style="font-size:20px;">
                        <input type="checkbox" name="complaint" value="<?php print $line[$p][0]; ?>" id="<?php print 'checkbox'.$line[$p][0]; ?>" data-toggle="checkbox">
                        <?php print $line[$p][1]; ?>
                        </label>
                    <?php
                    endforeach;
                    $n++;
                }
                ?>
           <br/>
          </div>

           </div> 

CSS

    <style>
    .column-left{ float: left; width: 25%; }
    .column-right{ float: right; width:25%; }
    .column-center{ float: left; width: 25%; }
    .column-center-right{ float: left; width: 25%; }
div.added {
    padding: 0px 0px 5px 0px;
    font-size: 22px;
    font-family: "freightbook";
    color: #2a4753;
    text-align: left;
}
    </style>  

I am getting Error like Notice: Uninitialized string offset. The affected lines are between foreach loop

Can anyone please tell me, what went wrong in above code or any other solution of it?


Solution

  • Here's a solution:

    • Variable number of columns
    • Unequal number of lines are filled from left to right
    • Formatting of HTML elements thru styles (CSS)

    Input (complaint.csv)

    1,complaint of health
    2,complaint of money
    3,complaint type 3
    4,complaint type 4
    5,complaint type 5
    6,complaint type 6
    7,complaint type 7
    8,complaint type 8
    9,complaint of bill
    

    Code

    <?php
    
        // Setup ---------------------------------------------------------------        
        define('numcols',4);  // set the number of columns here
        $csv = array_map('str_getcsv', file('./complaint.csv'));
        $numcsv = count($csv);
        $linespercol = floor($numcsv / numcols);
        $remainder   = ($numcsv % numcols);
        // Setup ---------------------------------------------------------------        
    
        // Page, part 1 --------------------------------------------------------
        echo '<html
        <head>
        <style type="text/css">
            BODY { font-family:tahoma,arial,helvetica,sans-serif; font-size:76%; }
            .table { background-color: #e0e0e0; }
            .break { break:both; border:0; with:1px; }
            .column {  border:0; float:left; padding:0.333em; }
        </style>
        </head>
        <body>
        ';    
        // Page, part 1 --------------------------------------------------------
    
        // The n-column table --------------------------------------------------
        echo '<div class="table">'.PHP_EOL;
        echo '   <div class="column">'.PHP_EOL;
    
        $lines = 0;
        $lpc   = $linespercol;
        if ($remainder>0) { $lpc++; $remainder--; }
        foreach($csv as $item) {
            $lines++;
            if ($lines>$lpc) {
                echo '   </div>' . PHP_EOL . '<div class="column">'.PHP_EOL;
                $lines = 1;
                $lpc   = $linespercol;
                if ($remainder>0) { $lpc++; $remainder--; }
            }
            echo '      <label class="checkbox" for="checkbox'.$item[0].'" style="font-size:20px;">
                        <input type="checkbox" name="complaint" value="'.$item[0].'" id="'.$item[0].'" data-toggle="checkbox">'
                            .$item[1].
                        '</label><br />';
        }
        echo '   </div>'.PHP_EOL;
        echo '</div>'.PHP_EOL;
        // The n-column table --------------------------------------------------
    
        // Page, part 2 --------------------------------------------------------             
        echo '</body>
        </html>
        ';
        // Page, part 2 --------------------------------------------------------        
    
    ?>
    

    Result

    enter image description here