Solved

PHP PDO foreach Statement

Posted on 2014-04-24
3
706 Views
Last Modified: 2014-04-25
I have a PHP PDO Query that runs almost 100%, however, I have one issue I'm not sure how to fix:  
I am querying a Wordpress DB and have used JOIN to go through all of the appropriate tables to get the desired results:
The query:
	$user_id = get_current_user_id();


	$sql = "SELECT
    		p1.ID AS ID,
    		pm1.meta_value AS customer_id,
			oi1.order_item_id AS order_item_id,
			om1.meta_value AS first_name,
			om2.meta_value AS last_name,
			om3.meta_value AS mailing_address
			FROM wp_posts p1
			JOIN wp_postmeta pm1 ON (pm1.post_id = p1.ID AND pm1.meta_key = '_customer_user')
			JOIN wp_woocommerce_order_items oi1 ON (oi1.order_id = p1.ID)
			JOIN wp_woocommerce_order_itemmeta om1 ON (om1.order_item_id = oi1.order_item_id AND om1.meta_key = 'Applicant Information - First Name')
			JOIN wp_woocommerce_order_itemmeta om2 ON (om2.order_item_id = oi1.order_item_id AND om2.meta_key = 'Applicant Information - Last Name')
			JOIN wp_woocommerce_order_itemmeta om3 ON (om3.order_item_id = oi1.order_item_id AND om3.meta_key = 'Applicant Information - Mailing Address')
			WHERE p1.post_type = 'shop_order'";
	

	
	$customer = $pdo->prepare($sql);
 
	// EXECUTE QUERY
	try {
    	$customer->execute();
		$result = $customer->fetchAll();

foreach ($result as $row) {
	$cid = $row->customer_id;
	$fn = $row->first_name;
    	$ln = $row->last_name;
	if ($user_id == $cid) {
    		echo $fn.' '.$ln;
	}
}

Open in new window


The code grabs all of the Order details from the DB table wp_woocommerce_order_itemmeta.  Lets say there are three orders in the DB, each for a different product.  I'm going to grab each product name and list it out, however, I don't want to list my name three times, I want to echo it out only once.
Does that make sense?  What other info can I offer you to help me fix this issue?
0
Comment
Question by:rgranlund
  • 2
3 Comments
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 40022082
Try putting DISTINCT in the field list

SELECT DISTINCT ...
0
 
LVL 7

Author Comment

by:rgranlund
ID: 40023464
Can you elaborate?
0
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 40023629
SELECT DISTINCT
    		p1.ID AS ID,
    		pm1.meta_value AS customer_id,
			oi1.order_item_id AS order_item_id,
			om1.meta_value AS first_name,
			om2.meta_value AS last_name,
			om3.meta_value AS mailing_address
			FROM wp_posts p1
			JOIN wp_postmeta pm1 ON (pm1.post_id = p1.ID AND pm1.meta_key = '_customer_user')
			JOIN wp_woocommerce_order_items oi1 ON (oi1.order_id = p1.ID)
			JOIN wp_woocommerce_order_itemmeta om1 ON (om1.order_item_id = oi1.order_item_id AND om1.meta_key = 'Applicant Information - First Name')
			JOIN wp_woocommerce_order_itemmeta om2 ON (om2.order_item_id = oi1.order_item_id AND om2.meta_key = 'Applicant Information - Last Name')
			JOIN wp_woocommerce_order_itemmeta om3 ON (om3.order_item_id = oi1.order_item_id AND om3.meta_key = 'Applicant Information - Mailing Address')
			WHERE p1.post_type = 'shop_order'

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now