1

your Answear helped me but i stuck again. here is what i have done now :

<?PHP
define( 'WP_USE_THEMES', false ); get_header();

global $wpdb;
$sql = "SELECT vehicle_id, manufacturer, series, class FROM $wpdb->rc_vehicles WHERE class = 'Compact'";
$result = $wpdb->get_results($sql, OBJECT);
// See your results printed out (optional)
print_r($result);

// Simple PHP loop
$donations = 0;
foreach ($result as $k => $v) {
  $donations = $donations + ($v->donation * $v->qty);
  echo $result['vehicle_id'];
  echo $result['manufacturer'];
  echo $result['series'];
  echo $result['class'];

}


?>

print_r($result); Display :

Array ( [0] => stdClass Object ( [vehicle_id] => 4 [manufacturer] => RENAULT [series] => Symbol [class] => Compact ) [1] => stdClass Object ( [vehicle_id] => 5 [manufacturer] => RENAULT [series] => CLIO [class] => Compact ) [2] => stdClass Object ( [vehicle_id] => 6 [manufacturer] => RENAULT [series] => KANGOO [class] => Compact ) )

What seems good

but the Echo in the loop dosent show nothing can u help pls ?

2
  • 2
    You shouldn't be using mysql_ functions, use a custom wpdb object to connect and select the db. Remember, the mysql extension is deprecated, your code will fatal error on PHP 5.5+ unless the extension is installed
    – Tom J Nowell
    Nov 17, 2014 at 21:20
  • enable WP_DEBUG in your wp-settings.php file and see if you get any errors. Nov 17, 2014 at 21:44

1 Answer 1

3

I did something like this when making custom reports for WooCommerce since the data was in tables not part of WordPress. My advice to you as well, I can't tell all that you're trying to accomplish here but research WP_Query a lot because most any WordPress query can be accomplished with it and ONLY if it cannot be done should you consider using a $wpdb custom query like what I'm presenting here.

//Need to call the $wpdb global
global $woocommerce, $wpdb;

// Write your own custom query using $wpdb->prefix
$sql = "
  #<!--All Campaign Products and Quantity-->
  SELECT orders.order_item_id, meta_product.meta_value as product, meta_qty.meta_value as qty, pm.meta_value AS donation
  FROM {$wpdb->prefix}woocommerce_order_items AS orders
  LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS meta_product ON (orders.order_item_id = meta_product.order_item_id AND meta_product.meta_key='_product_id')
  LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS meta_qty ON (orders.order_item_id = meta_qty.order_item_id  AND meta_qty.meta_key='_qty')
  LEFT JOIN {$wpdb->prefix}postmeta AS pm ON (meta_product.meta_value = pm.post_id AND pm.meta_key = 'donation_amount')

  WHERE orders.order_item_id IN (".$this->sql_items_from_campaign($cid).")";

// Results can be parsed as OBJECT, OBJECT_K, ARRAY_A, ARRAY_N
$result = $wpdb->get_results($sql, OBJECT);

// See your results printed out (optional)
print_r($result);

// Simple PHP loop
$donations = 0;
foreach ($result as $k => $v) {
  $donations = $donations + ($v->donation * $v->qty);
}

==== UPDATE BASED ON QUESTION CHANGES =====

Couple notes, you should be able to echo $sql and then copy and past the results into the edit sql section in phpmyadmin, this is great for debugging that you're actually getting the correct results. Keep in mind also to use things like {wpdb->prefix} so that your code doesn't break when a different table prefix is used.

Since you were able to print result in the $results array I assume you have the response you were expecting. From there you need to use a simple PHP loop to get the data you want. (this will differ based on if you selected OBJECT, OBJECT_K, ARRAY_A, ARRAY_N for the $results)

// For OBJECT
foreach ($result as $key => $value) {
  echo $value->vehicle_id;
  echo $value->manufacturer;
  echo $value->series
  echo $value->class;
}
5
  • I followed ur instruction it helped me but got stuck to show result can u chek up pls
    – Dolomats
    Nov 19, 2014 at 20:56
  • I updated my answer not sure if you saw that Nov 20, 2014 at 16:23
  • EXEEEEEEEEEEEEEELENT worked like Charm that was nice from u men. just some small question i still dont understand how this loop work "foreach ($result as $key => $value)". Key and value are not called before and what s the logic of this foreach ! Thank you
    – Dolomats
    Nov 20, 2014 at 20:43
  • I would suggest reviewing the PHP documentation for foreach(), it can explain in more detail. When you say foreach( $array_or_object_var AS $new_name_while_in_loop ) So for each iteration in the loop the $key & $value OR just the $value can be referenced. Hope that helps, be sure to accept and vote Nov 20, 2014 at 20:52
  • Well thank for help i think i voted coz still new here and dont really know how it s working :p
    – Dolomats
    Nov 21, 2014 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.