Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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>');
share|improve this question
1  
Can you add more code please? This doesn't show how you are using php etc –  Andy Holmes Oct 27 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 –  Andy Holmes Oct 27 at 22:54
 
I've edited the code, if it makes it easier :D –  iWumbo Oct 27 at 23:20
 
Much better, helps everyone out when they know exactly what they are working with :) Thanks –  Andy Holmes Oct 27 at 23:23

1 Answer

up vote 1 down vote accepted

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;
}
share|improve this answer
 
I will try this, and get back to you. Edit: Returning shows only ONE name in the element. Why is that? And Tanks BTW. –  iWumbo Oct 27 at 23:14
 
Sec, will edit with code... –  Ing Oct 27 at 23:22
 
Works great! I avoided the assignment and simply put a return statement. That's why it showed one name. Thanks :D –  iWumbo Oct 27 at 23:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.