您现在的位置是:首页 > 程序 > php网站首页php

Ecshop后台分页加搜索总结

  • 袁志蒙
  • 2017-06-19 18:59:33
  • 2502 次阅读
最近由于工作的原因,不得不对ECShop进行研究和二次开发,记录一下笔记文档。方式调用的,所以对于刚接触该系统的人在研究这个功能时,往往比较费力。。。看了好几天总结一下步骤:1.先写一个functio...

最近由于工作的原因,不得不对ECShop进行研究和二次开发,记录一下笔记文档。

由于Ecshop后台分页和搜索是用ajax方式调用的,所以对于刚接触该系统的人在研究这个功能时,往往比较费力。。。看了好几天总结一下步骤:


1.先写一个function get_page_list() 函数,里面要包括可以进行排序,分页,查询等功能

2.在php文件里面写一个elseif ($_REQUEST['act'] == 'list') ,这里是显示默认分页数据

3.在php写一个elseif($_REQUEST['act'] == 'query')的逻辑,(listtable.js默认的是先读取$_REQUEST['act'] == 'query')排序、分页、查询都在query里面完成

4.html页面


实例:

1.先写一个function get_page_list,可以进行排序、分类、查询。


/**
 * 获取分页列表
 *
 * @access  public
 * @return  array
 */
function get_page_list(){
	
	//获取上一次保存的查询条件
    $result = get_filter();
	
    if ($result === false){
		
		//搜索条件
		$where = ' where 1=1 ';
		
        $filter = array();
		
		//ajax搜索功能
		if (isset($_REQUEST['is_ajax']) && $_REQUEST['is_ajax'] == 1){
			$is_import = isset($_REQUEST['is_import']) ? intval($_REQUEST['is_import']) : 99;
			$keyword = isset($_REQUEST['keyword']) ? trim($_REQUEST['keyword']) : '';
            $filter['is_import'] = $is_import;
            $filter['keyword'] = json_str_iconv($keyword);
			
			if($is_import != 99) $where .= "and is_import = $is_import ";
			if(!empty($filter['keyword'])) $where .= "and goods_name like '%$keyword%' ";
        }
		
		/* 记录总数以及页数 */
		$sql = "SELECT COUNT(*) FROM ".$GLOBALS['cy']->table('jz_goods').$where;
		$filter['record_count'] = $GLOBALS['db']->getOne($sql);
		
		//分页大小 
        $filter = page_and_size($filter);  //处理后$filter数组里有:record_count,page_size,page,page_count,start
		
        /* 查询记录 */
        $sql = "SELECT * FROM ".$GLOBALS['cy']->table('jz_goods').$where." ORDER BY id DESC  LIMIT " . $filter['start'] . ',' . $filter['page_size'];;
		
		//保存查询条件(cookie保存)
        set_filter($filter, $sql);
    }else{
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
	
    $depot_list = $GLOBALS['db']->getAll($sql);
	
	//组合成系统默认查询条件
    return array('depot_list' => $depot_list, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}

2.php列表文件:


elseif ($_REQUEST['act'] == 'list'){
	$smarty->assign('ur_here',      '测试');
	$smarty->assign('full_page',    1);
	$page_list = get_page_list();
    $smarty->assign('data',   		$page_list['depot_list']);
    $smarty->assign('filter',       $page_list['filter']);
    $smarty->assign('record_count', $page_list['record_count']);
    $smarty->assign('page_count',   $page_list['page_count']);
    assign_query_info();

    $smarty->display('import_list.htm');
}
/*------------------------------------------------------ */
//-- 排序、分页、查询
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'query')
{
	
	$page_list = get_page_list();
    $smarty->assign('data',   		$page_list['depot_list']);
    $smarty->assign('filter',       $page_list['filter']);
    $smarty->assign('record_count', $page_list['record_count']);
    $smarty->assign('page_count',   $page_list['page_count']);
	
    make_json_result($smarty->fetch('import_list.htm'), '',array('filter' => $page_list['filter'], 'page_count' => $page_list['page_count']));
}

3.HTML模板文件:


{if $full_page}
{include file="pageheader.htm"}
{insert_scripts files="../js/utils.js,listtable.js"}
{include file="goods_search_other.htm"}
<form method="post" action="" name="listForm">
<div class="list-div" id="listDiv">
{/if}

  <table cellpadding="3" cellspacing="1"  class="table table-bordered">
    <tr>
      <th><input onclick='listTable.selectAll(this, "checkboxes")' type="checkbox"></th>
      <th>商品图片</th>
      <th>商品名称</th>
      <th>商品ID</th>
      <th>品牌名称</th>
      <th>分类名称</th>
      <th>销售价</th>
      <th>库存数</th>
      <th>已导入</th>
      <th>操作</th>
    </tr>
    {foreach from=$data item=goods}
    <tr>
      <td><input value="{$goods.id}" name="checkboxes[]" type="checkbox"></td>
      <td><img src="{$goods.goods_img}" height="70"></td>
      <td>{$goods.goods_name}</td>
      <td>{$goods.goods_id}</td>
      <td>{$goods.brand_name}</td>
      <td>{$goods.cate_name}</td>
      <td>{$goods.sell_price}</td>
      <td>{$goods.store_nums}</td>
      <td>{if $goods.is_import eq 1}是{else}<span style="color:red">否</span>{/if}</td>
      <td align="center">
        <a href="javascript:;" onclick="remove_jz({$goods.id})" title="删除">删除</a> 
      </td>
    </tr>
    {foreachelse}
    <tr><td class="no-records" colspan="10">{$lang.no_records}</td></tr>
    {/foreach}
	
    <tr>
      <td align="right" nowrap="true" colspan="6">
      {include file="page.htm"}
      </td>
    </tr>
  </table>

{if $full_page}
</div>
</form>

<script type="text/javascript" language="javascript">

  listTable.recordCount = {$record_count};
  listTable.pageCount = {$page_count};

  {foreach from=$filter item=item key=key}
  listTable.filter.{$key} = '{$item}';
  {/foreach}


  function remove_jz(id){
      if(confirm('确定删除吗?')){
          window.location.href = "jiangzi.php?act=remove&id="+id;
      }
  }
</script>
{include file="pagefooter.htm"}
{/if}


goods_search_other.htm文件:

<div class="form-div">
  <form action="javascript:searchGoods()" name="searchForm" >
    <img src="images/icon_search.gif" width="26" height="22" border="0" alt="SEARCH" />
	{if $smarty.get.act neq "trash"}
      <select name="is_import"><option value='99'>是否导入</option><option value="1">是</option><option value="0">否</option></select>
    {/if}
	  商品名称 <input type="text" name="keyword" size="15" />
      <input type="submit" value="{$lang.button_search}" class="button" />
  </form> 
</div>

{literal}
<script language="JavaScript">
    function searchGoods()
    {
{/literal}
        {if $smarty.get.act neq "trash"}
        //listTable.filter['cat_id'] = document.forms['searchForm'].elements['cat_id'].value;
        listTable.filter['is_import'] = document.forms['searchForm'].elements['is_import'].value;
        {/if}
{literal}
        listTable.filter['keyword'] = Utils.trim(document.forms['searchForm'].elements['keyword'].value);
        listTable.filter['page'] = 1;

        listTable.loadList();
    }
</script>
{/literal}

说明:{literal}{/literal}是smarty标签,当smarty的边界符定义为{} (大括号)的时候~{literal}{/literal}是用来忽略该标签内的大括号的。

TAG: 无标签

文章评论 (0)

    • 这篇文章还没有收到评论,赶紧来抢沙发吧~


Top