PHP - 每行打印 4 个表(总共 12 个表),跳过表 5 和 10,这是不应该的

发布于 2024-12-28 00:05:58 字数 1361 浏览 2 评论 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++;
}
$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++;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

转瞬即逝 2025-01-04 00:05:58

如果我理解你想要什么,那么你已经让事情变得更加复杂了,因为有许多不需要的额外变量。

看看下面的代码是否产生了您所期望的结果(请注意,这会产生一个没有数据的表,这正是您的代码产生的结果,因为您从不使用 $produs, $pret< /code> 或 $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>";

If I understand what you want right, you have made this a lot more complicated with many extra variables that are not required for this.

See if the following code produces what you are expecting (note that this produces a table with no data in it, which is what your code produces, because you never use $produs, $pret or $imagine anywhere):

// 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>";
我的鱼塘能养鲲 2025-01-04 00:05:58

也许这更适合你想要的东西..阅读行并将第一行放在第一列中,第二行放在第二列中,第三行放在第三列中,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>");

Maybe this fits more the thing you want .. read in the lines and put the first line in the first column, second in the second, third in the third and 4. in the 4. - 5. is skipped and 6. again in the first column ....

$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>");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文