iWumbo
iWumbo

Reputation: 135

PHP array does not print in correct HTML element

I've been trying this for 10 minutes, but cannot find a way. I put several names into a database, and called those names using mysqli_fetch_array() in a function called sort_member_db()

My output code in the function is: echo $names['FirstName']. ' ' . $names['LastName']; echo '<br>';

It prints all the names properly, however the names are not in the div element i want them to be in. They show up on the very top of the website.

Here's what I put in the div element:

 <div class="myclass">
            <hgroup class="title">
                <h1>Text1</h1>
                <h2>
                     text2
                </h2>
            </hgroup>
            <p>
                Array should print after this line:<br>

             '. sort_member_db() .'

            </p>
        </div>

Edit: Here's the entire function

function sort_member_db()
{

    $openConn = mysqli_connect(
            "host", "user", "", "");

    //The sorting function for mysql goes here
    $sortedMembers = mysqli_query($openConn, "SELECT * FROM Members ORDER BY LastName");

    //Get the names and insert them into a PHP array
    while($names = mysqli_fetch_array($sortedMembers))
    {
        //Escape sequence to find the possible error
        if (!$sortedMembers)
        {
            printf("Error: %s\n", mysqli_error($openConn));
            exit();
        }

        //Print out the names here        
        echo $names['FirstName']. ' ' . $names['LastName'];
        echo '<br>';
    }

    mysqli_close($openConn);

}

Here's the div element i'm trying to get the array into:

page_body('        <div id="body">
            <!-- RenderSection("featured", required:=false) -->
            <section class="content-wrapper main-content clear-fix">
                <!-- @RenderBody() -->
                    <section class="featured">
        <div class="content-wrapper">
            <hgroup class="title">
                <h1>Members:</h1>
                <h2>

                </h2>
            </hgroup>
            <p>
                Our Team Members :<br>

             '. sort_member_db() .'

            </p>
        </div>
            </section>
            </section>');

Upvotes: 0

Views: 150

Answers (1)

Ingmar Boddington
Ingmar Boddington

Reputation: 3500

You need to return and concatenate the names in the HTML, rather than echo the names directly in the called function otherwise this will be done first.

function sort_member_db()
{

    $openConn = mysqli_connect(
            "host", "user", "", "");

    //The sorting function for mysql goes here
    $sortedMembers = mysqli_query($openConn, "SELECT * FROM Members ORDER BY LastName");

    //Get the names and insert them into a PHP array
    $returnString = '';
    while($names = mysqli_fetch_array($sortedMembers))
    {
        //Escape sequence to find the possible error
        if (!$sortedMembers)
        {
            printf("Error: %s\n", mysqli_error($openConn));
            exit();
        }

        //Print out the names here        
        $returnString .= $names['FirstName']. ' ' . $names['LastName'];
        $returnString .='<br>';
    }

    mysqli_close($openConn);
    return $returnString;
}

Upvotes: 1

Related Questions