简体   繁体   English

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

[英]How can I use php variables with Slim Framework?

I'm trying to populate a page with information based on what id is selected by the user. 我正在尝试使用基于用户选择的ID的信息填充页面。 I have tried defining my variables using the slim framework but I'm confused as to how to use them or call them. 我尝试使用苗条的框架定义变量,但是对于如何使用它们或调用它们感到困惑。 My variables are stored on the listings.php page, and I'm trying to call the on the listings.html page. 我的变量存储在listings.php页面上,而我试图在listings.html页面上调用。

I've included the relevant code below. 我在下面包含了相关代码。 On a different page, the user can select a database entry to share: 在另一个页面上,用户可以选择一个数据库条目进行共享:

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

If I select a specific item (for example item 34), the URL appears as: 如果我选择一个特定的项目(例如,项目34),则URL显示为:

.../listings/share/34

But now I want to populate that share.html page with all relevant information from item 34 of the listings table. 但是,现在我想用清单表项目34中的所有相关信息填充该share.html页面。 My listings.php page looks like this to extract the data from the database: 我的listings.php页面看起来像这样从数据库中提取数据:

$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);
});

And my share.html page looks like this: 我的share.html页面如下所示:

<div class='wrapper'>
    <div class='listing-image'>
    <img src='###'>
    </div>
    <div class='listing-info'>
    <div class='listing-title'>
        <div class='lister-logo'><img src='image/###' alt='Lister Logo'></div>
        <div class='listing-category'>.$row['category']."'</div>
        <h2 class='listing-name'></h2></div>
    <div class='profile-pic'><img src='image/###' alt='profile picture'></div>
    <div class='lister-bio'>
        <h2 class='about'>.$row['name']</h2></div>

        <p class='biography'>".$row['details']"</p>
    </div>
    <div class='lister-contact'>
        <div class='address'>".$row['address']</div>

I know this is wrong, I'm trying to use a previous method I know for calling information from a database but I'm using Slim framework and its totally new to me. 我知道这是错误的,我正在尝试使用以前知道的方法从数据库中调用信息,但是我正在使用Slim框架及其全新的框架。 Would anybody be able to demonstrate or show me how I can get the information from the database to show on the share.html page based on what I have attempted? 有人可以演示或演示如何根据我的尝试从数据库中获取信息以显示在share.html页面上吗?

I suggest to rename share.html as share.php . 我建议将share.html重命名为share.php As @Nima said in the comment, if you have 正如@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);

Then you can access it inside share.php using name $title and $listing . 然后,您可以使用名称$title$listingshare.php访问它。

You need to change share.php content from 您需要从以下位置更改share.php内容

<div class='listing-category'>.$row['category']."'</div>

to something like (assuming $listing is array of database records) 类似于(假设$listing是数据库记录的数组)

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

or using <?= $var ?> (this is short version of <?php echo $var; ?> ) 或使用<?= $var ?> (这是<?php echo $var; ?>简短版本)

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

To print content of $variables['title'] in share.php , you can do it like 要在share.php打印$variables['title']share.php ,您可以像

<div><?= $title ?></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM