0

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>');
4
  • 1
    Can you add more code please? This doesn't show how you are using php etc Commented Oct 27, 2013 at 22:53
  • 1
    It's also worth pasting the code where the names to echo out, as that seems to be a major part of your question Commented Oct 27, 2013 at 22:54
  • I've edited the code, if it makes it easier :D Commented Oct 27, 2013 at 23:20
  • Much better, helps everyone out when they know exactly what they are working with :) Thanks Commented Oct 27, 2013 at 23:23

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I will try this, and get back to you. Edit: Returning shows only ONE name in the element. Why is that? And Tanks BTW.
Works great! I avoided the assignment and simply put a return statement. That's why it showed one name. Thanks :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.