10 messages on every page.

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

ednark
New php-forum User
New php-forum User
Posts: 18
Joined: Fri Oct 04, 2002 8:09 pm

batch display code

Postby ednark » Thu Oct 10, 2002 11:19 am

im not sure exactly what you are asking, but i think this should be part of what you want.

this is some batch display code i wrote, based off something originally written in python by one Randolpho...

this function basically three important pieces of information between page calls:
1) the current page we wish to view
2) the total number of results
3) the number of results displayed on each page

the first time you view a page using this you were not given this info, so it is up to the recieveing page to determine defualt values for the first call to this function

It is also the reponsibility of the page to use this info passed to it to actually retieve and display each results.

This function only writes the menu, but should give each page enough info to find only the results that should be displayed on this page

you may not be familiar with the c-style sprintf function but as this was adapted from python code it retains that much similarity to how python processes strings...

hope you enjoy

Code: Select all

function batchDisplayMenu( $link_location, $current_page, $total_batch_size, $batch_step_size, $extra_get_params=array(), $menu_properties=array() )
{
    /// display options
    $display_all_pages  = FALSE;
    $page_display_range = 4;

    $show_total_pages = TRUE;
    $show_ellipses    = TRUE;
    $show_next        = TRUE;
    $show_previous    = TRUE;
    $show_last        = TRUE;
    $show_first       = TRUE;

    /// format templates for displaying menu
    $total_pages_format_string  = "%s results in %s page(s) : ";
    $ellipse_format_string      = "...";
    $no_ellipse_format_string   = "   ";
    $next_format_string         = ">>";
    $previous_format_string     = "<<";

    $first_format_string        = "first";
    $last_format_string         = "last";
    $current_page_format_string = "<b>%s</b>";
    $blank_page_format_string   = "&nbsp;&nbsp";

    $width = 20;

    /// this should overwrite any previous variables
    extract($menu_properties);

    /// display ranges for pages
    $total_pages = ceil( $total_batch_size / $batch_step_size );
       
    $page_range_begin = max( ($current_page-$page_display_range), 1 );
    $page_range_span  = ($page_display_range*2);
    $page_range_end   = ($page_range_begin+$page_range_span);


    /// pass along, via GET, anything in the extra_get_params array, when a user clicks on new page of results to view
    $link_format_string = "<a href=\"{$link_location}?current_page=%s&batch_step_size={$batch_step_size}&total_batch_size={$total_batch_size}";
    foreach ( $extra_get_params as $item=>$value ) {
        $safe_value = str_replace( '%', '%%', urlencode($value) );
        $link_format_string .= "&{$item}={$safe_value}";
    }
    $link_format_string .= "\">%s</a>";

    /// show the X out of Y page results
    if ( $show_total_pages ) {
        $string .= '<center>';
        $string .= sprintf($total_pages_format_string,$total_batch_size,$total_pages);
        $string .= '</center>';
    }

    $string .= '<table border="0" class="batch_list"><tr align="center">';

    $string .= '<td nowrap="nowrap">';
   
    /// show link to jump to first page
    if ( $show_first ) {
        if ( $current_page > 1 ) {
            $string .= sprintf($link_format_string,1,$first_format_string) ." ";
        } else {
            $string .= $first_format_string ." ";
        }
    }
   
    /// show link to go back one page
    if ( $show_previous ) {
        if ( $current_page > 1 ) {
            $string .= sprintf($link_format_string,max(($current_page-1),1),$previous_format_string) ." ";
        } else {
            $string .= $previous_format_string ." ";
        }
    }
   
    /// to ellipse or not ellipse
    $string .= ( $show_ellipses and $page_range_begin>1 ) ? sprintf($ellipse_format_string)." " : sprintf($no_ellipse_format_string)." ";
    $string .= '</td>';

    /// show either all pages on screen at once, or show only X at a time, like google, a batch of batch pages :)
    if ( $display_all_pages ) {
        $link_range = range( 1, $total_pages );
    } else{
        $link_range = range( $page_range_begin, $page_range_end );
    }

    /// them meat, show a link to each page of items we have
    foreach ( $link_range as $index ) {
        $string .= '<td width="'.$width.'" nowrap="nowrap">';
        if ( $index == $current_page ) {
            $string .= sprintf($current_page_format_string,$index) ." ";
        } elseif ( $index <= $total_pages ) {
            $string .= sprintf($link_format_string,$index,$index) ." ";
        } elseif ( $index > $total_pages ) {
            $string .= $blank_page_format_string ." ";
        }
        $string .= '</td>';
    }
   
    $string .= '<td nowrap="nowrap">';
    $string .= ( $show_ellipses and $page_range_end<$total_pages ) ? $ellipse_format_string ." " : $no_ellipse_format_string ." ";

    /// show a link to skip foreward on epage
    if ( $show_next ) {
        if ( $current_page < $total_pages ) {
            $string .= sprintf($link_format_string,min(($current_page+1),$total_pages),$next_format_string) ." ";
        } else {
            $string .= $next_format_string ." ";
        }
    }

    /// show a link to the last page
    if ( $show_last ) {
        if ( $current_page < $total_pages ){
            $string .= sprintf($link_format_string,$total_pages,$last_format_string) ." ";
        } else {
            $string .= $last_format_string ." ";
        }
    }
    $string .= '</td>';
   
    $string .= '</tr></table>';

    return $string;
}




usage:

Code: Select all


$current_page = /*find_in_get*/
if ( !$current_page ) {
    $current_page = 1;
}

$batch_step_size = /*find_in_get*/
if ( !$batch_step_size ) {
    $batch_step_size = 20;
}

$total_batch_size = /*find_in_get*/
if ( !$total_batch_size ) {
    $total_batch_size = /* get total number of results that will be shown accross all pages */;
}

$limit_start = (($current_page-1) * $batch_step_size);

$item_for_this_page = /* get entries $limit_start through ($limit_start+$batch_step_size)  */

if ( $total_batch_size > 0 ) {
     $get_params = array( 'var1'=>'val1', 'var2'=>'val2' );
    echo batchDisplayMenu( './', $current_page, $total_batch_size, $batch_step_size, $get_params );
}

foreach ( $item_for_this_page as $item ) {
    displayItem( $item );
}



Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 2 guests