PHP How to increment only 4 rows of CSV file

315 views Asked by At

I am trying, to display some data from a CSV file, and need to show only the 4 first rows. Each row is inside an 'ul' list. Right now everything shows up, so all ul's are displayed. I tryed to add another 'while' loop but everything got broken.

I'm sure this is easy for someone who is used to code in php. I am learning php right now so I will carefully study the good answer to understand just what changes I need to do. Here is the code:

<?php

        $row = 1;
        if (($handle = fopen($this->getBaseUrl()."csvfile/csv_test.csv", "r")) !== FALSE) {

            echo '<ul>';

            $count = 0;
            while (($data = fgetcsv($handle, 1000, ",",'"')) !== FALSE) {

            $name=$data[0];
            $bonus=$data[1];
            $start=$data[2];
            $end=$data[3];
            $brand=strtok($name, " - ");

                $count++;
                echo
                '<li><ul><li><img width="90" height="40" src="'
                .$this->getSkinUrl('images/logos/').strtolower($brand).'.jpg" /></li>';
                echo '<li>'.$name.'</li>';
                echo '<li class="bonus"><strong>'.$bonus.'</strong></li>';
                echo '<li><span class="green">Start: '.$start.'</span></li>';
                echo '<li><span class="red"><strong>end: '.$end.'</strong></span></li>';
                echo '<li class="pdf"><a target="_blank" href="'
                .$pdf.'" title=""><img src="'.$this->getSkinUrl('images/icon-pdf.png')
                .'"/><br/>PDF</a></li></ul></li>';
                ?>

            <?php
            }   
            echo '</ul>';
            fclose($handle);
        }

        else {
                echo "NOPE.";
            }

        ?>
1

There are 1 answers

0
Cups On
while (($data = fgetcsv($handle, 1000, ",",'"')) !== FALSE) 

is the equivalent of

while(true)

if you add more elements to the condition you will be checking for

while(true && true) // so loop carries on

so if it finds

while(true && false) // equates to false, so loop will stop

it stops.

so with correct parenthesis if you had :

while (
      ($data = fgetcsv($handle, 1000, ",",'"')) !== false // true
      &&
      $count < 5 // true
      ){}

I'm sorry but am not in a place to test what I just wrote, but in principle this is ONE WAY to stop at 4 iterations.

There are many others, many are more readable than this.

But I just wanted to answer your immediate question.

I find foreach() much easier to read, something like, with dumps so you KNOW what is happening instead of guessing what should be happening:

$data = fgetcsv($handle, 1000, ",",'"');
// var_dump($data) // uncomment this to see what is happening
$count = 1;
foreach($data as $row){
if($count === 5) break;
// var_dump($row); // uncomment this to see what is happening

echo $row[0];
$count++;
// var_dump($count); // uncomment this to see what is happening
}

Again, untested but illustrates just one more way to achieve the same thing.

Do one step, prove it, move to next step.

If it does not work, uncomment the lines and chase the problem back -- that is how you have to think and act.

HTH