Search code examples
phpsqlmodxmodx-revolution

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.

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

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

Solution

  • 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

    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