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.";
}
?>
is the equivalent of
if you add more elements to the condition you will be checking for
so if it finds
it stops.
so with correct parenthesis if you had :
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:
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