Subject:*
<
Name/Email:*
<
Message Icon:*
<
Select*
<
Message:*
<



Click the Preview to see the content in action.
Options:*
<
Confirmation Code*
<
0 + 0 = ?  
Input the result from the expression
Maximum attempts you can try: 10
*
<
   

Re: search, sort, pagination in XOOPS
by irmtfan on 2013/4/7 12:54:19

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:
le="color: #000000"><?php global $xoopsDB; $result= mysql_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
le="color: #000000"><?php CREATE TABLE `PUBLISHER_categories` ( `categoryid` int(11) NOT NULL auto_increment, `parentid` int(11) NOT NULL default '0', `name` varchar(100) NOT NULL default '', `description` text NOT NULL, `image` varchar(255) NOT NULL default '', `total` int(11) NOT NULL default '0', `weight` int(11) NOT NULL default '1', `created` int(11) NOT NULL default '1033141070', `template` varchar(255) NOT NULL default '', `header` text NOT NULL, `meta_keywords` text NOT NULL, `meta_description` text NOT NULL, `short_url` varchar(255) NOT NULL default '', `moderator` int(6) NOT 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:
le="color: #000000"><?php 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_INT, null, false); $this->initVar("parentid", XOBJ_DTYPE_INT, null, false); $this->initVar("name", XOBJ_DTYPE_TXTBOX, null, true, 100); $this->initVar("description", XOBJ_DTYPE_TXTAREA, null, false, 255); $this->initVar("image", XOBJ_DTYPE_TXTBOX, null, false, 255); $this->initVar("total", XOBJ_DTYPE_INT, 1, false); $this->initVar("weight", XOBJ_DTYPE_INT, 1, false); $this->initVar("created", XOBJ_DTYPE_INT, null, false); $this->initVar("template", XOBJ_DTYPE_TXTBOX, null, false, 255); $this->initVar("header", XOBJ_DTYPE_TXTAREA, null, false); $this->initVar("meta_keywords", XOBJ_DTYPE_TXTAREA, null, false); $this->initVar("meta_description", XOBJ_DTYPE_TXTAREA, null, false); $this->initVar("short_url", XOBJ_DTYPE_TXTBOX, null, false, 255); $this->initVar("moderator", XOBJ_DTYPE_INT, null, false, 0); //not persistent values $this->initVar("itemcount", XOBJ_DTYPE_INT, 0, false); $this->initVar('last_itemid', XOBJ_DTYPE_INT); $this->initVar('last_title_link', XOBJ_DTYPE_TXTBOX); $this->initVar("dohtml", XOBJ_DTYPE_INT, 1, false); } /** * @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:
le="color: #000000"><?php /** * Categories handler class. * This class is responsible for providing data access mechanisms to the data source * of Category class objects. * * @author marcan <marcan@notrevie.ca> * @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.
search, sort, pagination in XOOPS
by sarahmx on 2013/4/7 5:51:11

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

<?php 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) > 0 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; ?>  <html> <head> <title><?php echo ""._DIRECTORY_NAME.""; ?></title> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body> <form method="post" action="" > <br /><?php echo ""._DIR_SEARCHSTAF.""; ?> <input type="text" name="search" /> <input type="submit" value="<?php echo ""._DIR_SEARCH.""; ?>"/><br /> <?php //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; $result= mysql_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 "<div style='text-align:right;padding:5px;color:#CB1D1D;font-weight:bold;'><img src='message.gif'> "._DIR_KEYWORD."</div>"; //exit(); } else { echo "<div style='text-align:right;padding:5px;color:#000;'><img src='message.gif'> $search_result "._DIR_SEARCHRECORD." <b>" . $_POST['search'] . "</b> "._DIR_FOUND."</div>"; } } 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 "<div style='text-align:right;padding:5px;'><img src='message.gif'> "._DIR_TOTAL.": $nume<br /></div>"; //if not searching then display all data } echo "<ul id='pagination-digg'>"; 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 "<li class='previous-off'><a href='$page_name?start=$back'>« "._DIR_PREV."</a></li>"; } //////////////// 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 " <li><a href='$page_name?start=$i'>$l</a></li> "; } else { echo "<li class='active'>$l</li>";} /// 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 "<li class='next'><a href='$page_name?start=$next'>"._DIR_NEXT."</a></li>"; } }// end of if checking sufficient records are there to display bottom navigational link. echo "</ul>"; // check if records were returned if (mysql_num_rows($result) > 0) { $countstaf = (isset($eu) && $eu > 0) ? $eu+1 : 1; echo "<table id='directorystaf'><tr>"; echo "<th>No</th>"; echo "<th>"._DIR_NAME."</th>"; echo "<th>"._DIR_POSTION."</th>"; echo "<th>"._DIR_UNIT."</th>"; $int = 0; while($row = mysql_fetch_array($result)) { $log_id=$row['log_id']; echo '<tr class="', ++$int & 1 ? 'even' : 'odd', '">'; echo "<td>"; echo $countstaf++; echo "</td>"; echo "<td>" . $row['name'] . "</td>"; echo "<td>" . $row['position'] . " </td>"; echo "<td>" . $row['unit'] . "</td>"; echo "</tr>"; } echo "</table>";echo ""; echo "<ul id='pagination-digg'>"; /////////////////////////////// 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 "<li class='previous-off'><a href='$page_name?start=$back'>« "._DIR_PREV."</a></li>"; } //////////////// 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 "<li><a href='$page_name?start=$i'>$l</a></li> "; } else { echo "<li class='active'>$l</li>";} /// 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 "<li class='next'><a href='$page_name?start=$next'>"._DIR_NEXT."</a></li>";} }// end of if checking sufficient records are there to display bottom navigational link. echo "</ul>"; } ?>  </body> </html> <?php 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 ?

Who's Online

803 user(s) are online (760 user(s) are browsing Support Forums)


Members: 0


Guests: 803


more...

Donat-O-Meter

Stats
Goal: $15.00
Due Date: Jul 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $15.00
Make donations with PayPal!

Latest GitHub Commits