Jump to content

fixing each record number


mehdi_php

Recommended Posts

hi all ,

 

i write this function to paginate my query result and display it with smarty

<?php
function GetAllProfiles($p , $s , $dbtbl , $dis){
global $dbh , $smarty  , $db ;
$display = $dis ; 
$count = $db->get_var("select count(*) from $dbtbl"); 	

	if(isset($p)):
	$pages = $p ; 
	else:
		if($count > 0 ):
		$pages = ceil($count / $display ) ; 
		endif; 
	endif; 

	//get start 
	if(isset($s)):
	$start = $s ; 
	else:
	$start = 0 ;
	endif;

	//determind current page 
	if($pages > 1 ) :
	$current_page = ($start/$display) + 1; 
	endif;

$sth = $dbh->prepare("select * from $dbtbl LIMIT $start , $display ") ; 
$sth->execute();
$result = $sth ->fetchAll();
$smarty->assign("result" , $result) ; 
$smarty->assign("cur_page" , $current_page);
$smarty->assign("display" , $display) ; 
$smarty->assign("start" , $start) ; 
$smarty->assign("pages" , $pages ) ; 
}
?>

 

and call it like this

 

GetAllProfiles($_GET['p'] , $_GET['s'] , 'xxprofile' ,5 );

 

i profile tpl page i have this code

  	 {section name=co loop=$result}
          <tr bgcolor="#FFFFFF">
            <td>{$smarty.section.co.index_next}</td>
            <td><a href="?module=profile&task=edit&id={$result[co].xprofileid}">{$result[co].xprojectname}</a></td>
            <td>{$result[co].xprojectsubject}</td>
            <td>{$result[co].xcvkey}</td>
            <td>{$result[co].xdate}</td>
            <td><a href="?module=profile&task=del&id={$result[co].xprofileid}">
            <img src="../images/trash.gif" border="0"/></a></td>
            <td><a href="?module=profile&task=edit&id={$result[co].xprofileid}">
            <img src="../images/edit.gif" border="0"/></a></td>
          <tr>
        {sectionelse}
          <tr><td colspan="5">No items found</td></tr>
        {/section}
      {if $pages gt 1 }
        <tr bgcolor="#FFFFFF">
        <td colspan="7">
        
        {if $cur_page ne 1 }
        <a href="?module=profile&s={math equation="x - y" x=$start y=$display}&p={$pages}"> Previous </a>
        {/if}
        {section name=num_links loop=$pages }
       	<a href="?module=profile&s={math equation="x*(y-1)" x=$display y=$smarty.section.num_links.index_next}&p={$pages}">
        {$smarty.section.num_links.index_next}
        </a>
        {/section} 
       
        {if $cur_page ne $pages } 
        <a href="?module=profile&s={math equation="x+y" x=$start y=$display}&p={$pages}">Next</a>
        {/if}
        </td>
        
        </tr>
       {/if}

 

and an screen shot of result is http://i33.tinypic.com/jj72gj.jpg

 

but as you can see in image i use {$smarty.section.co.index_next} for # filed in page 1 it's correct . but when i go to the second page the # filed is again from 1 to 5 instead of 6 to 10 what can i do to fix it ?

and please let me know if this function is reusable or not ? is it handy and helpful ?

Link to comment
https://forums.phpfreaks.com/topic/129557-fixing-each-record-number/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.