Solved

Use a Where Clause in a Foreach loop

Posted on 2016-10-06
14
52 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 43

Expert Comment

by:zephyr_hex (Megan)
ID: 41832684
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 83

Expert Comment

by:Dave Baldwin
ID: 41832691
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 22

Expert Comment

by:Kim Walker
ID: 41832697
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 55

Expert Comment

by:Julian Hansen
ID: 41832774
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 109

Expert Comment

by:Ray Paseur
ID: 41832775
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
ID: 41833647
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 109

Expert Comment

by:Ray Paseur
ID: 41833678
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
 
LVL 2

Author Comment

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

Steve
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41833722
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 55

Expert Comment

by:Julian Hansen
ID: 41833739
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
ID: 41833751
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 109

Expert Comment

by:Ray Paseur
ID: 41833773
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 55

Expert Comment

by:Julian Hansen
ID: 41833782
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
ID: 41834157
Thanks to everyone for helping me out.  The experts on experts-exchange come through again!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Number of hours between date in DB and now 8 21
php help 34 58
Displaying text in text field when clicking on ajax search result 10 38
Link failure 16 31
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

791 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