虽然我不推荐这种方法,但这是一种无需进入 Userfrostings MVC 即可查询表并在页面上显示信息的简单方法。
在index.php
下面的功能页面评论中,使用此代码注册具有 url 的页面/mysales
,它还从 user_sales 表中获取用户信息并呈现 mysales.twig 以显示信息。
$app->get('/mysales/?', function () use ($app) {
// Access-controlled page
if (!$app->user->checkAccess('uri_dashboard')){
$app->notFound();
}
$db_config = $app->config('db');
$db_mysqli = new mysqli($db_config['db_host'], $db_config['db_user'], $db_config['db_pass'], $db_config['db_name']);
$sales_rows = array();
$result = $db_mysqli->query('select `description`, `total` from `user_sales` where `user_id` = 10 ');
if($result){
while($row = $result->fetch_assoc()) {
$sales_rows[] = $row;
}
}
$app->render('mysales.twig', [ //calls to render mysales.twig
'sales_rows' => $sales_rows //passing variables to twig
]);
});
现在创建mysales.twig
内部userfrosting/templates/themes/default
文件夹,其中包含代码以显示 sales_rows 树枝变量中的内容。这将扩展,dashboard-layout.twig
以便导航面板保持原位。
{% extends "layouts/layout-dashboard.twig" %}
{% set page_group = "dashboard" %}
{% block page %}
{% set page = page | merge({
"title" : "My Sales",
"description" : ""
}) %}
{{ parent() }}
{% endblock %}
{% block content %}
<h1>My Sales</h1>
<table border="1">
<tbody>
<tr>
<td><strong>Description </strong></td>
<td><strong>Total </strong></td>
</tr>
{% for sales in sales_rows %}
<tr><td>{{ sales.description }}</td><td>{{ sales.total }}</td></tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
现在就是这样,如果您登录并导航到/mysales
您应该从表格中获取信息。