Solved

PHP PDO foreach Statement

Posted on 2014-04-24
3
735 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 56

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 56

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
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.

680 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