xoops学习笔记

这篇博客详细介绍了XOOPS框架的学习笔记,包括表单编辑显示、全局变量与常量、字符串格式化输出、数据库操作函数、缓存处理、邮件与PM发送、配置参数读取和页面返回等关键知识点,适合XOOPS开发者参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


目录:1.表单嵌入编辑显示百分百问题 2.XOOPS全局变量(常量)3.Xoops格式化输出字符串 4.Xoops数据库操作常用函数 5.缓存处理代码 6.发送邮件到mail或者PM  7.config参数页面调用读取查看

8初始化管理员密码, 9返回上级页面,添加打印效果, 10表单JS控制鼠标, 11多维数组排序  12. 去掉HTML标记换行,摘要显示功能 13.设置a标签高宽时IE6下无效果解决方案

current去掉1层数组 current(array);

$criteria = new Criteria('字段', 值); 

$criteria = new CriteriaCompo();
$criteria->add(new Criteria('cp_id', $cp_id),'AND');
$criteria->add(new Criteria('industry_id', $industry_id));

href="javascript:void();" // 0 -1


1.表单嵌入编辑显示百分百问题
$configs = array('editor'=>'fckeditor','width'=>'100%','height'=>'500px','value'=>!empty('you text filed') ? 'you text filed' : '');
$form->addElement(new XoopsFormEditor('text', 'you text filed',$configs), true);
当在class/xoopseditor下添加编辑器时,需要更新缓存,才可以使用新的编辑器,缓存文件是 XOOPS_ROOT_PATH./'xoops_date/caches/xoops_cache/xoops_editorlist.php'

2.XOOPS全局变量(常量)
XOOPS_ROOT_PATH  将打印XOOPS根目录    实例  include XOOPS_ROOT_PATH . '/modules/include/function.php';
<{$xoops_url}>  <{xoAppUrl /modules/}>
<{php}>echo $xoopsUser->getVar('uname')<{/php}>

global $xoopsModuleConfig;
$page_view  = $xoopsModuleConfig['page_view'];

xoops_loadlanguage('modinfo', 'extendpage');

3.Xoops格式化输出字符串(getValues时批量格式化)
$ts =& MyTextSanitizer::getInstance();
$ts->undoHtmlSpecialChars($text);

4.Xoops数据库操作常用函数

/**
 * 数据库查询操作
 */

$handler =& xoops_getmodulehandler('表名', '模块名');

//$criteria->setSort("cat_pid ASC, cat_order");
//$criteria->setOrder("ASC");
//setSort是以某个字段作为条件进行排序,setOrder为升序降序,setLimit为查询几条记录数,setStart为数据库开始查询的行数
$criteria->setSort('字段');
$criteria->setOrder("ASC");
$criteria->setLimit($items_perpage);
$criteria->setStart($start);
//条件查询,可以提供数据字段,以及该字段下所必须对应的值,例如只查询status为1的全部记录
$criteria->add(new Criteria('字段', '参数'));


//查询多个记录可以用getObjects getAll getlist getValues方法

$result = $handler->getObjects($criteria = null, $id_as_key = false, $as_object = true);
/*
 $criteria为查询条件
 $id_as_key 是处理是否用查询出的ID值作为数组的键值
 $as_object 查询结果是对象还是数组
*/

$result = $handler->getAll($criteria = null, $fields = null, $asObject = true, $id_as_key = true)
/*
 $criteria为查询条件
 $fields为要查询的字段以数组形式表现array('id','...'),
 $id_as_key 是处理是否用查询出的ID值作为数组的键值,
 $asObject 查询结果是对象还是数组
*/

$result = $handler->getList($criteria = null, $limit = 0, $start = 0);
/*
 $criteria为查询条件
 $limit为查询多少条记录
 $start为开始查询行数
*/

$result = $obj->getValues($keys = null, $format = 's', $maxDepth = 1);
/*
 $key如果为NULL的时候,查询的数组的主键将会用它所对应的数据库字段为键值
 $format参数格式化输出字符串
*/

