Getting calculated data in table

Getting calculated data in table

JoepioooJoepiooo Posts: 1Questions: 0Answers: 0
edited November 2013 in DataTables 1.9
Hello,

I'm using the latest version of Datatable and I really love it! I only got one problem... I'm using a WordPress plugin called (Football Pool) http://wordpress.org/plugins/football-pool/

You also got a ranking page that calculate the positions of participants...

This ranking data is not inserted into a MySQL table but is calculated...

To be honest I dont know how its working exactly...

I got the server side script working but dont know how to manage getting all the data into the table without a MySQL table...

This are all the classes:

How can I get this into the table?

[code]<?php
class Football_Pool_Pool {
public $leagues;
public $has_bonus_questions = false;
public $has_matches = false;
public $has_leagues;
private $force_lock_time = false;
private $lock_timestamp;
private $lock_datestring;
public $always_show_predictions = 0;
public $show_avatar = false;
private $pool_has_jokers;



public function get_pool_ranking_limited( $league, $num_users
, $ranking_id = FOOTBALLPOOL_RANKING_DEFAULT
, $score_date = '' ) {
global $wpdb;
$sql = $this->get_ranking_from_score_history( $league, $ranking_id, $score_date ) . ' LIMIT %d';
$sql = $wpdb->prepare( $sql, $num_users );
return $wpdb->get_results( $sql, ARRAY_A );
}

public function get_pool_ranking( $league, $ranking_id = FOOTBALLPOOL_RANKING_DEFAULT ) {
$cache_key = 'fp_get_pool_ranking_' . $ranking_id;
$rows = wp_cache_get( $cache_key );

if ( $rows === false ) {
global $wpdb;
$sql = $this->get_ranking_from_score_history( $league, $ranking_id );
$rows = $wpdb->get_results( $sql, ARRAY_A );
wp_cache_set( $cache_key, $rows );
}

return $rows;
}

public function get_prediction_count_per_user( $users, $ranking_id = FOOTBALLPOOL_RANKING_DEFAULT ) {
global $wpdb;
$prefix = FOOTBALLPOOL_DB_PREFIX;

if ( is_array( $users ) && count( $users ) > 0 ) {
$num_predictions = array();
$users = implode( ',', $users );

// matches
if ( $ranking_id == FOOTBALLPOOL_RANKING_DEFAULT ) {
$sql = "SELECT user_id, COUNT(*) AS num_predictions
FROM {$prefix}predictions
WHERE user_id IN ( {$users} ) GROUP BY user_id";
} else {
$sql = "SELECT p.user_id, COUNT(*) AS num_predictions
FROM {$prefix}predictions p
JOIN {$prefix}rankings_matches r
ON ( r.match_id = p.match_id AND r.ranking_id = {$ranking_id} )
WHERE p.user_id IN ( {$users} ) GROUP BY p.user_id";
}
$rows = $wpdb->get_results( $sql, ARRAY_A );

foreach ( $rows as $row ) {
$num_predictions[$row['user_id']] = $row['num_predictions'];
}

// questions
if ( $ranking_id == FOOTBALLPOOL_RANKING_DEFAULT ) {
$sql = "SELECT user_id, COUNT(*) AS num_predictions
FROM {$prefix}bonusquestions_useranswers
WHERE user_id IN ( {$users} ) GROUP BY user_id";
} else {
$sql = "SELECT p.user_id, COUNT(*) AS num_predictions
FROM {$prefix}bonusquestions_useranswers p
JOIN {$prefix}rankings_bonusquestions r
ON ( r.question_id = p.question_id AND r.ranking_id = {$ranking_id} )
WHERE p.user_id IN ( {$users} ) GROUP BY p.user_id";
}
$rows = $wpdb->get_results( $sql, ARRAY_A );

foreach ( $rows as $row ) {
if ( array_key_exists( $row['user_id'], $num_predictions) ) {
$num_predictions[$row['user_id']] += $row['num_predictions'];
}
}

// return resulting array of user ids with their total number of predictions
return $num_predictions;
} else {
return false;
}
}

public function print_pool_ranking( $league, $user, $ranking_id = FOOTBALLPOOL_RANKING_DEFAULT ) {
$output = '';

$rows = $this->get_pool_ranking( $league, $ranking_id );
$ranking = $users = array();
if ( count( $rows ) > 0 ) {
// there are results in the database, so get the ranking
foreach ( $rows as $row ) {
$ranking[] = $row;
$users[] = $row['user_id'];
}
} else {
// no results, show a list of users
$rows = $this->get_users( $league );
if ( count( $rows ) > 0 ) {
$output .= '' . __( 'No results yet. Below is a list of all users.', FOOTBALLPOOL_TEXT_DOMAIN ) . '';
foreach ( $rows as $row ) {
$ranking[] = $row;
$users[] = $row['user_id'];
}
} else {
$output .= ''. __( 'No users have registered for this pool (yet).', FOOTBALLPOOL_TEXT_DOMAIN ) . '';
}
}

if ( count( $ranking ) > 0 ) {
// get number of predictions per user if option is set
$show_num_predictions = ( Football_Pool_Utils::get_fp_option( 'show_num_predictions_in_ranking' ) == 1 );
if ( $show_num_predictions ) {
$predictions = $this->get_prediction_count_per_user( $users, $ranking_id );
}

$userpage = Football_Pool::get_page_link( 'user' );
$all_user_view = ( $league == FOOTBALLPOOL_LEAGUE_ALL && $this->has_leagues );
$i = 1;

$output .= '';
if ( $show_num_predictions ) {
$output .= sprintf( '
positie
%s
%s
%s
%s'
, __( 'user', FOOTBALLPOOL_TEXT_DOMAIN )
, __( 'predictions', FOOTBALLPOOL_TEXT_DOMAIN )
, __( 'points', FOOTBALLPOOL_TEXT_DOMAIN )
, ( $all_user_view ? '' : '' )
);
}

foreach ( $ranking as $row ) {

$class = ( $i % 2 != 0 ? 'even' : 'odd' );
if ( $all_user_view ) $class .= ' league-' . $row['league_id'];
if ( $row['user_id'] == $user ) $class .= ' currentuser';
if ( $show_num_predictions ) {
if ( array_key_exists( $row['user_id'], $predictions ) ) {
$num_predictions = $predictions[$row['user_id']];
} else {
$num_predictions = 0;
}
$num_predictions = sprintf( '%d', $num_predictions );
} else {
$num_predictions = '';
}
$output .= sprintf( '%d.
%s%s%s
%s%d%s
',
$class,
$i++,
esc_url( add_query_arg( array( 'user' => $row['user_id'] ), $userpage ) ),
$this->get_avatar( $row['user_id'], 'medium' ),
$row['user_name'],
Football_Pool::user_name( $row['user_id'], 'label' ),
$num_predictions,
$row['points'],
( $all_user_view ? $this->league_image( $row['league_id'] ) : '' )
);
$output .= "\n";
}
$output .= '';
}

return $output;
}

[/code]

I know this code is actualy way to long for posting but I dont know where I should edit something so thats why I'm posting a part of it.

Can somebody help me?

Tnx

Regards

Joep

Replies

  • allanallan Posts: 63,381Questions: 1Answers: 10,449 Site admin
    I'm afraid this isn't a great place to ask server-side questions. All I can really say is that you want to output the data as a plain HTML table or as JSON data. Not helpful I know - sorry. You'd be better asking on SO for a general PHP question.

    Allan
This discussion has been closed.