我们打开页面时如何调用php函数

时间:2017-03-28 11:12:41

标签: php

/*the function which fetches data from database */public function allClients()
{
    try
    {
        $stmt = $this->conn->query("SELECT * FROM client");
        $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
        return $results;
    }
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
    }
}

这是调用该函数的下一页。我使用了一个获取按钮:'btn-signup'但是我想在不使用任何按钮的情况下在表格中显示这些数据。我是怎么做的。

$client2 = new client2();
if (isset($_POST['btn-signup'])) 
{  
    try
    {   
            $results=$client2->allClients();

            foreach(array ($results) as $row) 
        {


            $id = $row['id'];
            $name = $row['name'];
            $email = $row['email'];
            echo $id."\t\t";
            echo $name."\t\t";
            echo $email;
            echo "\n\n";

        }

    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
    }

}

我需要获取值的表。感谢你

<table id="data-table-command" class="table table-striped table-vmiddle">
                            <thead>
                                <tr>
                                    <th data-column-id="id" data-type="numeric">ID</th>
                                    <th data-column-id="sender">Sender</th>
                                    <th data-column-id="received" data-order="desc">Received</th>
                                    <th data-column-id="commands" data-formatter="commands" data-sortable="false">Commands</th>
                                </tr>
                            </thead>
                            <tbody>
                                                        <tr>
                                    <td>10238</td>
                                    <td>eduardo@pingpong.com</td>
                                    <td>14.10.2013</td>
                                </tr>
                                <tr>
                                    <td>10243</td>
                                    <td>eduardo@pingpong.com</td>
                                    <td>19.10.2013</td>
                                </tr></tr>
                            </tbody>
                        </table>

4 个答案:

答案 0 :(得分:1)

<table id="data-table-command" class="table table-striped table-vmiddle">
    <thead>
        <tr>
           <th data-column-id="id" data-type="numeric">ID</th>
           <th data-column-id="sender">Sender</th>
           <th data-column-id="received" data-order="desc">Received</th>
           <th data-column-id="commands" data-formatter="commands" data-sortable="false">Commands</th>
        </tr>
    </thead>
    <tbody>
        <?php
        foreach( array($results) as $row ) 
        {
           echo '<tr>';
           echo '<td>' . $row['id'] . '</td>';
           echo '<td>' . $row['name'] . '</td>';
           echo '<td>' . $row['email'] . '</td>';
           echo '</tr>';
        }
        ?>
    </tbody>
</table>

您在表格内foreach打印带有正确表格标记的变量

答案 1 :(得分:0)

更改

echo $id."\t\t";
echo $name."\t\t";
echo $email;
echo "\n\n";

echo "<tr><td>$id</td><td>$name</td><td>$email</td></tr>;

在表格结束前调用该函数。  而且,你还有一个额外的&lt; / TR&GT;在关闭身体标签之前

</tr></tbody>

答案 2 :(得分:0)

那么为什么不用PHP构建通用表呢?我可以看到你没有做AJAX-Request或类似的东西。

类似的东西:

$args = array(
    'numberposts' => 5,
    'post_type' => 'fvc_members',
    'meta_query' => array(
        array(
            'key'       => 'user',
            'value'     => '"' . get_current_user_id() . '"',
        )
    )
);

答案 3 :(得分:0)

问题在于功能,这是正确的代码。它现在正常运作

public function allClients()//function 
    {
        try
        {
            $stmt = $this->conn->query("SELECT * FROM client");
            $stmt->execute();
            $results = $stmt->setFetchMode(PDO::FETCH_ASSOC);
            return $stmt;

        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }
    }

函数调用

 <table id="data-table-command" class="table table-striped table-vmiddle">
                         <thead>
                            <tr>
                                <th data-column-id="id" data-type="numeric">ID</th>
                                <th data-column-id="sender">Sender</th>
                                <th data-column-id="received" data-order="desc">Received</th>
                                <th data-column-id="commands" data-formatter="commands" data-sortable="false">Commands</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php

                            $results=$client2->allClients();

                            foreach($results as $row){
                                echo "<tr><td>".$row['id']."</td>";
                                echo "<td>".$row['name']."</td>";
                                echo "<td>".$row['username']."</td>";
                                echo "<td>".$row['whatphn']."</td>";
                            //  echo "<td>".$row['email']."</td></tr>";

                            }
                        ?>
                        </tbody>
                    </table>