1
sarahmx
search, sort, pagination in XOOPS
  • 2013/4/7 5:51

  • sarahmx

  • Quite a regular

  • Posts: 381

  • Since: 2007/10/28


Hi

Im a php newbie

Im currently working on a simple directory module to show list of staff at my club house

is there any easy example module that can be my guide that implement its own search, with sort and pagination

i would like the data be display have pagination (30 records per page) and im able to sort it by asc/desc for all columns ..

and when doing search, pagination and sorting is also working

this i what i did so far..

excuse me for my newbie code..want to do this right way if there any easy xoops example out there...

will try to move this to using smarty template later


include("../../mainfile.php");
include(
XOOPS_ROOT_PATH."/header.php");

$meta_keywords ""._DIRECTORY_NAME."";
$meta_description ""._DIRECTORY_NAME."";
$pagetitle ""._DIRECTORY_NAME."";

if(isset(
$xoTheme) && is_object($xoTheme)) {
    
$xoTheme->addMeta'meta''keywords'$meta_keywords);
    
$xoTheme->addMeta'meta''description'$meta_description);
} else {    
// Compatibility for old Xoops versions
    
$xoopsTpl->assign('xoops_meta_keywords'$meta_keywords);
    
$xoopsTpl->assign('xoops_meta_description'$meta_description);
}

$xoopsTpl->assign('xoops_pagetitle'$pagetitle);

//this will only work if your theme is using this smarty variables
$xoopsTpl->assign'xoops_showlblock'1); //set to 0 to hide left blocks
$xoopsTpl->assign'xoops_showrblock'1); //set to 0 to hide right blocks
$xoopsTpl->assign'xoops_showcblock'1); //set to 0 to hide center blocks


$page_name="index.php"//  If you use this code with a different page ( or file ) name then change this 
$start=$_GET['start'];
if(
strlen($start) > and !is_numeric($start)){
echo 
"Data Error";
exit;
}
$eu = ($start 0); 
$limit 25;                                 // No of records to be shown per page.
$this1 $eu $limit
$back $eu $limit
$next $eu $limit

?>



<span style="color: #0000BB"><?php </span><span style="color: #007700">echo </span><span style="color: #DD0000">""</span><span style="color: #007700">.</span><span style="color: #0000BB">_DIRECTORY_NAME</span><span style="color: #007700">.</span><span style="color: #DD0000">""</span><span style="color: #007700">; </span><span style="color: #0000BB">?></span>








echo ""._DIR_SEARCHSTAF.""?> 
echo ""._DIR_SEARCH.""?>"/>

