【发布时间】:2017-11-05 00:21:49
【问题描述】:
我是 Modx 的新手,我似乎不知道如何制作一个简单的 sn-p 来显示该表中的数据。
<?xml version="1.0" encoding="UTF-8"?>
<model package="wayfinder" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
<object class="wayfindertable" table="wayfinder" extends="xPDOSimpleObject">
<fields ..... />
</object>
</model>
我的 sn-p 看起来像这样:
<?php
$output = '';
$result = $modx->query("SELECT item_title,item_username,item_date,item_image FROM modx_wayfinder");
foreach ( $result as $row){
$properties['item_title'] = $row['item_title'];
$properties['item_username'] = $row['item_username'];
$properties['item_date'] = $row['item_date'];
$properties['fav_image'] = $row['item_img'];
$output = $modx->getChunk('src-res-item.searchbox.main_page',$properties);
};
return $output;
?>
以及我尝试打印数据的模板:
<div class="cs-item">
<a href="#" class="cs-attachment only-lrg">
<img src="[[+fav_image]]" alt="main attachment">
</a>
<ul class="cs-head">
<li class="cs-title">
<h4><a href="#">[[+item_title]]</a></h4>
</li>
</ul>
<div class="cs-body">
<div class="cs-item-details">
<p>Posted by <a href="#" class="user-name" data-ajax="false">[[+item_username]]</a> on <a href="#" class="post-date" data-ajax="false">[[+item_date]]</a></p>
<p>Located in <a href="#" class="item-location" data-ajax="false">[[+item_loc_country]], [[+item_loc_city]]</a></p>
<p>The item has been viewed <span class="item-views">[[+views]]</span> times</p>
</div>
</div>
<div class="cs-footer only-sml">
<a href="[[~10]]" class="btn" data-ajax="false">View item</a>
<a href="[[~11]]" class="btn btn-alt" data-ajax="false">Contact user</a>
</div>
</div>
过去几个小时我一直在寻找答案,但找不到描述与我正在寻找的简单查询类似的简单文档。
非常感谢任何帮助。
哦,我目前遇到的错误是:
Warning: Invalid argument supplied for foreach() in .../core/cache/includes/elements/modsnippet/82.include.cache.php on line 12
亲切的问候,
亚历克斯
稍后编辑。
感谢 Sean Kimball,我最终得到了这个,它非常适合打印表格中的一个元素。
<?php
$output = '';
$results = $modx->query('SELECT * FROM `modx_wayfinder` ORDER BY ID');
while ($row = $results->fetch(PDO::FETCH_ASSOC)) {
$properties['item']['title'] = $row['item_title'];
$properties['item']['author'] = $row['item_author'];
$properties['item']['country'] = $row['item_loc_country'];
$properties['item']['city'] = $row['item_loc_city'];
$properties['item']['date'] = $row['item_date'];
$properties['fav']['image'] = $row['item_img'];
$output = $modx->getChunk('src-res-item.searchbox.main_page',$properties);
return $output;
exit;
}
【问题讨论】:
标签: php sql modx modx-revolution