如何在Slim Framework中使用php变量?

时间:2018-11-05 22:48:51

标签: php slim slim-3

我正在尝试根据用户选择的ID填充信息。我尝试使用苗条的框架定义变量,但是对于如何使用它们或调用它们感到困惑。我的变量存储在listings.php页面上,而我试图在listings.html页面上调用。

我已在下面添加了相关代码。用户可以在另一个页面上选择一个数据库条目进行共享:

function doSomething(x::MyStruct,fieldName::String)

y = x.fieldName

return f(y)

end

如果我选择一个特定的项目(例如项目34),则URL显示为:

<div class="list-option">
<a href="/listings/share/{{listing.id}}" class="option-button settings">Share Listing</a>
</div>

但是现在我想用清单表项目34中的所有相关信息填充该share.html页面。我的listings.php页面如下所示,以从数据库中提取数据:

.../listings/share/34

我的share.html页面如下:

$app->get('/listings/share/{lisitingid}', function ($request, $response, $args) {

    $variables['title'] = 'Share Listing';
    $variables['listing'] = $this->db->select('listing', ['id', 'name', 'details', 'associated_user', 'address', 'location', 'category']);

    return $this->view->render ($response, 'share.html', $variables);
});

我知道这是错误的,我正在尝试使用一种我以前知道的方法来从数据库中调用信息,但是我正在使用Slim框架及其全新的框架。有人能够演示或演示如何根据我的尝试从数据库中获取信息以显示在share.html页面上吗?

1 个答案:

答案 0 :(得分:0)

我建议将share.html重命名为share.php。 正如@Nima在评论中所说,如果您有

$variables['title'] = 'Share Listing';
$variables['listing'] = $this->db->select(
    'listing', 
    [
        'id', 
        'name', 
        'details', 
        'associated_user', 
        'address',  
        'location', 
        'category'
    ]
);

return $this->view->render ($response, 'share.html', $variables);

然后,您可以使用名称share.php$title$listing内部对其进行访问。

您需要从

更改share.php的内容
<div class='listing-category'>.$row['category']."'</div>

类似(假设$listing是数据库记录的数组)

<?php foreach($listing as $row) : ?>
    <div class='listing-category'><?php echo $row['category']; ?></div>
<?php endforeach; ?>

或使用<?= $var ?>(这是<?php echo $var; ?>的简短版本)

<?php foreach($listing as $row) : ?>
    <div class='listing-category'><?= $row['category']; ?></div>
<?php endforeach; ?>

要在$variables['title']中打印share.php的内容,您可以像

<div><?= $title ?></div>
相关问题