PHP 循环数据不会在 while 函数之外回显,或者如何通过在 while 函数之外回显来传递循环数据?
我有一个数据循环,它只会回显 while 函数内部的循环,但如果我在 while 函数外部调用/回显循环数据,它只会运行第一个循环。
示例:
$num = mysql_num_rows($queryFromDB);
$i=0;
while($i < $num)
{
$field1= mysql_result($queryFromDB,$i,"field1");
$field2= mysql_result($queryFromDB,$i,"field2");
$bothFields = $field1 . " " . $field2 "\n";
// This will show 2 rows of data
echo $bothFields;
$i++;
// This will only show 1 row of data. How can I pass the looped data to another variable?
echo $bothFields;
}
我想显示的输出是:
TITLE/HEADER GOES HERE in the 1st Line
-1st Row of Data from DB
-2nd Row of Data from DB
这是实际的代码: $num = mysql_num_rows($qWoundAssessment);
$i=0; 而 ($i < $num) { $wndType = mysql_result($qWoundAssessment,$i,"wndType"); $wndNum = mysql_result($qWoundAssessment,$i,"wndNum"); $wndLocation = mysql_result($qWoundAssessment,$i,"wndLocation"); $wndStage = mysql_result($qWoundAssessment,$i,"wndStage"); $wndL = mysql_result($qWoundAssessment,$i,"wndL"); $wndD = mysql_result($qWoundAssessment,$i,"wndD"); $wndW = mysql_result($qWoundAssessment,$i,"wndW"); $wndAseptic = mysql_result($qWoundAssessment,$i,"wndAseptic"); $wndIrrigate = mysql_result($qWoundAssessment,$i,"wndIrrigate"); $wndIrrigateBox = mysql_result($qWoundAssessment,$i,"wndIrrigateBox"); $wndPat = mysql_result($qWoundAssessment,$i,"wndPat"); $wndCover = mysql_result($qWoundAssessment,$i,"wndCover"); $wndCoverBox = mysql_result($qWoundAssessment,$i,"wndCoverBox"); $wndSecure = mysql_result($qWoundAssessment,$i,"wndSecure"); $wndSecureBox = mysql_result($qWoundAssessment,$i,"wndSecureBox"); $wndQvisit = mysql_result($qWoundAssessment,$i,"wndQvisit");
$wnd = "-" 。 $wndType 。 ” “。 “#”。 $wndNum . “,”。 “地点 ” 。 $wndLocation 。 “,”。 “阶段 ” 。 $wndStage; $wndSize = "大小" 。 $wndL 。 “厘米” 。 “X”。 $wndW 。 “厘米” 。 “X”。 $wndD; if($wndAseptic=="1"){$wndAsepticTech = "使用无菌技术";} if($wndIrrigate=="1"){$wndIrrigateWith = "灌溉" . $wndIrrigateBox;} if($wndPat=="1"){$wndPatDry = "PAT DRY";} if($wndCover=="1"){$wndCoverWith = "覆盖" . $wndCoverBox;} if($wndSecure=="1"){$wndSecureWith = "覆盖" . $wndSecureBox;} if($wndQvisit=="1"){$wndQv = "Q 访问";}
if(isset($wnd, $wndSize, $wndAsepticTech, $wndIrrigateWith, $wndPatDry, $wndCoverWith, $wndSecureWith, $wndQv)){ $woundCare = implode(", ",array($wnd, $wndSize, $wndAsepticTech, $wndIrrigateWith, $wndPatDry, $wndCoverWith, $wndSecureWith, $wndQv)) 。 "\n\n ";}
$wndCare .= $woundCare;
$i++; }
$snWoundCare = "SN 为伤口护理提供熟练的护理访问:" . “\n”。 $wndCare;
如果我回显 $wndCare,它会显示实际循环数据的“未定义变量”错误。但如果我将此变量传递给 PDF,它就会起作用。
SN 为伤口护理提供熟练的护理就诊:
-1 号压力性溃疡,尾骨位置 3,尺寸 2.0 厘米 X 1.5 厘米 X 0.07,使用无菌技术,用生理盐水冲洗,拍干,用 AQUACEL AG 覆盖,用 MEPILEX 覆盖边境、Q 访问
-手术伤口 #2,位置 (R) 膝盖,尺寸 29CM X 0CM X 0,使用无菌技术,用生理盐水冲洗,拍干,用 AQUACEL AG 覆盖,用 MEPILEX 边框覆盖,Q 就诊
====== ========== 代码现在可以运行了!!!这是我的最终解决方案====================== $num = mysql_num_rows($qWoundAssessment); $i=0; $storeMyData = 数组(); while($i < $num) { $wnd=“-”。 mysql_result($qWoundAssessment,$i,"wndType") 。 “#”。 mysql_result($qWoundAssessment,$i,"wndNum"). “, 地点 ” 。 mysql_result($qWoundAssessment,$i,"wndLocation") 。 “, 阶段 ” 。 mysql_result($qWoundAssessment,$i,"wndStage"); $wndSize = "大小" 。 mysql_result($qWoundAssessment,$i,"wndL") 。 “厘米” 。 “X”。 mysql_result($qWoundAssessment,$i,"wndW") 。 “厘米” 。 “X”。 mysql_result($qWoundAssessment,$i,"wndD") 。 “厘米”; if(isset($rowWoundAssessment['wndAseptic'])){$wndAsepticTech = "使用无菌技术";} if(isset($rowWoundAssessment['wndIrrigate'])){$wndIrrigateWith = "灌溉" 。 mysql_result($qWoundAssessment,$i,"wndIrrigateBox");} if(isset($rowWoundAssessment['wndPat'])){$wndPatDry = "PAT DRY";} if(isset($rowWoundAssessment['wndCover'])){$wndCoverWith = "覆盖" 。 mysql_result($qWoundAssessment,$i,"wndCoverBox");} if(isset($rowWoundAssessment['wndSecure'])){$wndSecureWith = "安全" 。 mysql_result($qWoundAssessment,$i,"wndSecureBox");} if(isset($rowWoundAssessment['wndQvisit'])){$wndQvisit = "Q VISIT";}
$wndCare = implode (", ", array($wnd, $wndSize, $wndAsepticTech, $wndIrrigateWith, $wndPatDry, $wndCoverWith, $wndSecureWith, $wndQvisit)). "\n\n";
// This will show 2 rows of data
$storeMyData[] = $wndCare ; // store current data in array
$i++;
}
/* 这将回显循环的存储数据 */ foreach($storeMyData as $prevData)
/* 或使用字符串连接 / 连接数据 $allFinalData2 = ""; / 这将回显循环的存储数据 */ foreach($storeMyData 作为 $prevData) { $allFinalData2 = $allFinalData2.$prevData ; // 继续连接 } echo“SN 为伤口护理提供熟练的护理就诊:”。 “\n”。 $allFinalData2;
感谢 DhruvPathak 和安东尼奥·拉古纳!你们是最棒的!刚刚度过了我的一天! 在房间里跳来跳去
I have a loop of data that will only echo the loop inside the while function, but if i call/echo the looped data outside the while function, it only runs the 1st loop.
SAMPLE:
$num = mysql_num_rows($queryFromDB);
$i=0;
while($i < $num)
{
$field1= mysql_result($queryFromDB,$i,"field1");
$field2= mysql_result($queryFromDB,$i,"field2");
$bothFields = $field1 . " " . $field2 "\n";
// This will show 2 rows of data
echo $bothFields;
$i++;
// This will only show 1 row of data. How can I pass the looped data to another variable?
echo $bothFields;
}
The output that I wanted to show is:
TITLE/HEADER GOES HERE in the 1st Line
-1st Row of Data from DB
-2nd Row of Data from DB
Here's the actual code:
$num = mysql_num_rows($qWoundAssessment);
$i=0;
while ($i < $num)
{
$wndType = mysql_result($qWoundAssessment,$i,"wndType");
$wndNum = mysql_result($qWoundAssessment,$i,"wndNum");
$wndLocation = mysql_result($qWoundAssessment,$i,"wndLocation");
$wndStage = mysql_result($qWoundAssessment,$i,"wndStage");
$wndL = mysql_result($qWoundAssessment,$i,"wndL");
$wndD = mysql_result($qWoundAssessment,$i,"wndD");
$wndW = mysql_result($qWoundAssessment,$i,"wndW");
$wndAseptic = mysql_result($qWoundAssessment,$i,"wndAseptic");
$wndIrrigate = mysql_result($qWoundAssessment,$i,"wndIrrigate");
$wndIrrigateBox = mysql_result($qWoundAssessment,$i,"wndIrrigateBox");
$wndPat = mysql_result($qWoundAssessment,$i,"wndPat");
$wndCover = mysql_result($qWoundAssessment,$i,"wndCover");
$wndCoverBox = mysql_result($qWoundAssessment,$i,"wndCoverBox");
$wndSecure = mysql_result($qWoundAssessment,$i,"wndSecure");
$wndSecureBox = mysql_result($qWoundAssessment,$i,"wndSecureBox");
$wndQvisit = mysql_result($qWoundAssessment,$i,"wndQvisit");
$wnd = "-" . $wndType . " " . "#" . $wndNum . ", " . "LOCATION " . $wndLocation . ", " . "STAGE " . $wndStage;
$wndSize = "SIZE " . $wndL . "CM" . " X " . $wndW . "CM" . " X " . $wndD;
if($wndAseptic=="1"){$wndAsepticTech = "USING ASEPTIC TECHNIQUE";}
if($wndIrrigate=="1"){$wndIrrigateWith = "IRRIGATE WITH " . $wndIrrigateBox;}
if($wndPat=="1"){$wndPatDry = "PAT DRY";}
if($wndCover=="1"){$wndCoverWith = "COVER WITH " . $wndCoverBox;}
if($wndSecure=="1"){$wndSecureWith = "COVER WITH " . $wndSecureBox;}
if($wndQvisit=="1"){$wndQv = "Q VISIT";}
if(isset($wnd, $wndSize, $wndAsepticTech, $wndIrrigateWith, $wndPatDry, $wndCoverWith, $wndSecureWith, $wndQv)){
$woundCare = implode(", ",array($wnd, $wndSize, $wndAsepticTech, $wndIrrigateWith, $wndPatDry, $wndCoverWith, $wndSecureWith, $wndQv)) . "\n\n ";}
$wndCare .= $woundCare;
$i++;
}
$snWoundCare = "SN TO PROVIDE SKILLED NURSING VISITS FOR WOUND CARE:" . "\n" . $wndCare;
if I echo $wndCare, it shows the "Undefined variable" error with the actual looped data. But if I pass this variable to PDF, it works.
SN TO PROVIDE SKILLED NURSING VISITS FOR WOUND CARE:
-PRESSURE ULCER #1, LOCATION COCCYX, 3, SIZE 2.0CM X 1.5CM X 0.07, USING ASEPTIC TECHNIQUE, IRRIGATE WITH NORMAL SALINE, PAT DRY, COVER WITH AQUACEL AG, COVER WITH MEPILEX BORDER, Q VISIT
-SURGICAL WOUND #2, LOCATION (R) KNEE, , SIZE 29CM X 0CM X 0, USING ASEPTIC TECHNIQUE, IRRIGATE WITH NORMAL SALINE, PAT DRY, COVER WITH AQUACEL AG, COVER WITH MEPILEX BORDER, Q VISIT
================ CODE NOW WORKS!!! HERE's MY FINAL SOLUTION ======================
$num = mysql_num_rows($qWoundAssessment);
$i=0;
$storeMyData = array();
while($i < $num)
{
$wnd= "-" . mysql_result($qWoundAssessment,$i,"wndType") . " #" . mysql_result($qWoundAssessment,$i,"wndNum"). ", LOCATION " . mysql_result($qWoundAssessment,$i,"wndLocation") . ", STAGE " . mysql_result($qWoundAssessment,$i,"wndStage");
$wndSize = "SIZE " . mysql_result($qWoundAssessment,$i,"wndL") . "CM" . " X " . mysql_result($qWoundAssessment,$i,"wndW") . "CM" . " X " . mysql_result($qWoundAssessment,$i,"wndD") . "CM";
if(isset($rowWoundAssessment['wndAseptic'])){$wndAsepticTech = "USING ASEPTIC TECHNIQUE";}
if(isset($rowWoundAssessment['wndIrrigate'])){$wndIrrigateWith = "IRRIGATE WITH " . mysql_result($qWoundAssessment,$i,"wndIrrigateBox");}
if(isset($rowWoundAssessment['wndPat'])){$wndPatDry = "PAT DRY";}
if(isset($rowWoundAssessment['wndCover'])){$wndCoverWith = "COVER WITH " . mysql_result($qWoundAssessment,$i,"wndCoverBox");}
if(isset($rowWoundAssessment['wndSecure'])){$wndSecureWith = "SECURE WITH " . mysql_result($qWoundAssessment,$i,"wndSecureBox");}
if(isset($rowWoundAssessment['wndQvisit'])){$wndQvisit = "Q VISIT";}
$wndCare = implode (", ", array($wnd, $wndSize, $wndAsepticTech, $wndIrrigateWith, $wndPatDry, $wndCoverWith, $wndSecureWith, $wndQvisit)). "\n\n";
// This will show 2 rows of data
$storeMyData[] = $wndCare ; // store current data in array
$i++;
}
/* this will echo your storedData of loop */
foreach($storeMyData as $prevData)
/* or join the data using string concatenation /
$allFinalData2 = "";
/ this will echo your storedData of loop */
foreach($storeMyData as $prevData)
{
$allFinalData2 = $allFinalData2.$prevData ; // keep on concatenating
}
echo "SN TO PROVIDE SKILLED NURSING VISITS FOR WOUND CARE:" . "\n" . $allFinalData2;
thanks to DhruvPathak and Antonio Laguna! You guys are the best! Just made my day! jumps around the room
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该有效:
我看到的问题是,您正在测试所有变量是否先前已设置,这可能会产生奇怪的事情,因为您有时会建立它们,有时则不会。
This should work:
The issue I see is that you were testing if all variables where previously setted and this could make strange things as you were stablishing them sometimes and sometimes don't.
我不确定您想如何处理您的数据。看来你想存储
所有数据在循环之外使用它,那么这就是要走的路:
I am not sure what you want to do with your data. It seems you want to store
all the data to use it outside the loop, then this is the way to go :