在PHP和MySql中多次打印记录 [英] Printing a Record multiple times in PHP and MySql

查看:74
本文介绍了在PHP和MySql中多次打印记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个网页,对表格进行简单的查询并将该表格显示在页面上,但是有一个复杂的转折让我感到困惑。以下是由逗号分隔的表名和一个示例记录:

  R_ID,B_ID,R_No,RoomName,淋浴,洗眼器污水,洗眼瓶
1,609,609,危险废物排出口,1,1,1

我需要打印此表的每一行,但如果淋浴,洗眼,或EyewashBottles大于0,也会打印多行。例如,我将打印此行三次。如果淋浴为0,我只打印两次(一个用于洗眼器,一个用于EyewashBottles,0用于淋浴)。如果阵雨是2,我会打印4次,等等。



我用来打印的代码如下:

 <?php while($ row = mysql_fetch_array($ result,MYSQL_ASSOC)):?> 
< tr>
< td><?php print $ row [B_ID];?>< / td>
< td><?php print $ row [R_No];?>< / td>
< td><?php print $ row [RoomName];?>< / td>
< td><?php print $ row [Showers];?>< / td>
< td><?php print $ row [eyewashPlumbed];?>< / td>
< td><?php print $ row [EyewashBottles];?>< / td>
< / tr>
<?php endwhile; ?>

问题是我不知道如何中断while循环以便打印相同的行多次。当它从mysql_fetch_array中拉出时,它会进入下一行。 你真的应该切换到PDO / mysqli,但是用你的当前的代码会是这样的(对于 showers 就像你的例子):

 <$ c ($ row = mysql_fetch_array($ result,MYSQL_ASSOC)):
$ count = 2 + $ row [showers]; $ c><?php

for($ i = 0; $ i <$ count)
{
?>
< tr>
< td><?php print $ row [Date];?>< / td>
< td><?php print $ row [Inspector1];?>< / td>
< td><?php print $ row [Inspector2];?>< / td>
< td><?php print $ row [Building_Name];?>< / td>
< td><?php print $ row [Room];?>< / td>
< td><?php print($ i + 1);?>< / td>
< / tr>
<?php
}
endwhile;
?>

您可能想更改 $ row [shower_no] 类似于 $ i + 1 ,因为该列不会出现在您的数据库中。


I am trying to create a webpage that does a simple query of a table and displays the table to a page, but with one complex twist that has got me confused. Here are the column names and one example record from the table separated by commas:

R_ID ,  B_ID ,  R_No ,  RoomName , showers , eyewashPlumbed , EyewashBottles  
1 , 609 ,   609 ,   Hazardous Waste Shed ,  1   ,   1    ,  1

I need to print each row of this table, but also print multiple rows if either showers, eyewashPlumbed, or EyewashBottles is greater than 0. For example, I would print this row three times. If showers was 0 I would only print it two times (one for eyewashPlumbed, one for EyewashBottles, and 0 for showers). If showers was 2 I would print it 4 times, etc.

The code I'm using to print is as follows:

<?php while ($row = mysql_fetch_array($result, MYSQL_ASSOC)): ?> 
    <tr>
    <td><?php print $row["B_ID"];?></td> 
    <td><?php print $row["R_No"];?></td> 
    <td><?php print $row["RoomName"];?></td>
    <td><?php print $row["Showers"];?></td>
    <td><?php print $row["eyewashPlumbed"];?></td>
    <td><?php print $row["EyewashBottles"];?></td>  
    </tr>
<?php endwhile; ?>

The problem is that I don't know how to interrupt the while loop in order to print the same row multiple times. It goes onto the next row as it pulls from the mysql_fetch_array.

解决方案

You really should switch to PDO / mysqli, but with your current code it would be something like (for showers as in your example):

<?php
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)):
  $count = 2 + $row["showers"];
  for ($i = 0; $i < $count)
  {
?> 
    <tr>
    <td><?php print $row["Date"];?></td> 
    <td><?php print $row["Inspector1"];?></td> 
    <td><?php print $row["Inspector2"];?></td>
    <td><?php print $row["Building_Name"];?></td>
    <td><?php print $row["Room"];?></td>
    <td><?php print ($i + 1);?></td>  
    </tr>
<?php
  }
endwhile;
?>

You probably want to change $row["shower_no"] to something like $i + 1 as that column does not appear in your database.

这篇关于在PHP和MySql中多次打印记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