$rowcount=0;
$prodcount=0;
while (($record = fgetcsv($handle, 1000, "|")) !== FALSE) {
//here, the csv file is opened
$numrecords = count($record);
$produs=$record[1]; //records read
$pret=$record[3];
$imagine=$record[4];
if ($rowcount < $linecount) {
//linecount is the number of lines in my csv file and i take 1 from it. for some reason it shows one extra entry when i use count()
if($rowcount > 0) //this line skips the first csv line (table head)
{
if ($rowcount %5 != 0) {
//this line is where my problem lies. i think it's because of the modulus operator
if ($prod_count==5) {$prod_count="1";}
if ($prod_count==1) {print ("<tr>");} //these two lines limit the drawn tables to 4 per row
print "some table";
}else {
//this line is where I think i can fix it. Just need to decrement $rowcount by one. $rowcount -= 1; thing is: it doesn't work for some reason when i take 1 from $rowcount, the whole script freezes and only displays 4 table out of 12, which is the number of rows minus the first one in my csv file.
$table_lastrow=0;
if ($rowcount <= $linecount){
print ("</tr><tr><td height='30'></td></tr>");
}
}
}}
$rowcount++;
$prod_count++;
}
问问题
410 次
2 回答
0
如果我理解您想要的正确,那么您会使这变得更加复杂,并带有许多不需要的额外变量。
查看以下代码是否产生了您所期望的(请注意,这会产生一个没有数据的表,这是您的代码产生的,因为您从不使用$produs
,$pret
或$imagine
任何地方):
// Do this here to skip the header row
fgetcsv($handle, 1000);
echo "<table>\n <tr>\n";
for ($prod_count = 0; ($record = fgetcsv($handle, 1000, "|")) !== FALSE; $prod_count++) {
$numrecords = count($record);
$produs = $record[1];
$pret = $record[3];
$imagine = $record[4];
if ($prod_count < $linecount) {
if ($prod_count && (!$prod_count % 4)) echo " </tr>\n <tr>\n";
echo " <td height='30'></td>\n";
}
}
echo " </tr>\n</table>";
于 2012-01-20T17:21:03.227 回答
0
也许这更适合您想要的东西..在行中阅读并将第一行放在第一列中,将第二行放在第二列中,将第三行放在第三列中,将 4. 放在 4. - 5. 中,然后将 6. 再次放入第一列....
$rowcount=0;
// skip header row
fgetcsv($handle, 1000);
print "<table>";
while (($record = fgetcsv($handle, 1000, "|")) !== FALSE) {
if ($rowcount%5 == 0) print '<tr>'; //open row
if ($rowcount%5 == 4) ;//skip 5. row
else print "<td>data</td>";
if ($rowcount%5 == 3) print '</tr>'; //close row
$rowcount++;
}
//need to be at the end because the if statement would otherwise never fulfill
//if ($rowcount <= $linecount)
print ("<tr><td height='30'></td></tr></table>");
于 2012-01-20T17:46:37.497 回答