Comment 1 for bug 736257

rg1024 (rg1024) wrote :

// create as member of class CreateLayout
// and call function...

private function sql_records_num ($widget) {
    global $db;

    // void the numerous SELECT count(id)
    if ( strncasecmp($widget->normal_select,"select count(",13)==0) {
        return 1;
    }

    // correct BUG for GROUP [HAVING], AS, LIMIT.
    if ( stristr ( $widget->normal_select, " GROUP " )!== false ||
         stristr ( $widget->normal_select, " AS " ) !== false ||
         stristr ( $widget->normal_select, " LIMIT ") !== false ) {
        $db->get_results($widget->normal_select);
        return $db->num_rows;
    }

    preg_match('/select DISTINCT(.*)from/i', $widget->normal_select, $get_DISTINCT);
    preg_match('/select (.*)from/i' , $widget->normal_select, $selectionmatch);

    if (isset($selectionmatch['1'])){
        if (isset ($get_DISTINCT['1'])){
            $mysql_count = ' count(DISTINCT('.$get_DISTINCT['1'].')) ';
        }else{
            $mysql_count = ' count(*) ';
        }
        $records_num_query = str_replace($selectionmatch['1'], $mysql_count, $widget->normal_select);
        //RG1024. original don't include ASC.
        $records_num_query = preg_replace('/ORDER BY(.*)( DESC| ASC)?/i', '', $records_num_query);

        $records_num = $db->get_var($records_num_query);
        return $records_num;

    }
    // return 0 not '' ok?
    return 0;

    }