user509433
user509433

Reputation:

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.

Upvotes: 4

Views: 904

Answers (3)

Angelin Nadar
Angelin Nadar

Reputation: 9300

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 ?

Upvotes: 0

lonesomeday
lonesomeday

Reputation: 237965

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'];
}

Upvotes: 1

Tudor Constantin
Tudor Constantin

Reputation: 26861

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);

Upvotes: 1

Related Questions