0

I have a foreach loop inside the loop that is getting info from a separate table. The table has a column that has an id that reflects the id of the post type so it should always be unique.

When I look at the results, the created variable ($fullname) is displayed multiple times in rows with posts that do not share the same id as the $fullname event id.

Here is the code with all of the looping action

<?php
        // the loop
        if (have_posts()) : 
                $output.='<table class="contentTable" border="0" width="100%">';
                $output.='<tr class="one-sixth">';
                $output.='<th>Agency</th>';
                $output.='<th>Project Description</th>';
                $output.='<th>Time Frame</th>';
                $output.='<th>Team Leader</th>';
                $output.='<th>Volunteer Limit</th>';
                $output.='</tr>';
                while (have_posts()) : the_post();

                        //Get post attendee amount
                        $project_id = get_the_ID();
                        $attendees = $wpdb->get_var("SELECT COUNT(user_id) FROM ".$wpdb->prefix."mro_attendees WHERE event_id = ".$project_id."");

                        //Get team leader name
                        $leader = $wpdb->get_results("SELECT user_first_name, user_last_name FROM ".$wpdb->prefix."mro_attendees WHERE event_id = ".$project_id." AND user_role = 'team_leader' LIMIT 0,1");
                        foreach ($leader as $lead) {
                        $leaderfirst = $lead->user_first_name;
                        $leaderlast = $lead->user_last_name;
                        $fullname = $leaderfirst . ' ' . $leaderlast;
                          }

                        //Title and link
                        $temp_title = get_the_title($post->ID);
                        $temp_link = get_permalink($post->ID);

                        // output all findings - CUSTOMIZE TO YOUR LIKING
                        $output.='<tr class="one-sixth">';
                        $output.='<td><a href="'.$temp_link.'">'.$temp_title.'</a></td>';
                        $output.='<td>'.get_custom_field('project_description').'</td>';
                        $output.='<td>'.get_custom_field('project_timeframe').'</td>';
                        $output.='<td>'.$fullname.'</td>';
                        $output.='<td>'.$attendees.'/'.get_custom_field('project_limit').'</td>';
                        $output.='</tr>';

                endwhile;
                $output.='</table><div class="clear-line"></div>';
                //pagination links
                $output.= wp_pagenavi();
                $output.='<style>#pagenavi{display:none;}</style>';
                $output.='<style>.wp-pagenavi{margin-bottom:20px;}</style>';
        else:

                $output.='nothing found.';

        endif;
?>

The team leader $fullname should only show once according to the post id that the event id is equal to in the loop, however, the output results end up putting these people in other rows for projects (post ids) that do not share the same event id with the person. So they (the team leader names) appear in extra rows in the loop and I can not figure out why this is the case. The count query above that does not cause the same problem.

Thanks and great appreciation to whoever can offer me help!

  • is it the case that the projects it happens on don't have leader associated with them? or do all projects have a leader and some are outputting the wrong name? – Milo Aug 13 '14 at 22:06
  • They happen on ones that do not have them. – Alex Howell Aug 13 '14 at 22:07
1

This is a php logic / variable issue. You output $fullname whether or not it has been set inside that iteration's foreach. So for projects that never enter the foreach to set $fullname, it still contains whatever the last iteration had set it to. Set it to an empty string before the foreach so it only contains a name if it enters the foreach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.