<![CDATA[ Retrieving items from a database table and adding them to an existing session array - My Forums]]> https://forums.modx.com/thread/?thread=84113 <![CDATA[Retrieving items from a database table and adding them to an existing session array]]> https://forums.modx.com/thread/84113/retrieving-items-from-a-database-table-and-adding-them-to-an-existing-session-array#dis-post-464280 In the code block below, between lines 32 to 44, I am trying to retrieve the 4 items from a row in a table in the database and add them to the session array created at lines 22 to 28. Without the added code (32 to 44) the file works fine, however with the code added nothing is returned and the cart is empty.
Any help to show me where my error is would be appreciated.

// [[orderDetailsInToSESSION]]

if ( empty($_GET['customer']) ) return "no customer account specified";
else {
    // sanitize and validate the input type to make sure it's what you are expecting;
    $customer = $modx->db->escape($_GET['customer']);
    $orderId = $modx->db->escape($_GET['orderId']);
    // get the details from the orderId
    $db_query = $modx->db->select("productId, quantity", $modx->getFullTableName('customer_order_details'), "orderId='" . $orderId . "'");
    if ($modx->db->getRecordCount($db_query) > 0) {
        while ($row = $modx->db->getRow($db_query)) {
            // get the current prices and names of the products previously ordered
            $document_tvs = $modx->getTemplateVars(array("pagetitle", "price", "specialPrice"), "name", $row['productId']);

         // to put the returned array into a simpler/easier to use array use the following
               if (is_array($document_tvs)) {  
               foreach( $document_tvs as $document_TV) {
                $docTVArray[$document_TV['name']] = $document_TV['value'];
               }
              }

          // put these details into the session
         $_SESSION['shoppingCartContents'][] = Array (
              'productId' => $row['productId'],
             'productTitle' => $docTVArray['pagetitle'],
              'price' => ( !empty( $docTVArray['specialPrice']) ) ?  $docTVArray['specialPrice'] :  $docTVArray['price'],
              'quantity' => $row['quantity']
          );
        }
	}
		
	// if the productId is 219 get the 4 items in the row and add to session array
		if ($_SESSION['shoppingCartContents']['productId'] == 219) {
		$db_query = $modx->db->SELECT("hummusOne, hummusTwo, mayo, jam", $modx->getFullTableName('customer_order_giftpack_details'), "orderId='" . $orderId . "'");
		if ($modx->db->getRecordCount($db_query) > 0) {
                  while ($row = $modx->db->getRow($db_query)) {
		    $_SESSION['shoppingCartContents']['hummusOne'] => $row['hummusOne'],
		    $_SESSION['shoppingCartContents']['hummusTwo'] => $row['hummusTwo'],
		    $_SESSION['shoppingCartContents']['mayo'] => $row['mayo'],
		    $_SESSION['shoppingCartContents']['jam'] => $row['jam']
             );
	   }
        }
    } // end of product219 if
 }
]]>
Twobears Apr 25, 2013, 12:03 AM https://forums.modx.com/thread/84113/retrieving-items-from-a-database-table-and-adding-them-to-an-existing-session-array#dis-post-464280
<![CDATA[Re: Retrieving items from a database table and adding them to an existing session array]]> https://forums.modx.com/thread/84113/retrieving-items-from-a-database-table-and-adding-them-to-an-existing-session-array#dis-post-464411

If that is the entire snippet here is a slightly condensed version I put together (untested) that might work if you're interested:
// [[orderDetailsInToSESSION]]

