English 中文(简体)
MODX - Making multiple snippet calls on a page returning differeny output
原标题:
  • 时间:2009-12-07 21:35:31
  •  标签:
  • modx

I ve created a snippet that pulls data from a databse table and displays it in tabular format. The snippet takes an id as parameter, and this is added to the sql query.

My problem is that if I ve got more than 1 snippet call (sometimes need the tabular data for different id s displayed on a page) on the same page, all table data is the same as the last database call that s been made by the last snippet.

What do I need to do to kinda not cache the snippet database calls and have them all display their own content?

I ve tried setting the page to no cache-able. Also used the [! !] brackets for the snippet calls, and even used the function_exists() method, but none of them helped.

Please can someone help me?

thanks

问题回答

Try this at the end of the snippet:

mysql_connect( host ,  user ,  pass );
mysql_select_db( db_name );

You need to specify the connection parameters ofcourse.

It would help to answer if you can post your snippet. I do this with multiple calls on the page without issue, so there is either something wrong inside the snippet, or you need to output to unique placeholder names.

You have encountered a glitch of ModX, and it took me a long time to solve. ModX does a lot of caching by using hashing and apparently, when multiple connections are made from within one page divided over multiple snippets, this erratic behaviour can be seen. This is most likely very unwanted behaviour, it can be solved easily but gives you terrible headache otherways.

One sympton is that $modx->getObject($classname, $id)returns null (often).

The solution is very simple: either use a static class with a single db instance, or use $modx->setPlaceholder($instance, $tag);, or a combination.

My solution has been:

class dt__xpdo {

  private function __construct() {}

  public function __destruct() {
    $this->close();
  }

  static public function db($modx = null) {

    if ($modx->getPlaceholder( dt_xpdo ) ==   ) {
      $dt_user   =  xxxxxxxxx ;
      $dt_pw     =  xxxxxxxxx ;
      $dt_host   =  localhost ;
      $dt_dbname =  xxxxxxxxx ;
      $dt_port   =  3306 ;

      $dt_dsn = "mysql:host=$dt_host;dbname=$dt_dbname;port=$dt_port;charset=utf8";

      $dt_xpdo = new xPDO($dt_dsn, $dt_user, $dt_pw);

      $dt_xpdo->setPackage( mymodel , MODX_CORE_PATH. components/mymodel/ . model/ ,   );

      //$modx->log(modX::LOG_LEVEL_DEBUG,  mymodel.config.php );
      //$modx->log(modX::LOG_LEVEL_DEBUG,  Could not addPackage for mymodel! );

      $modx->setPlaceholder( dt_xpdo , $dt_xpdo);
    }

    return $modx->getPlaceholder( dt_xpdo );
  }
}

Now you can use in your code:

require_once above.php ;

and use something like

$xpdo = dt__xpdo::db($modx);

and continue flawlessly!





相关问题
predefine template for new resource/document

I would like to set the new child documents to default to a set template that is different from the parent. I ve looked through managermanager, but looks like it is unable to do this. mm_inherit can ...

Modx css issue not pulling in styles on the form

I m editing a site using Modx cms. I m trying to add a contact form to the contact page. I already have one in the sidebar specified in a template. The code on the contact page form points to the same ...

PHP MODx mysql Parse error? Where do I change the settings?

Hi I m using MODx on a site, but something has gone wrong with the database. Any idea how I can change the PHP to point to my database? « MODx Parse Error » MODx encountered the following error while ...

Print a table in modx to send info for mail by eForm

My question is, how can I create a table with dynamic fields (I will explain this later), and send them values by mail with eform? In the table, on the left, I want to print all the document children ...

Simple PHP CMS as an alternative to hacking Wordpress

For creating common user modifiable site I ve been forcing Wordpress to do the work of a CMS. It s worked and the back-end is purdy but it s just too hacky for my tastes. So I d like a simple CMS that ...

Modx assign default date to template variable

I have a template variable of input type date but I d like to have a default value of the current date at the time the document was first saved. I ve noticed that using the date formatter widget ...

热门标签