【问题标题】:Modx Revolution 2.2.8-pl simple database table queryModx Revolution 2.2.8-pl 简单数据库表查询
【发布时间】: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


    【解决方案1】:

    你可以试试这样的:

    $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

    【讨论】:

    • 在这种情况下,“Wayfinder”这个名字只是一个名字巧合:)
    • “退出;​​”只允许循环运行一次......你可以考虑重命名你的额外以避免可能的碰撞。如果你不得不在 runSnippet 调用中使用它,modx 可能会尝试运行 WayFinder sn-p。只是一个建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多