if ( empty( $_GET['customer'] ) ){

    return "no customer account specified";

}else{

    // sanitize and validate the input type to make sure it's what you are expecting;
    $customer = $modx->db->escape( $_GET['customer'] );

    $orderId = $modx->db->escape( $_GET['orderId'] );

    // get the details from the orderId
    $order_details = $modx->db->getRow( $modx->db->select( 'productId, quantity', $modx->getFullTableName( 'customer_order_details' ), 'orderId="' . $orderId .'"' ) );

    if( $order_details ){
    
        // get the current prices and names of the products previously ordered
        $doc_tvs = $modx->getTemplateVars( array( 'pagetitle', 'price', 'specialPrice' ), 'name', $order_details['productId'] );

        // put these details into the session
        $_SESSION['shoppingCartContents'][] = Array (
                'productId'     => $order_details['productId'],
                'productTitle'  => $doc_tvs['pagetitle'],
                'price'         => ( !empty( $doc_tvs['specialPrice'] ) ) ?  $doc_tvs['specialPrice'] :  $doc_tvs['price'],
                'quantity'      => $order_details['quantity']
                );

        // if the productId is 219 get the 4 items in the row and add to session array
        if( $order_details['productId'] == 219 ){

            $items = $modx->db->getRow( $modx->db->select( 'hummusOne, hummusTwo, mayo, jam', $modx->getFullTableName( 'customer_order_giftpack_details' ), 'orderId="' . $orderId .'"' ) );

            if( $items ){

                $_SESSION['shoppingCartContents'][0]['hummusOne'] = $items['hummusOne'];
                $_SESSION['shoppingCartContents'][0]['hummusTwo'] = $items['hummusTwo'];
                $_SESSION['shoppingCartContents'][0]['mayo']      = $items['mayo'];
                $_SESSION['shoppingCartContents'][0]['jam']       = $items['jam'];

            }
        }
    }
}
]]>
breezer Apr 25, 2013, 11:41 PM https://forums.modx.com/thread/84113/retrieving-items-from-a-database-table-and-adding-them-to-an-existing-session-array#dis-post-464411
<![CDATA[Re: Retrieving items from a database table and adding them to an existing session array]]> https://forums.modx.com/thread/84113/retrieving-items-from-a-database-table-and-adding-them-to-an-existing-session-array#dis-post-464304 sottwell Apr 25, 2013, 03:07 AM https://forums.modx.com/thread/84113/retrieving-items-from-a-database-table-and-adding-them-to-an-existing-session-array#dis-post-464304 <![CDATA[Re: Retrieving items from a database table and adding them to an existing session array]]> https://forums.modx.com/thread/84113/retrieving-items-from-a-database-table-and-adding-them-to-an-existing-session-array#dis-post-464298 Thanks for your help with this. Used your suggestion with a slight modification. Success, many thanks.
// if the productId is 219 get the 4 items in the row and add to session array
if ( $_SESSION['shoppingCartContents'][0]['productId'] == 219 ) {
 
    $items = $modx->db->getRow( $modx->db->select( 'hummusOne, hummusTwo, mayo, jam', $modx->getFullTableName( 'customer_order_giftpack_details' ), 'orderId="' . $orderId .'"' ) );
 
    if( $items ){
     
        $_SESSION['shoppingCartContents'][0]['hummusOne'] = $items['hummusOne'];
        $_SESSION['shoppingCartContents'][0]['hummusTwo'] = $items['hummusTwo'];
        $_SESSION['shoppingCartContents'][0]['mayo']      = $items['mayo'];
        $_SESSION['shoppingCartContents'][0]['jam']       = $items['jam'];
     
    }
 
  } // end of product219 if
]]>
Twobears Apr 25, 2013, 02:07 AM https://forums.modx.com/thread/84113/retrieving-items-from-a-database-table-and-adding-them-to-an-existing-session-array#dis-post-464298
<![CDATA[Re: Retrieving items from a database table and adding them to an existing session array (Best Answer)]]> https://forums.modx.com/thread/84113/retrieving-items-from-a-database-table-and-adding-them-to-an-existing-session-array#dis-post-464292
    // if the productId is 219 get the 4 items in the row and add to session array
    if ( $_SESSION['shoppingCartContents']['productId'] == 219 ) {

        $items = $modx->db->getRow( $modx->db->select( 'hummusOne, hummusTwo, mayo, jam', $modx->getFullTableName( 'customer_order_giftpack_details' ), 'orderId="' . $orderId .'"' ) );

        if( $items ){
        
            $_SESSION['shoppingCartContents']['hummusOne'] = $items['hummusOne'];
            $_SESSION['shoppingCartContents']['hummusTwo'] = $items['hummusTwo'];
            $_SESSION['shoppingCartContents']['mayo']      = $items['mayo'];
            $_SESSION['shoppingCartContents']['jam']       = $items['jam'];
        
        }

    } // end of product219 if
]]>
breezer Apr 25, 2013, 12:43 AM https://forums.modx.com/thread/84113/retrieving-items-from-a-database-table-and-adding-them-to-an-existing-session-array#dis-post-464292