//if the search button is being clicked
if (isset($_POST['search']))  {
$_POST['search'] = mysql_real_escape_string($_POST['search']);  

//run the query for searching

global $xoopsDB
$resultmysql_query("SELECT * FROM ".$xoopsDB->prefix("directory_info")." where name like '%" .  $_POST['search']  .  "%' or unit   like '%" .  $_POST['search']  .  "%' or department like '%" .  $_POST['search']  .  "%'  or position like '%" .  $_POST['search']  .  "%' ORDER BY name");



 
$search_result=mysql_num_rows($result);
if (
$_POST['search'] =='')
{
echo 
" "._DIR_KEYWORD."
";
//exit();
}
else
{
echo 
" $search_result "._DIR_SEARCHRECORD.$_POST['search'] . " "._DIR_FOUND."
";
 } 
  }  
 else {

$result mysql_query("SELECT * FROM ".$xoopsDB->prefix("directory_info")." ORDER BY name limit $eu$limit");
$result2 mysql_query("SELECT * FROM ".$xoopsDB->prefix("directory_info")." ORDER BY name");

$nume=mysql_num_rows($result2);
echo 
" "._DIR_TOTAL.": $nume
";
//if not searching then display all data
}
echo 
"";
if(
$nume $limit ){ // Let us display bottom links if sufficient records are there for paging

/////////////// Start the bottom links with Prev and next link with page numbers /////////////////

//// if our variable $back is equal to 0 or more then only we will display the link to move back ////////

if($back >=0) { 

print 
"$page_name?start=$back'>« "._DIR_PREV.""


//////////////// Let us display the page links at  center. We will not display the current page as a link ///////////

$i=0;
$l=1;
for(
$i=0;$i $nume;$i=$i+$limit){
if(
$i <> $eu){
echo 
  • $page_name?start=$i'>$l
  •  "
    ;
    }
    else { echo 
    "$l";}        /// Current page is not displayed as link and given font color red
    $l=$l+1;
    }

    ///////////// If we are not in the last page then Next link will be displayed. Here we check that /////
    if($this1 $nume) { 
    print 
    "$page_name?start=$next'>"._DIR_NEXT."";



    }
    // end of if checking sufficient records are there to display bottom navigational link. 
     
    echo "";
    // check if records were returned
    if (mysql_num_rows($result) > 0)  {

    $countstaf = (isset($eu) && $eu 0) ? $eu+1;

    echo 
    "";


    echo 
    "No";
    echo 
    ""._DIR_NAME."";
    echo 
    ""._DIR_POSTION."";
    echo 
    ""._DIR_UNIT."";

    $int 0

    while(
    $row mysql_fetch_array($result))
    {


    $log_id=$row['log_id']; 


    echo 
    ', ++$int 'even' 'odd''">'
    echo 
    ""
    echo 
    $countstaf++;
    echo 
    "";
    echo 
    "" $row['name'] . "";
    echo 
    "" $row['position'] . " ";
    echo 
    "" $row['unit'] . "";
    echo 
    "";

    }
     echo 
    "";echo "";
    echo 
    "";
     
    /////////////////////////////// 
    if($nume $limit ){ // Let us display bottom links if sufficient records are there for paging

    /////////////// Start the bottom links with Prev and next link with page numbers /////////////////
    //// if our variable $back is equal to 0 or more then only we will display the link to move back ////////
    if($back >=0) { 
    print 
    "$page_name?start=$back'>« "._DIR_PREV.""

    //////////////// Let us display the page links at  center. We will not display the current page as a link ///////////
    $i=0;
    $l=1;
    for(
    $i=0;$i $nume;$i=$i+$limit){
    if(
    $i <> $eu){
    echo 
    "
  • $page_name?start=$i'>$l
  •  "
    ;
    }
    else { echo 
    "$l";}        /// Current page is not displayed as link and given font color red
    $l=$l+1;
    }



    ///////////// If we are not in the last page then Next link will be displayed. Here we check that /////
    if($this1 $nume) { 
    print 
    "$page_name?start=$next'>"._DIR_NEXT."";} 


    }
    // end of if checking sufficient records are there to display bottom navigational link. 
     
    echo ""
      }
      

    ?>




    include(XOOPS_ROOT_PATH."/footer.php");
    ?>

    the code is currently working where it will display data with pagination and searching is working but without paginantion

    what im trying to do right now is
    - display data/search with pagination/sorting asc&desc


    any xoops master can guide me ?

    2
    irmtfan
    Re: search, sort, pagination in XOOPS
    • 2013/4/7 12:54

    • irmtfan

    • Module Developer

    • Posts: 3419

    • Since: 2003/12/7


    You really should start by reading XoopsObject class located in XOOPS255/kernel/object.php
    That is the heart of any module to do anything using database include search,sort,order,pagination,...
    so you should stop writing these kind of codes:
    global $xoopsDB;  
    $resultmysql_query("SELECT * FROM ".$xoopsDB->prefix("directory_info")." where name like '%" .  $_POST['search']  .  "%' or unit   like '%" .  $_POST['search']  .  "%' or department like '%" .  $_POST['search']  .  "%'  or position like '%" .  $_POST['search']  .  "%' ORDER BY name");


    you should create the sql file for every table and start create a class for that table.
    I recommend to see publisher as a very profesional module.
    lets start with an example from publisher module.
    we want to see how we can write a class for table publisher_categories in sql file.
    at first you can see the table fields and structure in sql file:
    http://svn.code.sf.net/p/xoops/svn/XoopsModules/publisher/trunk/publisher/sql/mysql.php
    CREATE TABLE `PUBLISHER_categories` (
      `
    categoryidint(11NOT NULL auto_increment,
      `
    parentidint(11NOT NULL default '0',
      `
    namevarchar(100NOT NULL default '',
      `
    descriptiontext NOT NULL,
      `
    imagevarchar(255NOT NULL default '',
      `
    totalint(11NOT NULL default '0',
      `
    weightint(11NOT NULL default '1',
      `
    createdint(11NOT NULL default '1033141070',
      `
    templatevarchar(255NOT NULL default '',
      `
    headertext NOT NULL,
      `
    meta_keywordstext NOT NULL,
      `
    meta_descriptiontext NOT NULL,
      `
    short_urlvarchar(255NOT NULL default '',
      `
    moderatorint(6NOT NULL default '0',
      
    PRIMARY KEY (`categoryid`),
      
    KEY parentid (parentid)
    ENGINE=MyISAM;

    then see how to define the class:
    the filename is category.php
    http://svn.code.sf.net/p/xoops/svn/XoopsModules/publisher/trunk/publisher/class/category.php

    you can see how to define your fields:
    class PublisherCategory extends XoopsObject
    {
        
    /**
         * @var PublisherPublisher
         * @access public
         */
        
    public $publisher null;

        
    /**
         * @var array
         */
        
    public $_categoryPath false;

        
    /**
         * constructor
         */
        
    public function __construct()
        {
            
    $this->publisher PublisherPublisher::getInstance();
            
    $this->initVar("categoryid"XOBJ_DTYPE_INTnullfalse);
            
    $this->initVar("parentid"XOBJ_DTYPE_INTnullfalse);
            
    $this->initVar("name"XOBJ_DTYPE_TXTBOXnulltrue100);
            
    $this->initVar("description"XOBJ_DTYPE_TXTAREAnullfalse255);
            
    $this->initVar("image"XOBJ_DTYPE_TXTBOXnullfalse255);
            
    $this->initVar("total"XOBJ_DTYPE_INT1false);
            
    $this->initVar("weight"XOBJ_DTYPE_INT1false);
            
    $this->initVar("created"XOBJ_DTYPE_INTnullfalse);
            
    $this->initVar("template"XOBJ_DTYPE_TXTBOXnullfalse255);
            
    $this->initVar("header"XOBJ_DTYPE_TXTAREAnullfalse);
            
    $this->initVar("meta_keywords"XOBJ_DTYPE_TXTAREAnullfalse);
            
    $this->initVar("meta_description"XOBJ_DTYPE_TXTAREAnullfalse);
            
    $this->initVar("short_url"XOBJ_DTYPE_TXTBOXnullfalse255);
            
    $this->initVar("moderator"XOBJ_DTYPE_INTnullfalse0);
            
    //not persistent values
            
    $this->initVar("itemcount"XOBJ_DTYPE_INT0false);
            
    $this->initVar('last_itemid'XOBJ_DTYPE_INT);
            
    $this->initVar('last_title_link'XOBJ_DTYPE_TXTBOX);
            
    $this->initVar("dohtml"XOBJ_DTYPE_INT1false);
        }

        
    /**
         * @param string $method
         * @param array  $args
         *
         * @return mixed
         */
        
    public function __call($method$args)
        {
            
    $arg = isset($args[0]) ? $args[0] : null;
            return 
    $this->getVar($method$arg);
        }


    then another class by extending XoopsPersistableObjectHandler for access to table:
    /**
     * Categories handler class.
     * This class is responsible for providing data access mechanisms to the data source
     * of Category class objects.
     *
     * @author  marcan 
     * @package Publisher
     */
    class PublisherCategoryHandler extends XoopsPersistableObjectHandler
    {
        
    /**
         * @var PublisherPublisher
         * @access public
         */
        
    public $publisher null;

        
    /**
         * @param null|object $db
         */
        
    public function __construct(&$db)
        {
            
    $this->publisher PublisherPublisher::getInstance();
            
    parent::__construct($db"publisher_categories"'PublisherCategory'"categoryid""name");
        }


    reviewing publisher module would be enough for everybody.
    but you can see userlog as a simpler module.
    http://svn.code.sf.net/p/xoops/svn/XoopsModules/userlog/trunk/userlog/

    see i defined one class for each table
    userlog/class/log.php for mod_userlog_log table
    userlog/class/setting.php for mod_userlog_set table
    userlog/class/stats.php for mod_userlog_stats table

    I want to write a tutorial "how to write a standard module for xoops" based on what i learnt from publisher module.

    Login

    Who's Online

    266 user(s) are online (169 user(s) are browsing Support Forums)


    Members: 0


    Guests: 266


    more...

    Donat-O-Meter

    Stats
    Goal: $100.00
    Due Date: Nov 30
    Gross Amount: $0.00
    Net Balance: $0.00
    Left to go: $100.00
    Make donations with PayPal!

    Latest GitHub Commits