Help with Array
11 posts by 2 authors in: Forums > CMS Builder
Last Post: March 15, 2012 (RSS)
By gkornbluth - March 13, 2012
Well, I’m stuck again.
I’m trying to create a dynamic board of directors list and I think I’m almost there.
I have a list field in my accounts editor that’s fed from a the num and position field in a multi-record editor called board_of_director_positions.
I’ve got the positions to list correctly, but all records that match any of the positions are listed under each position, instead of just those records that match the particular position. Can’t seem to get that part (I’m probably forgetting something really basic).
I’ve defined a $directorposition variable after the load records calls at the top of the page:
$directorposition = array_filter(array_pluck($board_of_director_positionsRecords, 'position'));
In the body I’m using the following code to list the positions and each of the position holders:
?php foreach ($directorposition as $position): ?>
<h3><?php echo $position; ?></h3>
<?php foreach ($accountsRecords as $record): ?>
<?php if (($record['board_of_director_position_1'] || $record['board_of_director_position_2']) == $job) : ?>
<?php echo strtoupper($record['first_name']); ?> <?php echo strtoupper($record['last_name']); ?></b><br />
<?php endif ?>
<?php endforeach ?> <hr />
<?php endforeach ?>
Here’s an example of the current result:
President
JOE FRIEDMAN
JEAN HUTCHISON
LUCY KESHEVARZ
LYNDA KIDD
CORA LEE PALMA-HAYDEN
JOHN VINCENT PALOZZI
ALVARO ROJAS
ELLE SCHORR
KATHLEEN SCHULER
BEVERLY SNOW
MIKKI VICOLI-WALKER
JANET VILLASMIL
BETTY WILSON
Vice President
JOE FRIEDMAN
JEAN HUTCHISON
LUCY KESHEVARZ
LYNDA KIDD
CORA LEE PALMA-HAYDEN
JOHN VINCENT PALOZZI
ALVARO ROJAS
ELLE SCHORR
KATHLEEN SCHULER
BEVERLY SNOW
MIKKI VICOLI-WALKER
JANET VILLASMIL
BETTY WILSON
Any ideas appreciated.
Thanks,
Jerry Kornbluth
The first CMS Builder reference book is now available on-line!
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Re: [gkornbluth] Help with Array
By Jason - March 13, 2012
I noticed a couple of things in your code. First, in your if statement, you are comparing to a variable called $job, but I can't see where that is defined. I think you may want to change that to $position. Next you will want to change your if statement to use AND (&&) instead of OR (||). Using the OR means that the statement will evaluate to true as long as the record has a value in the board_of_director_position_1 field, which is why you're seeing every record under every position.
Try this:
<?php foreach ($directorposition as $position): ?>
<h3><?php echo $position; ?></h3>
<?php foreach ($accountsRecords as $record): ?>
<?php if (($record['board_of_director_position_1'] && $record['board_of_director_position_2']) == $position) : ?>
<?php echo strtoupper($record['first_name']); ?> <?php echo strtoupper($record['last_name']); ?></b><br />
<?php endif ?>
<?php endforeach ?> <hr />
<?php endforeach ?>
Hope this helps
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Re: [Jason] Help with Array
By gkornbluth - March 13, 2012
Thanks for looking at this.
Sorry, I had changed the $job in the actual code but not in the post.
Actually, I think it should be an "OR" since some of the board members hold 2 positions and need to be listed under both position headers. In those cases, their records would have values selected from both fields.
I don't know how to show all of the positions but only show the actual office holders under each position header.
I had an idea that I needed to use something like array_key somehow, but no idea.
Thanks,
Jerry
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Re: [gkornbluth] Help with Array
By Jason - March 14, 2012
I should have seen this the first time. :) You are right about the OR statement, but you need to check against $position for both fields. Current the statement says evaluate to true if position_1 has any value OR position_2 is equal to the current position.
Try this:
<?php if (($record['board_of_director_position_1'] == $position || $record['board_of_director_position_2']) == $position) : ?>
Hope this helps
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Re: [Jason] Help with Array
By gkornbluth - March 14, 2012 - edited: March 14, 2012
But I still get the complete list of matches listed for every position,where a record has an entry for both positions. I know that is confusing so, here's the example:http://www.artistsofpalmbeachcounty.org/bodtest.php
I've also attached the viewer.
Jerry
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Re: [gkornbluth] Help with Array
By Jason - March 15, 2012
Taking an even closer look at the if statement, it seems like we have some stray parentheses hanging about.
Try this:
<?php if ($record['board_of_director_position_1'] == $position || $record['board_of_director_position_2'] == $position) : ?>
Hope this helps
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Re: [Jason] Help with Array
By gkornbluth - March 15, 2012
But...
With the parentheses I get a list of all the categories and each lists those records that have an entry (from the pulldown list) in both fields in each Position category
http://www.artistsofpalmbeachcounty.org/bodtest-with.php
Without the parentheses I get only the position categories listed.
http://www.artistsofpalmbeachcounty.org/bodtest-without.php
I'm trying to get the president's name listed under president, the vice president's name listed under vice-president, etc.
The list of categories seems to work well, it's that depending on where the commas are, I get either
1) all the records with an entry in either field, or
2) all the records with entries in both fields or
3) none of the records
I can't seem to show only the specific record where the category matches the one displayed. (president under president, etc.)
Jerry
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Re: [gkornbluth] Help with Array
By Jason - March 15, 2012
My guess is that the code without the parentheses is correct and the issue is coming down to the way positions are being stored in the accounts table. In your code the value of $position is a string. How are positions stored inside the accounts table? If there's drop downs, what is the option value?
Let me know.
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Re: [Jason] Help with Array
By gkornbluth - March 15, 2012
There are separate records in a multi-record editor called Board of Director Positions with only one text field in each record for the position. There are currently 10 records.
The user accounts list fields are populated from that editor with the option values as num and the option labels as position
Hope that helps explain.
Jerry
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Re: [gkornbluth] Help with Array
By Jason - March 15, 2012
That's explains it. The current code where nothing shows up is the correct format of the if statement. What's happening is you're comparing the value of your positions fields (a number) to the $position variable (text). This is why nothing shows up. What you should do is loop through each of your position records one at a time and use the num field for comparison.
Hope this helps
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/