Modx Revolution 2.2.8-pl简单数据库表查询

时间:2014-05-14 10:52:56

标签: php sql modx modx-revolution

我是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>

我的代码段看起来像这样:

<?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

亲切的问候,

亚历

LATER EDIT。

感谢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;
}

1 个答案:

答案 0 :(得分:0)

您可以尝试以下内容:

$results = $modx->query("SELECT * FROM some_table");
while ($r = $results->fetch(PDO::FETCH_ASSOC)) {
        print_r($r); exit;
}

这里记录得非常好: http://rtfm.modx.com/xpdo/2.x/class-reference/xpdo/xpdo.query

另外,如果你挥动加载的寻路器模型,你应该可以没有所有额外的sql,看看,getObject,getCollection:http://rtfm.modx.com/xpdo/2.x/getting-started/using-your-xpdo-model/retrieving-objects