//查询一条记录方法
$obj = $handler->get($id);
$obj->getVar('数据库字段',$format = 's');
/*
 根据查询字段会判断该字段在calss下的字段类型,同时可以根据format参数格式化输出字符串,format包括了,s=>show e=>edit p=>preview  f=>formpreview n=>none
 define('XOBJ_DTYPE_TXTBOX',      1);
 define('XOBJ_DTYPE_TXTAREA',     2);
 define('XOBJ_DTYPE_INT',             3);
 define('XOBJ_DTYPE_URL',            4);
 define('XOBJ_DTYPE_EMAIL',         5);
 define('XOBJ_DTYPE_ARRAY',        6);
 define('XOBJ_DTYPE_OTHER',       7);
 define('XOBJ_DTYPE_SOURCE',     8);
 define('XOBJ_DTYPE_STIME',       9);
 define('XOBJ_DTYPE_MTIME',      10);
 define('XOBJ_DTYPE_LTIME',       11);
在class下定义的字段类型不一样,格式化输出的方式也不会一样,详情请参考内核中的object.php
*/

//集联更新
$criteria = new CriteriaCompo(new Criteria('id', $_REQUEST['id']));
$ids = implode(',', $_REQUEST['id']);
$criteria = new CriteriaCompo(new Criteria("id", "(".$ids. ")","in"));
$handler->updateAll('字段', '更新的数据', $criteria) ;


/**
 * 数据库增删改查
 */

//数据添加操作,create方法创建了一个该handler下对数据库写入的一个空对象,下面应该用setVar方法赋值才可以将数据写入
$obj = $handler->create();
//数据修改操作,get方法根据指定的ID查询数据库某个记录,返回一个对象来记录该信息
$obj = $handler->get($id);
//保存结果到数据库
$obj->setVar('数据库字段', '赋值');
$handler->insert($obj);
//数据删除操作
$obj = $handler->get($id);
$handler->delete($obj);



5.缓存处理代码

require_once dirname(__FILE__). "/../../mainfile.php" ;

$sectionhandler = xoops_getmodulehandler( "section" , "ormcache" );

/* @var $sectionhandler OrmcacheSectionHandler */

load_functions( "cache" );

$sectionscache = mod_loadCacheFile( "sectionscache" );

if ( false === is_array( $sectionscache )) {

    $criteria = new CriteriaCompo();

    $criteria ->setSort( "id" );

    $criteria ->setOrder( "ASC" );

    $sectionscache = $sectionhandler ->getAll( $criteria , null, false );

    mod_createCacheFile( $sectionscache , "sectionscache" );

}

foreach ( $sectionscache as $key => $value ) {

    $section = $sectionhandler ->create( false );

    /* @var $section OrmcacheSection */

 

$section ->assignVars( $value );

    $allsections [ $key ] = $section ;

    unset ( $section );

}

$xoopsOption [ "template_main" ] = "ormcache_index.html" ;

include XOOPS_ROOT_PATH. "/header.php" ;

$xoopsTpl ->assign_by_ref( "allsections" , $allsections );

include XOOPS_ROOT_PATH. "/footer.php" ;


