public function getLatestProducts($limit) { $product_data = $this->cache->get('product.latest.' . $this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id') . '.' . $limit); if (!$product_data) { $query = $this->db->query("SELECT *, pd.name AS name, p.image, m.name AS manufacturer, ss.name AS stock, (SELECT AVG(r.rating) FROM " . DB_PREFIX . "review r WHERE p.product_id = r.product_id GROUP BY r.product_id) AS rating FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) LEFT JOIN " . DB_PREFIX . "manufacturer m ON (p.manufacturer_id = m.manufacturer_id) LEFT JOIN " . DB_PREFIX . "stock_status ss ON (p.stock_status_id = ss.stock_status_id) WHERE p.status = '1' AND p.date_available <= NOW() AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND ss.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY p.date_added DESC LIMIT " . (int)$limit); $product_data = $query->rows; $this->cache->set('product.latest.' . $this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id') . '.' . $limit, $product_data); } return $product_data; }
<?php $link = mysql_connect('localhost', 'mydbusername', 'mydbpassword'); if (!$link) { die('Could not connect: ' . mysql_error()); } $output = 'Connected successfully'; $db_selected = mysql_select_db('mydbname', $link); if (!$db_selected) { die ('Can\'t use database : ' . mysql_error()); } $query = 'SELECT * FROM exampletable'; $result = mysql_query($query, $link); while($arecord = mysql_fetch_assoc($result)) { $output .= 'Field 1 : ' . $arecord['firstfield'] . '<br/>'; $output .= 'Field 2 : ' . $arecord['secondfield'] . '<br/>'; $output .= 'Field 5 : ' . $arecord['fifthfield'] . '<br/>'; } mysql_close($link); mysql_free_result($result); return $output;
$dsn = 'mysql:host=localhost;dbname=myDbName;port=3306;charset=utf-8'; $xpdo = new xPDO($dsn,'myDbUserName','myDbPassword');
$query = $xpdo->query("SELECT `name` FROM `opproduct_description`"); if ($query) { while ($row = $query->fetch(PDO::FETCH_ASSOC)) { print_r($row); } } else { echo "YOU SUCK at Programming!"; }
http://rtfm.modx.com/display/xPDO20/Database+Connections+and+xPDO
$query = $xpdo->query("SELECT * FROM `exampletable`"); $result = $query->fetchAll(PDO::FETCH_ASSOC); foreach( $result as $arecord) { $output .= $arecord['firstfield']."<br />"; }
$query = $xpdo->query("SELECT `name` FROM `opproduct_description` LIMIT 6"); if ($query) { $names = $query->fetchAll(PDO::FETCH_COLUMN); print_r($names); } else { echo "YOU SUCK at Programming!"; }
Array ( [0] => ProductName1 [1] => ProductName2 [2] => ProductName3 [3] => ProductName4 [4] => ProductName5 [5] => ProductName6 )
$c = array ( /* c for criteria */ 'name' = $productName'; ); $product = $modx->getObject('modProduct', $c); return $modx->getChunk('productTpl', $product->toArray() );
$c = array { 'category' = $category; } $products = $modx->getCollection('modProduct', $c); foreach ($products as $product) { $output . = $modx-getChunk('productTpl', $product->toArray()); } return $output;
$query = $xpdo->newQuery('Box'); $query->innerJoin('Owner','User'); // the Owner is actually a User object, defined as Owner in the relationship alias $query->where(array( 'Owner.name:LIKE' => '%a%', 'Box.width:>=' => 10, 'Box.height:!=' => 2, 'Box.color:IN' => array('red','green','blue'), )); $query->sortby('Box.name','ASC'); $query->sortby('Box.height','DESC'); $query->limit(4); $boxes = $xpdo->getCollection('Box',$query);
$query = $xpdo->query("SELECT opproduct.image, opproduct.price, opproduct_description.name FROM opproduct, opproduct_description WHERE opproduct.product_id = opproduct_description.product_id ORDER BY opproduct.date_added DESC LIMIT 6");
<?php $path = MODX_CORE_PATH . 'components/ocLatestProducts/'; $result = $modx->addPackage('ocLatestProducts',$path . 'model/','opcart_'); if (! $result) { return 'failed to add package'; } $query = $modx->newQuery('Product'); $query->innerJoin('ProductDescription', 'ProductDescription'); $query->where(array( 'ProductDescription.product_id' => 'Product.product_id' )); $query->sortby('Product.date_added', 'DESC'); $query->limit(6); $products = $modx->getCollection('Product',$query); $output = '<p>Total: '. count($products) . '</p>'; foreach($products as $product) { $output .= '<p>Name: ' . $product->get('name'); $output .= '<p>Price: ' . $product->get('price'); $output .= '<br />Image: ' . $product->get('image') . '<br /></p>'; } return $output;