1

I am trying put row['user'] into cell but it doesn't work.

When I uncomment "echo" it works fine.

PHP:

function mod_Something($database)
{
    $sql = "SELECT user FROM table_name";
    if ($result = mysqli_query($database, $sql)) {
        while ($row = mysqli_fetch_assoc($result)) {
            $html = $html . '<tr><td>' . $row['user'] . '</td></tr>';
            // echo $row['user'];
        }
    return $html;
    }
}

I also have a HTML view file where I have:

<table id="data-table-basic" class="table table-striped">
    <thead>
    <tr>
        <th>user</th>
    </tr>
    </thead>
    <tbody>
         %mod_Something%
    </tbody>
</table>

I know that HTML isn't a function but I must return it because there is a script which allows to return "view".

9
  • What does it do? What do you expect it to do? Why? Commented Feb 8, 2017 at 9:45
  • it should put username from database into cell. Commented Feb 8, 2017 at 9:46
  • You might want to define $html before the while loop, otherwise the first time it tries to prepend it to the table row it doesn't exist. Also, try changing the final return $html to echo $html Commented Feb 8, 2017 at 9:46
  • rather than echo do a var_dump() and let us know the output, also set error reporting on it will tell you whats up stackoverflow.com/questions/1053424/… Commented Feb 8, 2017 at 9:48
  • $html "function" is defined in other file Commented Feb 8, 2017 at 9:49

2 Answers 2

1

try this:

   function mod_Something($database)
    {
        $sql = "SELECT user FROM table_name";
        if ($result = mysqli_query($database, $sql)) {
            while ($row = mysqli_fetch_assoc($result)) {
                echo '<tr><td>' . $row['user']; . '</td></tr>';
            }
        return $html;
        }
    }


<table id="data-table-basic" class="table table-striped">
    <thead>
    <tr>
        <th>user</th>
    </tr>
    </thead>
    <tbody>
         <?php mod_Something(); ?>
    </tbody>
</table>
Sign up to request clarification or add additional context in comments.

Comments

0

You are not getting anything from the function mod_Something because you are not building & returning HTML properly. See the below code.

function mod_Something($database)
{
    $html = "";
    $sql = "SELECT user FROM table_name";
    if ($result = mysqli_query($database, $sql)) {
        while ($row = mysqli_fetch_assoc($result)) {
            $html .= '<tr><td>' . $row['user'] . '</td></tr>';            
        }    
    }
    return $html;
}

Hope this works for you!

Comments

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.