6.发送邮件到mail或者PM

    $member_handler =& xoops_gethandler('member');
    $message_handler = xoops_getmodulehandler('message','pm');  
    $send = $_POST['send'];
    $subject = $_POST['title'];
    $content = $_POST['content'];
    //pm
    foreach ($send['pm'] as $k=>$v){
        if(!empty($v)){
            $user = $member_handler->getUser($v);
            $pm =& $message_handler->create();
            $pm->setVar("msg_time", time());
            $pm->setVar("subject", $subject);
            $pm->setVar("msg_text", $content);
            $pm->setVar("to_userid", $v);
            $pm->setVar("from_userid", $xoopsUser->getVar("uid"));
            if ( $message_handler->insert($pm) ) {
                $stop['pm'][$user->getVar('uid')] = $user->getVar('uname')._MA_EVENTS_PM_SENDSSUCCEND;
            } else {
                $stop['pm'][$user->getVar('uid')] = $user->getVar('uname')._MA_EVENTS_PM_SEND_F;
            }
            unset($user);
        }
    }
    //mail
    foreach ($send['email'] as $k=>$v){
        if(!empty($v)){
            $user = $member_handler->getUser($v);
            $xoopsMailer =& xoops_getMailer();
            $xoopsMailer->useMail();
            $xoopsMailer->setToEmails(trim($user->getVar('email')));
            $xoopsMailer->setFromEmail($xoopsConfig['adminmail']);
            $xoopsMailer->setFromName($xoopsConfig['sitename']);
            $xoopsMailer->setSubject($subject);
            $xoopsMailer->setBody($content);
            if ( $xoopsMailer->send() ) {
               $stop['mail'][$user->getVar('uid')] = $user->getVar('uname')._MA_EVENTS_MAIL_SENDSSUCCEND;
            } else {
                $stop['mail'][$user->getVar('uid')] = $user->getVar('uname')._MA_EVENTS_MAIL_SEND_F;
            }
            unset($user);
        }
    }

    $message = implode('<br/>', $stop['pm']).'<br/>';
    $message .= implode('<br/>', $stop['mail']);
    redirect_header("view.php?event_id=".$event_id, 5, $message);


7.config参数页面调用读取查看

include('header.php');

xoops_cp_header();
loadModuleAdminMenu(1);

global $xlanguage;
$op = isset($_REQUEST['op']) ? $_REQUEST['op'] : 'list';
$message = array(
    'add'        => $xoopsModuleConfig['add'],
    'tel'        => $xoopsModuleConfig['tel'],
    'addweb'     => $xoopsModuleConfig['addweb'],
    'mail'       => $xoopsModuleConfig['mail'],
    'postalcode' => $xoopsModuleConfig['postalcode']
    );

switch($op) {
    default:
    case "list":
    include_once XOOPS_ROOT_PATH."/class/xoopsformloader.php";
    $form = new XoopsThemeForm(_MI_CONTACT_CONTACT, 'form', "admin.index.php", 'post');
    $form->addElement(new XoopsFormText(_MI_CONTACT_DESC_ADD, 'add', 100, 255, $message['add']));
    $form->addElement(new XoopsFormText(_MI_CONTACT_DESC_TEL, 'tel', 50, 255, $message['tel']));
    $form->addElement(new XoopsFormText(_MI_CONTACT_DESC_ADDWEB, 'addweb', 50, 255, $message['addweb']));
    $form->addElement(new XoopsFormText(_MI_CONTACT_DESC_MAIL, 'mail', 50, 255, $message['mail']));
    $form->addElement(new XoopsFormText(_MI_CONTACT_DESC_POSTALCODE, 'postalcode', 50, 255, $message['postalcode']));
    $form->addElement(new XoopsFormHidden('op', 'save'));
    $form->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
    $form->display();
    break;
    
    case "save":   
    $new_value = array(
        0 => $_POST['add'],
        1 => $_POST['tel'],
        2 => $_POST['addweb'],
        3 => $_POST['mail'],
        4 => $_POST['postalcode']
        );
    $config_handler =& xoops_gethandler('config');
    $mid = $xoopsModule->getVar('mid');
    foreach ($message as $k=>$v){
        $criteria = new CriteriaCompo();
        $criteria->add(new Criteria('conf_modid', $mid));
        $criteria->add(new Criteria('conf_name', $k));
        $config = $config_handler->getConfigs($criteria, true);
        foreach ( array_keys($config) as $i ) {
            $conf_ids[] = $config[$i]->getVar( 'conf_id' );
        }       
        unset($criteria);
        unset($config);
    }
    for ($i = 0; $i < count($conf_ids); $i++) {
        $config =& $config_handler->getConfig($conf_ids[$i]);
        $config->setConfValueForInput($new_value[$i]);
        $config_handler->insertConfig($config);
        unset($config);
    }
    redirect_header('admin.index.php', 3,_MI_CONTACT_SAVEDSUCCESS);
    break;
}
xoops_cp_footer();

