We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    Good thought Bruno. I assumed that the OP was running a snippet by visiting a resource. Maybe not.

    In that case: http://bobsguides.com/blog.html/2013/07/13/using-modx-outside-of-modx/.

      Did I help you? Buy me a beer
      Get my Book: MODX:The Official Guide
      MODX info for everyone: http://bobsguides.com/modx.html
      My MODX Extras
      Bob's Guides is now hosted at A2 MODX Hosting
      • 18373 ☆ A M B ☆
      • 3,141 Posts
      As I said, $modx->query returns a PDOStatement object. That's not an associative array you can loop over like that.

      Your code will need to look like this:

      $stmt = $modx->query(...);
      while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
          echo $row['field1'].' '.$row['field2']; //etc...
      }


        Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

        Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
        • 44866
        • 50 Posts
        Hi Guys,
        We have just had a long weekend here in AU. (Whoo Hoo!)
        I'll go over these responses today and have a go at changing my code.
        I'll let you know what I come up with.
        I'll post my snipet and the included php file so you won't need to make any assumptions.

        Thanks
        David
          • 44866
          • 50 Posts
          By The Way,
          I forgot to mention it is a custom table not a MODX table.
          David
            • 44866
            • 50 Posts
            Hi BobRay,
            I am running my code from a Snipet. The snipet is called from the cron manager.
            I have just used existing snipet's as an example so there is probably stuff in there that I'm not using and don't need.

            <?php
            $html= (int) $modx->getOption('html', $scriptProperties, 0);
            $twmg_base = $modx->getOption('core_path').'components/twmg/';
            include_once($twmg_base.'model/property/propertypurge.class.php');
            
            $property = new propertypurge();
            $_config = array(
            	'parent' => 63,
            	'template' => 20,
            	'move_image' => true,
            	'xml_folder' => MODX_BASE_PATH.'xml/',
            	'xml_success_folder' => MODX_BASE_PATH.'xml/processed/',
            	'image_folder' => MODX_BASE_PATH.'assets/images/properties/'
            );
            $property->initiliaze($_config);
            $property->run();
            $property->printMessage($html);
              • 44866
              • 50 Posts
              Hello Again,
              This is the include file propertypurge.class.php again based on an existing file.

              Currently this code does not execute.
              The problem is with this line. Hopefully I can figure out what the problem is.
              while($row = $sql->fetch(PDO::FETCH_ASSOC))



              <?php
              // ==========================================================================
              // Update Log
              // Date		Init	Ver	Description
              // --------------------------------------------------------------------------
              class propertyPurge {
              	var $image_folder 	= 'assets/images/properties/';
              	// var $debug 		= false;
              	var $debug		= true;
              
              	var $messages		= array();		
              
              	function initiliaze($config=array()) 
              	{
              
              		foreach ($config as $key=>$value) 
              		{
              			$this->$key = $value;
              		}
              	}
              
              	function run() 
              	{
              		if ($this->debug){ $this->logMessage('Debug: function run()', "");}
              		$output = '';
              		$row = '';
              		$sql = "SELECT * FROM vjraycom_db.vj_site_tmplvar_contentvalues";
              		
              		if ($this->debug){ $this->logMessage('Debug: function run() Sql=', $sql);}
              		
              		while($row = $sql->fetch(PDO::FETCH_ASSOC))
              		{
              			$ouptut = $row['id']."\t".$row['tmplvarid'];
              			if ($this->debug){ $this->logMessage('Debug: function run() Output=', $output);}
              		}
              
              		if ($this->debug){ $this->logMessage('Debug: End function run()', "");}
              	}
              
              
              	function logMessage($item, $message) 
              	{
              		$this->messages[] = array( 
              			'item' => $item,
              			'message' => $message
              		);
              	}
              
              	function printMessage($html=true) 
              	{
              		if ($this->debug){ $this->logMessage('Debug: function printMessage()', "");}
              		$output = '';
              		foreach ($this->messages as $msg) {
              			if($html)
              				$output .= '<p>'.$msg['item'].' '.$msg['message'].'</p>';
              			else
              				$output .= $msg['item'].' '.$msg['message']."\n";
              		}
              		echo $output;
              		if ($this->debug){ $this->logMessage('Debug: End function printMessage()', "");}
              	}
              }
                • 44866
                • 50 Posts
                OK Here is an update
                I found a typo in the code above. I forgot the ->query bit.
                However I still couldn't get it to run. So I changed to the example code from RTFM.
                http://rtfm.modx.com/xpdo/2.x/class-reference/xpdo/xpdo.query
                This line still won't execute.
                $result = $modx->query("SELECT * FROM vjray_modx.modx_users WHERE id=1");

                I'm obviously missing something. Any ideas?

                I also ran the sql in MySQL Desktop and it works fine. Returns one row as expected.

                function run() 
                {
                	if ($this->debug){ $this->logMessage('Debug: function run()', "");}
                	$output = '';
                	$row = '';
                	$result = '';
                	$result = $modx->query("SELECT * FROM vjray_modx.modx_users WHERE id=1");
                	if (!is_object($result)) 
                	{
                		if ($this->debug){ $this->logMessage('Debug: function run()SQL Result= Row', "No Result");}
                	}
                	else 
                	{
                   		$row = $result->fetch(PDO::FETCH_ASSOC);
                   		if ($this->debug){ $this->logMessage('Debug: function run()SQL Result Row=', $row);}
                	}
                	if ($this->debug){ $this->logMessage('Debug: End function run()', "");}
                }
                  • 4172
                  • 5,888 Posts
                  you didn't reference the modx-object in your class.

                  You could simply set

                  global $modx;


                  at the beginning of all of your functions

                  Then $modx is avaliable in the function.

                  Or send the modx-object, when you instatiate your class and set modx as reference in the _construct - method of your class.
                  This is the usual way.

                  Then you can use

                  $this->modx
                  in all of your functions
                    -------------------------------

                    you can buy me a beer, if you like MIGX

                    http://webcmsolutions.de/migx.html

                    Thanks!
                    • 44866
                    • 50 Posts
                    Hi Bruno,
                    Now that you mention it I do remember seeing "global $modx" in the code.
                    I must have accidentally removed it (arggg)

                    I have put it back and my code works again. Now I can move forward with my more complex query.

                    PHP syntax is still foreign to me. It is coming together slowly in my head.

                    Thanks for pointing out my problem!
                    David