[FX.php List] Problems with showing rangeStart/ rangeEnd of a
foundCount
DC
dan.cynosure at dbmscan.com
Mon Dec 5 10:51:31 MST 2005
Hi Lars,
Welcome to the list. I hope that this helps you advance quickly in FX
and PHP.
This has some application specific code (the way I build URLs with db
and skip params as GET params rather than POST or COOKIE or SESSION or
some other way) and extends your requirements to include a Paging
navigation control interface as well as a record range display interface.
Note: you can't just drop this in the PHP page and expect it to work.
As is Alyson's example code you need the FX result array variable (i
call it result_array rather than searchResult), a skip value, a group
size (number of records desired in the recordset), additionally, this
requires a database name because it aims to be general code. Also, if
you have a different way of submitting skip, groupsize and db values,
you'll need to modify the $page_link_array values to suit.
Use this code in one <?php php?> block to get all the data into
variables that you can then echo to the page wherever you need them.
I've included the code here and put it at my snippets repository for
easier copy paste:
http://dbmscan.com/snippets/index.php?cat_select=FX_and_Smarty
I welcome any feedback and improvements you might have.
-------------------------------------------------
<?php
//-----------------START PAGING interface---------------------------
// you need to provide variables
// $result_array (the FX.php result array)
// $which_db (an FMP database name)
// $which_skip (number of records user wants to skip) and
// $group_size (number of records requested in range)
// and you end up with a bunch of variables that can be used to
// generate a paging and record number range interface
// the $page_link_array variable is an associative array
// that holds keys (string or number) which are the description of the
paging navigation links
// and the value (string) which are links that will enable the
// navigation through the recordset
//
// set up some configuration data
$max_num_of_pages_to_display=10;
// generate all the paging links to access recordsets 1-7, 8-14 etc...
// divide the found count by the groupsize to get the number of pages
$found_count = $result_array['foundCount'];
// avoid div by zero error, this is a silly way to do it, sorry
$num_of_pages=ceil($found_count/max(0.0001,$group_size));
$current_page_num =($which_skip/max(0.0001,$group_size))+1;
$center_pages_count=$max_num_of_pages_to_display*0.5;
$end_page=min($num_of_pages,max($max_num_of_pages_to_display,$current_page_num+$center_pages_count-1));
// cull the pagelink array to only show
// pages in and around the current page number
// cutting off at half the maximum
// this value is negative (but maxed to 1) until it hits the
center_page_count value
$page_start_num_for_page_chooser=max(1,$current_page_num-$center_pages_count);
// calculate at what page the first page link should begin to show
if ($page_start_num_for_page_chooser >= 2) {$show_first_page_link=TRUE;}
// check if there are some pages that will not be shown in the page links
// so that we can decide in the template whether to show a "skip X
pages" link
if ($num_of_pages>$end_page) {$more_pages_available=TRUE;}
if ($which_skip>0) {$show_previous_link=TRUE;}
if ($which_skip+$group_size<$found_count) {$show_next_link=TRUE;}
// check if we are deep into pages if we need a "go back X pages" link
$pages_back=min($current_page_num,$max_num_of_pages_to_display);
$pages_skip=min(($num_of_pages-$current_page_num),$max_num_of_pages_to_display);
// now, we've got some data...
// define strings for use in the paging links
// other strings can be defined later in the PHP output
// or in a template file
// in fact, these *really should be* defined in the template
// but, here they are used as keys in an associative array
$back_pages_link_string="...Back ".$pages_back." ";
$previous_page_link_string="<< Previous";
$skip_pages_link_string="Skip ".$pages_skip."...";
$next_page_link_string="Next >>";
// build up the array - order is important here
// add the "back X pages..." link if necessary
if ($show_first_page_link==TRUE) {
$page_link_array[$back_pages_link_string]=$_SERVER['PHP_SELF'] .
"?skip=" . ($current_page_num-$pages_back-1)*$group_size . "&db=" .
$which_db;
}
// and then the Previous page link
if ($show_previous_link==TRUE){
$page_link_array[$previous_page_link_string]=$_SERVER['PHP_SELF'] .
"?skip=" . ($current_page_num-2)*$group_size . "&db=" . $which_db;
}
// go through pages and figure out the skip value that needs to be
applied to the next request
// for the code to actually get FX.php to send back that record range
while($page_start_num_for_page_chooser<=$end_page) {
$page_link_array[$page_start_num_for_page_chooser]=
$_SERVER['PHP_SELF'] . "?skip=" .
($page_start_num_for_page_chooser-1)*$group_size . "&db=" . $which_db;
$page_start_num_for_page_chooser++;
}
//next page
if ($show_next_link==TRUE){
$page_link_array[$next_page_link_string]=$_SERVER['PHP_SELF'] .
"?skip=" . ($current_page_num)*$group_size . "&db=" . $which_db;
}
// add the "more pages..." link if necessary
if ($more_pages_available==TRUE) {
$page_link_array[$skip_pages_link_string]=$_SERVER['PHP_SELF'] .
"?skip=" . ($current_page_num+$pages_skip-1)*$group_size . "&db=" .
$which_db;
$show_last_page_link=TRUE;
}
// set up some db calling links
// these are specific to the application you are making
// in this case, we just manipulate skip and db valuees so
// this code can be reused for any database
$link_first = $_SERVER['PHP_SELF'] . "?skip=0" . "&db=" . $which_db;
$link_last = $_SERVER['PHP_SELF'] . "?skip=" .
$num_of_pages*$group_size . "&db=" . $which_db;
// should change these so they don't rely on FX returned values
// but are just calculated like all the other params
$link_next = $result_array['linkNext'] . "&db=" . $which_db;
$link_previous = $result_array['linkPrevious'] . "&db=" . $which_db;
// calculate the start and end record nums of the current foundset
$start_record = max(1,$which_skip);
$end_record = min($found_count,$which_skip+$group_size);
// you can ignore and DELETE this
// unless you are using Smarty template engine
// which I really recommend to all FMP PHP developers
$smarty->assign('currentpagenum', $current_page_num);
$smarty->assign('numofpages', $num_of_pages);
$smarty->assign('showfirstpagelink', $show_first_page_link);
$smarty->assign('showlastpagelink', $show_last_page_link);
$smarty->assign('showpreviouslink', $show_previous_link);
$smarty->assign('shownextlink', $show_next_link);
$smarty->assign('pagelinkarray', $page_link_array);
$smarty->assign('foundcount', $found_count);
$smarty->assign('linkfirst', $link_first);
$smarty->assign('linknext', $link_next);
$smarty->assign('linkprevious', $link_previous);
$smarty->assign('linklast', $link_last);
$smarty->assign('morepagesavailable', $more_pages_available);
$smarty->assign('groupsize', $group_size);
$smarty->assign('whichskip', $which_skip);
$smarty->assign('startrecord', $start_record);
$smarty->assign('endrecord', $end_record);
//-----------------END PAGING interface---------------------------
?>
Lars Arlér had written:
> Hey and thanks in advance. ;-)
>
> I am getting futher in my quest in transforming my CDML to PHP but ned som
> help or direction in solving this range issue. I am a newbee and fighting to
> learn FX.php and PHP. So sorry if this is bacic knowlege !!!
>
> I am trying to show my Db info like this
>
> "Showing 1 to 20 of 46 records"
> And when skiping 20 records
> "showing 21 to 40 of 46 records"
> And so forth
> "showing 41 to 46 of 46 records"
>
>
> I have tryed to use this code, but only 'foundCount' showsup.
>
>
> ---------zip---------
> Showing from
> <? echo $searchResult['rangeStart']; ?> to <? echo
> $searchResult['rangeEnd']; ?> of <? echo $searchResult['foundCount']; ?>
> ---------zip---------
>
>
> Thanks again
>
> Lars Arlér
>
>
>
>
> _______________________________________________
> FX.php_List mailing list
> FX.php_List at mail.iviking.org
> http://www.iviking.org/mailman/listinfo/fx.php_list
>
More information about the FX.php_List
mailing list