0

I have class which extends WP_Table_List and renders a table. See the events results

In the prepare_items() function I querys the DB

function prepare_items($race)
{
    $columns = $this->get_columns();
    $hidden = array();
    $this->_column_headers = array($columns, $hidden, $this->get_sortable_columns());

    $orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'position+0'; //If no sort, default to title
    $order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'asc'; //If no order, default to asc
    if($orderby!='position+0')
        $order .= ', position+0';

    global $wpdb;
    $query = '
        SELECT wp_bhaa_raceresult.*,wp_users.display_name,
        wp_users.user_nicename,gender.meta_value as gender,wp_posts.id as cid,wp_posts.post_title as cname
        FROM '.$wpdb->prefix .'bhaa_raceresult 
        left join wp_users on wp_users.id=wp_bhaa_raceresult.runner 
        left join wp_usermeta gender on (gender.user_id=wp_users.id and gender.meta_key="bhaa_runner_gender")
        left join wp_usermeta company on (company.user_id=wp_users.id and company.meta_key="bhaa_runner_company")
        left join wp_posts on (wp_posts.post_type="house" and company.meta_value=wp_posts.id)
        where race='.$race.' and wp_bhaa_raceresult.class="RAN" ORDER BY '.$orderby.' '. $order;
    $querydata = $wpdb->get_results($query,ARRAY_A);
    $this->items = $querydata;
}

and i'm using an outputbuffer to capture the html of the table into a single string via

function renderTable($race)
{   
    error_log("render table ".$race);
    ob_start();
    $this->prepare_items($race);
    $this->display();
    return ob_get_clean();
}

In our most recent event, we had 500 results and this seems to a memory issue for the output buffer. I'm wondering is there better way for me to flush the table html code into the buffer to that it does not cause an out of memory issue. My memory limit setting is

 define('WP_MEMORY_LIMIT', '128M');

The full class code can be seen at

https://github.com/emeraldjava/bhaawp/blob/master/table/raceresulttable.class.php

EDIT

I added a call to memory_get_usage() when each row of the table is being built

function column_default( $item, $column_name ) {
    error_log('column_default '.$item['position'].' '.memory_get_usage());

the results are

[24-Apr-2013 15:14:12 UTC] column_default 489 48477784
[24-Apr-2013 15:14:12 UTC] column_default 489 48477784
[24-Apr-2013 15:14:12 UTC] column_default 489 48477832
[24-Apr-2013 15:14:12 UTC] column_default 489 48477816
[24-Apr-2013 15:14:12 UTC] column_default 489 48477832
[24-Apr-2013 15:14:12 UTC] column_default 489 48477832

which indicates i'm below the MEMORY_LIMIT?

EDIT 2

I decided to implement the 'display_rows()' method and flush the html content after every row of the table is generated

function display_rows() {
    parent::display_rows();
    ob_flush();
}

This solution allows the whole table to be generated but i cannot control the output witih the template.

  • How much memory do you have on the server and how much does PHP have to use? I believe that the output buffer limit is the PHP limit. – s_ha_dum Apr 24 '13 at 15:10
  • @s_ha_dum There is more memory available but increasing the limit doesn't effect it. See the extra EDIT which includes memory_get_usage() details – emeraldjava Apr 24 '13 at 15:17
  • Why don't you log the entries from your display() function? No need for output buffering. – fuxia Apr 24 '13 at 15:20
  • @toscho I want to output the html table in a specific location of my template. See the example where the table is embedded within some tabs which are generated via a template shortcode. – emeraldjava Apr 24 '13 at 15:22
  • Side Note: Your "an exmple is seen here" link is crashing my browser and my X server. What are doing on that site? – s_ha_dum Apr 24 '13 at 15:26
1

How often do the results change? I think the results are finally and did not change often (or never). Maybe it is better to use a file caching then using output buffering.

Write the results to a file and append a hash or date to the filename as indicator. If the results are updated or changed, delete the file and recreate it in prepare_items()

  • Table doesn't change that often so a file cache is an option, but wouldn't i still have the issue of capturing the output from the WP_Table_List during the prepare_items() call and dumping it to the file. – emeraldjava Apr 26 '13 at 8:20
  • A memory Problem? I don't think so. Maybe a time out would be possible. But in this case, the timeout limit can be raised. – Ralf912 Apr 26 '13 at 9:12

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.