How to echo an array multiple times in one page?
I was wondering if someone could help me with this problem. I want to print/echo the solution of the function multiple times on the same page. Is that possible?
Here's my function:
public function getFeedback($p_iUserid) {
开发者_如何转开发 include("Connection.php"); //open db
try
{
$sql = "select DiaryOpmerkingen, DiaryDoctorcomment from tblDiary
WHERE fk_UserId = ".$p_iUserid."
AND DiaryDay = '".$this->Day."';";
$rResult = mysqli_query($link, $sql);
return $rResult;
}
catch(Exception $e)
{
// no connection database
$feedback = $e->getMessage();
}
mysqli_close($link);
}
And this is how I manage to call on the function by now. But it only works once:
if(mysqli_num_rows($feedbackPatient) > 0)
{
while($oUser = mysqli_fetch_assoc($allUsers))
{
echo $oUser['DiaryOpmerkingen'];
}
}
I hope someone can help! Thanks anyway.
Create a function for your printing task, then call it as many times as you want:
function print_the_stuff($feedbackPatient){
if(mysqli_num_rows($feedbackPatient) > 0)
{
while($oUser = mysqli_fetch_assoc($allUsers))
{
echo $oUser['DiaryOpmerkingen'];
}
}
}
Then, wherever you need to print:
print_the_stuff($feedbackPatient);
The easiest way to do this is probably to get the whole result set in one go, and then pass that around.
This might be costly in terms of performance if the result set is large, but it's probably not going to make a significant difference. It is particularly easy with the mySqli extension, because you can use mysql_fetch_all
.
public function getFeedback($p_iUserid) {
include("Connection.php"); //open db
try
{
$sql = "select DiaryOpmerkingen, DiaryDoctorcomment from tblDiary
WHERE fk_UserId = ".$p_iUserid."
AND DiaryDay = '".$this->Day."';";
$rResult = mysqli_query($link, $sql);
return mysqli_fetch_all($rResult, MYSQLI_ASSOC);
}
catch(Exception $e)
{
// no connection database
$feedback = $e->getMessage();
}
mysqli_close($link);
}
You would then get an associative array returned from getFeedback
, and could loop through this as normal:
$feedback = getFeedback($id);
foreach ($feedback as $item) {
echo $item['DiaryOpmerkingen'];
}
I think it should be :
$feedbackPatient = getFeedback($p_iUserid);
if(mysqli_num_rows($feedbackPatient) > 0)
{
while($oUser = mysqli_fetch_assoc($feedbackPatient))
{
echo $oUser['DiaryOpmerkingen'];
}
}
why are u using diff result for mysqli_num_rows and sqli_fetch_assoc ?
精彩评论