0

Here is the custom post declaration

function ts_result_reg() {
$labels = array(
'name' => _x( 'Students Result', 'post type general name' ),
'singular_name' => _x( 'Students Result', 'post type singular name' ),
'add_new' => _x( 'Add New Result', 'result' ),
'add_new_item' => __( 'Add New Result' ),
'edit_item' => __( 'Edit Result' ),
'new_item' => __( 'New Result' ),
'all_items' => __( 'All Results' ),
'view_item' => __( 'View Result' ),
'search_items' => __( 'Search Results' ),
'not_found' => __( 'No result found' ),
'not_found_in_trash' => __( 'No result found in the Trash' ),
'parent_item_colon' => '',
'menu_name' => 'Student Results'
 );
 $args = array(
 'labels' => $labels,
 'description' => 'Add new custom post type students result',
 'public' => true,
 'menu_position' => 5,
 'supports' => array( 'thumbnail','title', ),
 'taxonomies' => array( 'classes' ),
 'has_archive' => true,
 'rewrite'=> array ('slug' => 'tscheck'),
 );
 register_post_type( 'ts_students_result', $args );
 }
 add_action( 'init', 'ts_result_reg' );

The backend metaboxes

array(
            'name' => 'French',
            'id'   => $prefix . 'subject_french_exam_total',
            'type' => 'text_small',
            // 'repeatable' => true,
        ),

array(
            'name' => 'Business Education,
            'id'   => $prefix . 'subject_business_edu_exam_total',
            'type' => 'text_small',
            // 'repeatable' => true,
        ),

        ...

The front end query

 query_posts( array(
...
)
    ),
'posts_per_page' => 100
   )
);
?>
<table style="border:1px solid black" cellpadding="1.5" cellspacing="5">
<tbody>
    <tr>
        <th><strong>NAME\SUBJECT</strong></th>
        <th align='center'><strong>French</strong></th>
        <th align='center'><strong>Business Education</strong></th>
        <th align='center'><strong>Total</strong></th>
        <th align='center'><strong>Average</strong></th>
        <th align='center'><strong>Position</strong></th>
    </tr>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- Initializing count and sum -->
    <?php
        $subject_count=0;
        $subject_sum_total=0;

         $subject_french_exam_total= get_post_meta(get_the_ID(),'_ts_subject_french_exam_total',true );
         if($subject_french_exam_total):
         $subject_count++;
         $subject_sum_total+=$subject_french_exam_total;
         endif;

         $subject_business_edu_exam_total= get_post_meta(get_the_ID(),'_ts_subject_business_edu_exam_total',true );
         if($subject_business_edu_exam_total):
         $subject_count++;
         $subject_sum_total+=$subject_business_edu_exam_total;
         endif;

        $store_student_avg=array();
      if($subject_sum_total>0.1):
      $subject_avg_total= $subject_sum_total/$subject_count;
      array_push($store_student_avg,$subject_avg_total);
       endif;
    ?> 
    <tr>
    <td><?php the_title(); ?></td>
    <td><?php echo $subject_french_exam_total ;?></td>
    <td><?php echo $subject_business_edu_exam_total ;?></td>
    <td><?php echo $subject_sum_total ;?></td>
    <td><?php echo $subject_avg_total ;?></td>

<?php 
rsort($store_student_avg);
$arrlength = count($store_student_avg);
$rank = 1;
$prev_rank = $rank;
for($x = 0; $x < $arrlength; $x++)
 if ($x==0): ?>
    <td><?php echo $rank; ?></td>

   <?php elseif ($numbers[$x] != $numbers[$x-1]):
    $rank++;
    $prev_rank = $rank;
    ?>
    <td><?php echo $rank; ?></td>

   <?php else:
    $rank++;
    ?>
       <td><?php echo $prev_rank; ?></td>

<?php endif; ?>

    </tr>
    <?php endwhile;endif; ?>   
</tbody>
</table>

Edited: In the above code(shortened) I need the positions(rank) for each post's average calculated. Seems the whole loop is not run in wordpress before the output, hence, the output for each post is 1 (screenshot of output). How do I solve this?

Samuel Kazeem
  • 787
  • 1
  • 8
  • 15
  • You could put all averages into an array and then [`sort`](http://php.net/manual/en/function.sort.php) it. You'd have to decide what to do about equal averages though. – Pyromonk May 27 '17 at 04:13
  • can you post your custom post type code? – Vel May 27 '17 at 05:50
  • @Pyromonk do look up the edited above, still not getting the correct output though. – Samuel Kazeem May 29 '17 at 12:39
  • @ChuckG, are you familiar with [variable scope](https://stackoverflow.com/questions/25862715/variable-scope-in-for-loop-and-while-loop)? Sorry I didn't answer your question from before, but no notifications are sent for comments that don't feature a nickname prefixed with an @. – Pyromonk May 29 '17 at 13:32
  • @Pyromonk testing that code on its own works properly, I guess the WP loop must be the thing to get a work around on, you don't mind giving some code of your own? – Samuel Kazeem May 30 '17 at 03:12
  • @vel what does d custom type code declaration have to do with this? – Samuel Kazeem May 30 '17 at 03:13
  • I'm sorry, but I don't understand you at all. I don't have *your* Wordpress setup, so I don't understand how you expect me to be able to test your code. – Pyromonk May 30 '17 at 03:55
  • I don't have your Wordpress setup, i asked custom type code for this. – Vel May 30 '17 at 04:35
  • @vel Thanks. The custom code is posted above. – Samuel Kazeem May 31 '17 at 07:34
  • @Pyromonk Thanks. The custom code is posted above. – Samuel Kazeem May 31 '17 at 07:35
  • @ChuckG, can you add with hook for `The backend metaboxes`?. because it will take sometime setup your code – Vel May 31 '17 at 09:39

0 Answers0