'$wpdb->get_results' posts display in separate ccs tables -need a single table

My query pulls all the data I'm asking for from my custom post and custom fields. The fields fill in and the rows and columns look good. But the css table is breaking such that (as Nathan points out below) each post shows in a separate table and I get a header over a row, header over a row, etc.

I've tried a number of ways to code the table, but the rows display exactly like that each time.

It's messy, I admit, But I am not taking any shortcuts on the table structure so to show exactly the way I am getting it to at least show all the data in the correct columns

<section>
<?php
 $querystr = "
    SELECT $wpdb->posts.* 
    FROM $wpdb->posts, $wpdb->postmeta
    WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 
    AND $wpdb->posts.post_status = 'publish' 
    AND $wpdb->posts.post_type = 'event'
    AND $wpdb->posts.post_date < NOW()
    ORDER BY $wpdb->posts.post_date DESC
 ";

 $pageposts = $wpdb->get_results($querystr, OBJECT_K);
 ?>
 <div class="post" id="post-<?php the_ID(); ?>">

<?php if ($pageposts): ?>
<?php global $post; ?>
 <?php echo '<table class="maee-table"><col width="20%"><col width="30%"> 
<col width="20%"><col width="30%">';?>
 <?php foreach ($pageposts as $post): ?>
 <?php setup_postdata($post); ?>

<?php echo '<tr>';?>
<?php echo '<tr>';?>
    <?php echo '<th>';?>Date <?php echo '</th>';?>
    <?php echo '<th>';?>Name<?php echo '</th>';?>
    <?php echo '<th>';?> Audience<?php echo '</th>';?>
    <?php echo '<th>';?>Submitting partner<?php echo '</th>';?>
  <?php echo '</tr>';?>
<?php echo '<tr>';?>
<?php echo '<td class="maee-row">';?> <?php rwmb_the_value( 'prefix- 
event_date1');?> <?php echo '</td>';?>
<?php echo '<td class="maee-row">';?><a href="<?php the_permalink() ?>" 
rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"> 
<?php the_title(); ?></a><?php echo '</td>';?>
<?php echo '<td class="maee-row" >';?><?php rwmb_the_value( 'prefix- 
event_audience1');?><?php echo '</td>';?>
<?php echo '<td class="maee-row">';?> <?php the_category(', ') ?>
<?php echo '</td> </tr>';?>
<?php endforeach; ?>
<?php echo '</table>';?>
<?php wp_reset_postdata() ?>
<?php endif; ?>
</div>
</section>

1 answer

  • answered 2019-08-10 17:55 DubVader

    You could try something like this:

        global $wpdb;
    
        $querystr = "
        SELECT $wpdb->posts.* 
        FROM $wpdb->posts, $wpdb->postmeta
        WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 
        AND $wpdb->posts.post_status = 'publish' 
        AND $wpdb->posts.post_type = 'event'
        AND $wpdb->posts.post_date < NOW()
        ORDER BY $wpdb->posts.post_date DESC
     ";
    
    
    
        $content = '<table class="maee-table"><col width="20%"><col width="30%"> 
    <col width="20%"><col width="30%">';
    
        $content .= '<tr><th>Date</th><th>Name</th><th>Audience</th><th>Submitting Partner</th></tr>';
    
        $results = $wpdb->get_results($querystr);
    
        foreach ($results as $post) {
    
            setup_postdata($post);
    
            $content .= '<tr>';
            $content .= '<td class="maee-row">' . rwmb_the_value( 'prefix- 
    event_date1') . '</td>';
            $content .= '<td class="maee-row">' . the_title() . '</td>';
            $content .= '<td class="maee-row">' . rwmb_the_value( 'prefix- 
    event_audience1') . '</td>';
            $content .= '<td class="maee-row">' . the_category(', ') . '</td>';
            $content .= '</tr>';
        }
        $content .= '</table>';
    
        // return the table
        echo $content;