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".

Alexander Rossa
  • 1,900
  • 1
  • 22
  • 37
RamZes
  • 23
  • 4

2 Answers2

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>
PhpDude
  • 1,542
  • 2
  • 18
  • 33
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!

Niklesh_Chauhan
  • 647
  • 5
  • 16