[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 68
  • Last Modified:

Use a Where Clause in a Foreach loop

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
stkoontz
Asked:
stkoontz
  • 4
  • 4
  • 3
  • +3
1 Solution
 
zephyr_hex (Megan)DeveloperCommented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
 
Kim WalkerWeb Programmer/TechnicianCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Julian HansenCommented:
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
 
Ray PaseurCommented:
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
 
stkoontzAuthor Commented:
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
 
Ray PaseurCommented:
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
 
stkoontzAuthor Commented:
Nice suggestion, Ray.  I made the change in the code. Thanks!

Steve
0
 
Ray PaseurCommented:
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
 
Julian HansenCommented:
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
 
stkoontzAuthor Commented:
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
 
Ray PaseurCommented:
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
 
Julian HansenCommented:
As Ray said they are the same - Ray was first so he should get the credit on this.
0
 
stkoontzAuthor Commented:
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.

  • 4
  • 4
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now