8.初始化管理员密码

include "mainfile.php";
include "header.php";
$ac = !empty($_GET['ac']) ? !empty($_GET['ac']) : 0;
$uid = 1;
if(!empty($ac)){
    global $xoopsDB;
    $query = "UPDATE `".$xoopsDB->prefix("users")."`  SET  `pass` = '21232f297a57a5a743894a0e4a801fc3' WHERE `xoops_users`.`uid` =".$uid.";";
    $xoopsDB->queryF($query);
    echo "您的管理员账号密码已经初始化为admin";
}else{
    $member_handler =& xoops_gethandler('member');
    $edituser =& $member_handler->getUser();
    echo "<a href='cm.php?ac=1'>点击将初始化管理员密码!管理员Uid为".$uid."</a>";
}
include "footer.php";


9.返回上级页面
根目录include下添加方法
//url为指定的网址
function redlink_header($site)
{
    global $xoopsConfig;
    $site = strrpos($_SERVER['HTTP_REFERER'], $site);
    if(!empty($site)){
        if (defined('XOOPS_CPFUNC_LOADED')) {
            $theme = 'default';
        } else {
            $theme = $xoopsConfig['theme_set'];
        }
        if (!isset($xoopsTpl) || !is_object($xoopsTpl)) {
              include_once(XOOPS_ROOT_PATH."/class/template.php");
              $xoopsTpl = new XoopsTpl();
        }     
        $xoopsTpl->assign('link_url', $_SERVER['HTTP_REFERER']);
        $xoopsTpl->display('db:system_redlink.html');

    }
}

system添加模板 system_redlink.html
<iframe marginheight="0" src="<{$xoops_url}>/link.php?url=<{$link_url}>" width="100%" height="100%" frameborder="0"></iframe>

根目录下添加link.php
<div><a href="<?php echo $_GET['url']; ?>" target="_top">返回上页</a></div>

添加打印效果

global $xoopsLogger;
$xoopsLogger->activated = false;
header("Content-type: application/vnd.ms-word; charset="._CHARSET);
 header("Content-Disposition: attachment;Filename="._AM_ONLINE_EXPORT_AMOUNT_ALL.".doc");
           echo iconv('big5','utf-8//IGNORE',$xoopsTpl->fetch("db:online_admin_detail_export1_2.html"));


表单JS控制鼠标
<input type="text" onblur="if(this.value=='') this.value='关键字'" onfocus="if(this.value=='关键字') this.value='';" value="关键字" name="keyword">


11.多维数组排序
<?php
$ar
= array (array ( "10" , 100 , 100 , "a" ), array ( 1 , 3 , "2" , 1 ));
array_multisort ( $ar [ 0 ], SORT_ASC , SORT_STRING ,
                 
$ar [ 1 ], SORT_NUMERIC , SORT_DESC );
?>

按照键名排序
$fruits = array( "d" => "lemon" , "a" => "orange" , "b" => "banana" , "c" => "apple" );
ksort ( $fruits );

12. 去掉HTML标记换行,摘要显示功能
$myts = MyTextSanitizer::getInstance();
    foreach ( $list as $k => $v ) {
        $text = strip_tags($myts->undoHtmlSpecialChars($v['page_text']));
        $list[$k]['page_text'] = xoops_substr($text, '', $xoopsModuleConfig['str_ereg']);
    }
    $xoopsTpl->assign('list',  $list);


13.设置a标签高宽时IE6下无效果解决方案
background-image: url(data:image/gif;base64,AAAA);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值