0
$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++;
}
4

2 に答える 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

多分これはあなたが望むものにもっと適合します..行を読み、最初の行を最初の列に、2番目を2番目、3番目を3番目、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 に答える