Zen Cart Logo
If this is your first visit, check our documentation. To post, register or login.
Forums / General Questions / General Questions / getting list of products from last order

getting list of products from last order

Started by fakedecoy on 16 Oct 2015, 4:23 PM · Views: 798

Results 1 to 4 of 4
16 Oct 2015, 4:23 PM
#1
fakedecoy avatar

fakedecoy

Zen Follower

Join Date:
Jul 2006
Posts:
309
Plugin Contributions:
0

Post #1 by fakedecoy

I'm trying to get a list of products ordered on the checkout_success page so I can add some Facebook share buttons for each one. I can do the rest if I can manage to get a list of the products into an array.

I have this in includes/languages/english/html_includes/mytemplate/define_checkout_success.php

$orders_query = "SELECT * FROM " . TABLE_ORDERS . "
                 WHERE customers_id = :customersID
                 ORDER BY date_purchased DESC LIMIT 1";
$orders_query = $db->bindVars($orders_query, ':customersID', $_SESSION['customer_id'], 'integer');
$orders = $db->Execute($orders_query);				 

$orders_id = $orders->fields['orders_id'];
$products_query = "SELECT products_id, products_name
                     FROM " . TABLE_ORDERS_PRODUCTS . "
                     WHERE orders_id = '$orders_id'
                     ORDER BY products_name";	 

$products = $db->Execute($products_query);

echo '<pre>';
var_dump($products);
echo '</pre>';

I'm getting the below output. It's only returning one product, when the order has two products. Is this the way the db->Execute function works, limiting the results to 1? Is there another way I can get all results?

object(queryFactoryResult)#23 (5) { ["is_cached"]=> bool(false) ["resource"]=> resource(530) of type (mysql result) ["cursor"]=> int(0) ["EOF"]=> bool(false) ["fields"]=> array(2) { ["products_id"]=> string(4) "1994" ["products_name"]=> string(61) "Widget Product Name" } }

Adamant Barbell - Home & commercial gym equipment - Since 2007
www.adamantbarbell.com

16 Oct 2015, 5:25 PM
#2
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Post #2 by mc12345678

Yes basically. The normal process from that point is to iterate until $products->EOF is true. I have also seen somewhere that it may be possible (future?) To foreach on $products so that do not have to test for the EOF and do not have to remember to MoveNext() on $products.

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

18 Oct 2015, 12:04 AM
#3
fakedecoy avatar

fakedecoy

Zen Follower

Join Date:
Jul 2006
Posts:
309
Plugin Contributions:
0

Post #3 by fakedecoy

Thanks, I used Movenext and that's the trick.

Adamant Barbell - Home & commercial gym equipment - Since 2007
www.adamantbarbell.com

18 Oct 2015, 1:43 AM
#4
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Post #4 by mc12345678

fakeDecoy:

Thanks, I used Movenext and that's the trick.

$products->MoveNext() will give your second product, but hopefully you employed something like:

while(!$products->EOF) {
$products_array[] = $products->fields;
$products->MoveNext();
}

Where $products_array is previously declared as an array.

Then to tell if there are any values stored in the array can simply check the sizeof($products_array) to see if it is >0 which would mean that a value was assigned.

The important thing to note is that in the OP it was indicated that the test was going to only involve two products in the list. Then without further clarification of what was being done/used, someone not too familiar with the code methodology might think that using MoveNext() one time would be sufficient for all cases and that it wouldn't be a problem if only one item was in the order.

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...