[英]Modx Revolution 2.2.8-pl simple database table query
I'm new with Modx and I can't seem to figure out how to make a simple snippet that displays data from this table. 我是Modx的新手,似乎无法弄清楚如何制作显示此表中数据的简单代码段。
<?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>
My snippet looks something like this: 我的代码段看起来像这样:
<?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;
?>
and the template through which I'm trying to print out the data: 以及用于尝试打印数据的模板:
<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>
I've been searching for an answer for the past couple of hours and I can't find a simple document that describes a simple query similar to what I'm looking for. 在过去的几个小时中,我一直在寻找答案,但是找不到一个简单的文档来描述与我所寻找的内容相似的简单查询。
Any help would pe much appreciated. 任何帮助将不胜感激。
Oh, and the error I'm currently getting is: 哦,我当前遇到的错误是:
Warning: Invalid argument supplied for foreach() in .../core/cache/includes/elements/modsnippet/82.include.cache.php on line 12
Kind regards, 亲切的问候,
Alex 亚历克斯
LATER EDIT. 之后编辑。
Thanks to Sean Kimball I ended up having this, which works great for printing out one element from the table. 多亏了肖恩·金博尔(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;
}
You can try something like: 您可以尝试如下操作:
$results = $modx->query("SELECT * FROM some_table");
while ($r = $results->fetch(PDO::FETCH_ASSOC)) {
print_r($r); exit;
}
It's documented quite well here: http://rtfm.modx.com/xpdo/2.x/class-reference/xpdo/xpdo.query 它在这里很好地记录在案: http : //rtfm.modx.com/xpdo/2.x/class-reference/xpdo/xpdo.query
Also, if you wave the wayfinder model loaded, you should be able to do without all the extra sql, take a peek at, getObject, getCollection: http://rtfm.modx.com/xpdo/2.x/getting-started/using-your-xpdo-model/retrieving-objects 另外,如果您加载了Wayfinder模型,则无需使用所有额外的sql,就可以了解一下getObject,getCollection: http: //rtfm.modx.com/xpdo/2.x/getting-started /使用您的xpdo模型/检索对象
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.