Jump to content

[SOLVED] Implementing a class


darknessmdk

Recommended Posts

I found this class on the net, It breaks results of a query up into pages

 

I however know nothing of classes and have no idea how to implement this into my code

 

The class is

<?php
    
    class Pagination {
        
        public $curPage;
        public $totalItems;
        public $itemsPerPage;
        public $urlPrefix;
        
        public $pageCount;
        public $links;
        public $cutOff;
        
        function __construct( $curPage , $totalItems , $itemsPerPage , $urlPrefix ){
            
            $this->curPage = $curPage;
            $this->totalItems = $totalItems;
            $this->itemsPerPage = $itemsPerPage;
            $this->urlPrefix = $urlPrefix;
            
            if( $this->totalItems > $this->itemsPerPage ) $this->init( );
            
        }
        
        private function wrapInContainer( $what ){
            echo '<div class="pagination">'.$what.'</div>';
        }
        
        private function init( ){
            
            //global $SETTINGS;
            
            $this->links = array( );
            $this->cutOff = 4;//$SETTINGS->get( 'PAGINATION_CUTOFF' );
            $this->pageCount = ceil( $this->totalItems / $this->itemsPerPage );
            
            if( $this->curPage > 1 ){
                $links[] = '<a href="'.$this->urlPrefix.'&p='.( $this->curPage - 1 ).'">< Previous</a>';
            }
            
            for( $i = 1; $i <= $this->pageCount ; $i++ ){
                
                if( 
                    ( $i > ( $this->cutOff + 1 ) && $i < ( $this->curPage - $this->cutOff ) ) ||
                    ( $i > ( $this->curPage + $this->cutOff ) && $i < ( $this->pageCount - $this->cutOff ) )
                ){
                    
                    $links[] = '.';
                    
                } else {
                
                    if( $i == $this->curPage ){
                        
                        $links[] = '<b>'.$i.'</b>';
                        
                    } else {
                        
                        $links[] = '<a href="'.$this->urlPrefix.'&p='.$i.'">'.$i.'</a>';
                        
                    }
                    
                }
                
            }
            
            $imploded = implode( ',' , $links );
            $imploded = preg_replace( '/(\.,)+/' , '...,' , $imploded );
            $links = explode( ',' , $imploded );
            
            if( $this->curPage < $this->pageCount ){
                $links[] = '<a href="'.$this->urlPrefix.'&p='.( $this->curPage + 1 ).'">Next ></a>';
            }
            
            $output = implode( ' ' , $links );
            $this->wrapInContainer( $output );
            
        }
        
    }
    
?> 

 

my code is

    <?PHP //Search Results 



				$query  = "SELECT * FROM gallery";  
				$result = mysql_query($query) or die("Error: " . mysql_error()); 
			    $countrows = mysql_num_rows($result);
				if($countrows == NULL) {
				   
				     echo ("No images currently uploaded");

				 } else {
							 	echo ("<table border='0' cellspacing='0' cellpadding='5' align='center'>");				
								if($result && mysql_num_rows($result) > 0)
				{

					$i = 0;

					$max_columns = 3;

					while($row = mysql_fetch_array($result))        

				   {

					   extract($row);

					   if($i == 0){

						  echo "<tr>";

				}	   

					echo ("<td align='center'><a href=\"javascript:openwindow('$row[image]',500,375,600,525,'The Better Basement Company')\" class=\"linkcolor\"><img src='$row[image]' width='182' height='124' border='1' class=\"linkcolor\" /></a></td>");


       if(++$i == $max_columns) 

       {

           echo "</tr>";

			   $i=0;

		   }  // end if 

	   } // end while

	} // end if results
	// clean up table

	if($i < $max_columns)

	{

		for($j=$i; $j<$max_columns;$j++)

			echo "";

	}
		echo ("</table>");
	}
	?>

 

Any help would be appreciated, thanks

Link to comment
https://forums.phpfreaks.com/topic/69772-solved-implementing-a-class/
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.