Solved

Use a Where Clause in a Foreach loop

Posted on 2016-10-06
14
40 Views
Last Modified: 2016-10-07
I have an array with values I want to display.  But the values need filtered by id_family_member.  Can I use a WHERE clause in the following foreach loop?  If not, any other ideas on how to accomplish this?

   foreach ($array_family_registrations as $sub_array) {
			$id_event=$sub_array['id_event'];
                        $id_church = $sub_array['id_church'];
                        $health_form = $sub_array['health_form'];
                        $payment = $sub_array['payment'];
                        $custom_fields = $sub_array['custom_fields'];
                        $reg_complete = $sub_array['reg_complete'];

Open in new window


Here's part of the array.

Array
(
    [0] => Array
        (
            [id_family_member] => firstName3
            [id_event] => uh50761
            [id_church] => PA100 
            [health_form] => Yes
            [payment] => Yes
            [custom_fields] => Yes
            [reg_complete] => No
            [event_name] => Baptist Church of Western PA coming to UH
            [id_family] => smith254545
        )

    [1] => Array
        (
            [id_family_member] => firstName123
            [id_event] => mo60557
            [id_church] => PA100 
            [health_form] => Yes
            [payment] => Yes
            [custom_fields] => Yes
            [reg_complete] => No
            [event_name] => Momentum 2017
            [id_family] => smith254545
        )

Open in new window


Thanks in advance for help,

Steve
0
Comment
Question by:stkoontz
  • 4
  • 4
  • 3
  • +3
14 Comments
 
LVL 42

Expert Comment

by:zephyr_hex
Comment Utility
It's not entirely clear what you mean... I'll assume that you only want results when the family member id equals a particular value.

$filterVal = 'some value';
foreach ($array_family_registrations as $sub_array) {
     if($sub_array['id_family_member'] === $filterVal) {
        $id_event=$sub_array['id_event'];
        //and so on...
     }
}

Open in new window

0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
No, you have to use an 'if' statement to check the values.
if($sub_array['iid_family_member'] == $desiredvalue) then do something
else continue; //skips to next foreach

Open in new window

0
 
LVL 21

Expert Comment

by:Kim Walker
Comment Utility
Just include an if statement.
$filter = array(
    'firstname123',
    'firstname321'
)
foreach ($array_family_registrations as $sub_array) {
    if (array_search($sub_array['id_family_member'],$filter) === false) {
        $id_event=$sub_array['id_event'];
        $id_church = $sub_array['id_church'];
        $health_form = $sub_array['health_form'];
        $payment = $sub_array['payment'];
        $custom_fields = $sub_array['custom_fields'];
        $reg_complete = $sub_array['reg_complete'];
    }
}

Open in new window

I did not have a chance to test this. Let me know if there are errors.
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
Can you show us what your output would look like after a successful execution of the loop assuming it can do what you want - in other words the desired result - not what your code currently does.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Looks like this probably came from a database query, right?  If so you might use the WHERE clause in the SQL query to constrain the results set.  Then everything you want will be in the results set, and nothing else.
1
 
LVL 2

Author Comment

by:stkoontz
Comment Utility
Thanks for all 5 of you coming on to help me!  I'll try to explain more.

@zephyr_hex Yes, only results when id_family_member in the array matches id_family_member in the table

@Ray Paseur I could use the WHERE clause in the SQL query and not use an array, but that seems like it would take a lot more code to work out.

The if statement might be what I need.  

Go to http://www.testregister.cenational.org/family/family_dashboard.php and you'll see two arrays at the top.  

array_family_members = Family members
array_family_registrations = Events family members have registered for

Scroll down and you'll see each family member.  Under the family members, the code needs to show the event that each is registered for.  Example: firstName123 should only say "Momentum 2017" under it.  Not "Feb 8, 2017 Robert Lehman - Counseling" 4 times.

I tried adding the if statement that @Kim Walker suggested, changing the 'false' to 'true', but then no events showed up.  (when I left it 'false' I got the same output as when there was no if statement)

                    foreach ($array_family_registrations as $sub_array) {
						 if (array_search($sub_array['id_family_member'],$array_family_members) === false) {

						$id_family_member_event=$sub_array['id_family_member'];
						$id_event=$sub_array['id_event'];
                        $id_church = $sub_array['id_church'];
                        $health_form = $sub_array['health_form'];
                        $payment = $sub_array['payment'];
                        $custom_fields = $sub_array['custom_fields'];
                        $reg_complete = $sub_array['reg_complete'];

		                  echo $event_name . "<br />";
						 }
                    }

Open in new window


Thanks again for all the help.

Steve
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Hey, Steve.  Going forward, please use var_export() to print out the data you want us to work with.  This will give us valid PHP representations of the variables, and we can do copy/paste instead of having to reformat all of the print_r() output.  Thanks!
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 2

Author Comment

by:stkoontz
Comment Utility
Nice suggestion, Ray.  I made the change in the code. Thanks!

Steve
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
Please see: https://iconoun.com/demo/temp_stkoontz.php

Moving parts start at line 100.
<?php // demo/temp_stkoontz.php
/**
 * https://www.experts-exchange.com/questions/28974894/Use-a-Where-Clause-in-a-Foreach-loop.html
 * http://www.testregister.cenational.org/family/family_dashboard.php
 */
ini_set('display_errors', TRUE);
error_reporting(E_ALL);
echo '<pre>';

// TEST DATA FROM THE POST AT E-E
$array_family_members = Array
(
    '0' => Array
        (
            'id_family_member' => 'firstName123',
            'id_family' => 'smith254545',
            'fm_lname' => 'firstname1',
            'fm_fname' => 'LastName',
        ),

    '1' => Array
        (
            'id_family_member' => 'firstName29',
            'id_family' => 'smith254545',
            'fm_lname' => 'firstname2',
            'fm_fname' => 'LastName',
        ),

    '2' => Array
        (
            'id_family_member' => 'firstName3',
            'id_family' => 'smith254545',
            'fm_lname' => 'firstname3',
            'fm_fname' => 'LastName',
        ),
)
;

$array_family_registrations = Array
(
    '0' => Array
        (
            'id_family_member' => 'firstName3',
            'id_event' => 'uh50761',
            'id_church' => 'PA100 ',
            'health_form' => 'Yes',
            'payment' => 'Yes',
            'custom_fields' => 'Yes',
            'reg_complete' => 'No',
            'event_name' => 'Baptist Church of Western PA coming to UH',
            'id_family' => 'smith254545',
        ),

    '1' => Array
        (
            'id_family_member' => 'firstName123',
            'id_event' => 'mo60557',
            'id_church' => 'PA100 ',
            'health_form' => 'Yes',
            'payment' => 'Yes',
            'custom_fields' => 'Yes',
            'reg_complete' => 'No',
            'event_name' => 'Momentum 2017',
            'id_family' => 'smith254545',
        ),

    '2' => Array
        (
            'id_family_member' => 'firstName29',
            'id_event' => 'mo60557',
            'id_church' => 'PA100 ',
            'health_form' => 'Yes',
            'payment' => 'Yes',
            'custom_fields' => 'No',
            'reg_complete' => 'No',
            'event_name' => 'Momentum 2017',
            'id_family' => 'smith254545',
        ),

    '3' => Array
        (
            'id_family_member' => 'firstName29',
            'id_event' => 'pl2202',
            'id_church' => 'PA100 ',
            'health_form' => 'No',
            'payment' => 'Yes',
            'custom_fields' => '',
            'reg_complete' => 'No',
            'event_name' => 'Feb 8, 2017 Robert Lehman - Counseling',
            'id_family' => 'smith254545',
        ),
)
;

// DOES THE DATA LOOK RIGHT?  YES.
// var_export($array_family_members);
// var_export($array_family_registrations);


// ITERATE OVER THE family_members TO FIND THE family_registrations
foreach ($array_family_members as $fm)
{
    $fmid = $fm['id_family_member'];
    echo PHP_EOL . $fmid;

    foreach ($array_family_registrations as $fr)
    {
        if ($fmid == $fr['id_family_member'])
        {
            echo PHP_EOL . $fr['event_name'];
        }
    }
    echo PHP_EOL;
}

Open in new window

Outputs:
firstName123
Momentum 2017

firstName29
Momentum 2017
Feb 8, 2017 Robert Lehman - Counseling

firstName3
Baptist Church of Western PA coming to UH

Open in new window

0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
Do you mean this
<?php
$array_family_members = array (
  0 => 
  array (
    'id_family_member' => 'firstName123',
    'id_family' => 'smith254545',
    'fm_lname' => 'firstname1',
    'fm_fname' => 'LastName',
  ),
  1 => 
  array (
    'id_family_member' => 'firstName29',
    'id_family' => 'smith254545',
    'fm_lname' => 'firstname2',
    'fm_fname' => 'LastName',
  ),
  2 => 
  array (
    'id_family_member' => 'firstName3',
    'id_family' => 'smith254545',
    'fm_lname' => 'firstname3',
    'fm_fname' => 'LastName',
  ),
);

$array_family_registrations = array (
  0 => 
  array (
    'id_family_member' => 'firstName3',
    'id_event' => 'uh50761',
    'id_church' => 'PA100 ',
    'health_form' => 'Yes',
    'payment' => 'Yes',
    'custom_fields' => 'Yes',
    'reg_complete' => 'No',
    'event_name' => 'Baptist Church of Western PA coming to UH',
    'id_family' => 'smith254545',
  ),
  1 => 
  array (
    'id_family_member' => 'firstName123',
    'id_event' => 'mo60557',
    'id_church' => 'PA100 ',
    'health_form' => 'Yes',
    'payment' => 'Yes',
    'custom_fields' => 'Yes',
    'reg_complete' => 'No',
    'event_name' => 'Momentum 2017',
    'id_family' => 'smith254545',
  ),
  2 => 
  array (
    'id_family_member' => 'firstName29',
    'id_event' => 'mo60557',
    'id_church' => 'PA100 ',
    'health_form' => 'Yes',
    'payment' => 'Yes',
    'custom_fields' => 'No',
    'reg_complete' => 'No',
    'event_name' => 'Momentum 2017',
    'id_family' => 'smith254545',
  ),
  3 => 
  array (
    'id_family_member' => 'firstName29',
    'id_event' => 'pl2202',
    'id_church' => 'PA100 ',
    'health_form' => 'No',
    'payment' => 'Yes',
    'custom_fields' => NULL,
    'reg_complete' => 'No',
    'event_name' => 'Feb 8, 2017 Robert Lehman - Counseling',
    'id_family' => 'smith254545',
  ),
);

foreach($array_family_members as $person) {
  echo "First Name: {$person['fm_fname']} Last Name: {$person['fm_lname']}<br/>";
  echo "Events<br/>";
  foreach($array_family_registrations as $reg) {
    if ($reg['id_family_member'] == $person['id_family_member']) {
      echo "  => {$reg['event_name']}<br/>";
    }
  }
}

Open in new window

Output
First Name: LastName Last Name: firstname1
Events
=> Momentum 2017
First Name: LastName Last Name: firstname2
Events
=> Momentum 2017
=> Feb 8, 2017 Robert Lehman - Counseling
First Name: LastName Last Name: firstname3
Events
=> Baptist Church of Western PA coming to UH

Open in new window

0
 
LVL 2

Author Comment

by:stkoontz
Comment Utility
Julian,

Yes, both you and Ray understand what I'm looking for.  I just need need to figure out which implementation works best into the full page.

Thanks!

Steve
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Steve:  They are the same design patterns and implementations.  You can tailor either one to display exactly what you need to see.  What you're doing here in PHP is more-or-less the equivalent of a JOIN clause in MySQL.  You can filter and constrain the output by simulating a WHERE clause, too.

Best of luck with your project, ~Ray
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
As Ray said they are the same - Ray was first so he should get the credit on this.
0
 
LVL 2

Author Closing Comment

by:stkoontz
Comment Utility
Thanks to everyone for helping me out.  The experts on experts-exchange come through again!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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…
The viewer will learn how to dynamically set the form action using jQuery.
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.

763 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

11 Experts available now in Live!

Get 1:1 Help Now