在做ECSHOP的时候,想在模板里直接调用数据库,如何实现呢?问了很多人说要在HTML里写PHPSQL如下SELECT attr_value FROM ecs_goods_attr  WHERE attr_id = 1 AND goods_id = 32怎么才能在HTML里调用呢?

解决方案 »

  1.   

    确实是ecshop,呵呵
    普通的PHP就行,感觉用了smarty好像还更麻烦
    普通的php查询SQL然后输出就行了
    实在没有PHP知识。只会前端和基础SQL,真是惭愧啊,求各位大哥指教
      

  2.   

    插件的方式或者用assign具体操作,请查看手册
      

  3.   

    如在php中(ecshop中用的好像是smarty吧,忘了):
    function insert_execSQL($param){
    //echo '<pre>';print_r($param);exit;//输出模板中insert传过来的所有参数,你可以进行相当的处理
    return $param['a'];
    }在smarty模板中类似如下:
    {insert name="execSQL" a=1}//“{}”是边界符,a=1传递给php中的insert_execSQL的另一个参数,最终输出1,以此类推在php中的insert_execSQL中你向执行什么不行呢,smarty中你想传过去什么参数传递不过去呢
      

  4.   

    直接写 <?php //代码 ?> 啊, php怎么写就怎写, 他模板是html, 不过他会生成php缓存ecshop\temp\compiled\index.dwt.php 引用时是include执行代码的他模板应该是解析型,不是替换型的....
      

  5.   

    不行,发现在模板里怎么写PHP都会解析错误,现在在PHP里直接更改,在lib_goods.php中有如下一段
            $sql = 'SELECT g.goods_id, g.goods_name, g.goods_name_style, g.et_price, g.shop_price AS org_price, g.promote_price, ' .
                    "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, ".
                    "promote_start_date, promote_end_date, g.goods_brief, g.goods_thumb, g.goods_img, RAND() AS rnd " .
                    'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
                    "LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp ".
                    "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' ";
            $type_merge = array_merge($type_array['new'], $type_array['best'], $type_array['hot']);
            $type_merge = array_unique($type_merge);
            $sql .= ' WHERE g.goods_id ' . db_create_in($type_merge);
            $sql .= ' ORDER BY g.sort_order, g.last_update DESC';        $result = $GLOBALS['db']->getAll($sql);
            foreach ($result AS $idx => $row)
            {
                if ($row['promote_price'] > 0)
                {
                    $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
                    $goods[$idx]['promote_price'] = $promote_price > 0 ? price_format($promote_price) : '';
                }
                else
                {
                    $goods[$idx]['promote_price'] = '';
                }            $goods[$idx]['id']           = $row['goods_id'];
                $goods[$idx]['name']         = $row['goods_name'];
                $goods[$idx]['brief']        = $row['goods_brief'];
                $goods[$idx]['brand_name']   = isset($goods_data['brand'][$row['goods_id']]) ? $goods_data['brand'][$row['goods_id']] : '';
                $goods[$idx]['goods_style_name']   = add_style($row['goods_name'],$row['goods_name_style']);            $goods[$idx]['short_name']   = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
                                                   sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
                $goods[$idx]['short_style_name']   = add_style($goods[$idx]['short_name'],$row['goods_name_style']);
                $goods[$idx]['et_price'] = price_format($row['et_price']);
                $goods[$idx]['shop_price']   = price_format($row['shop_price']);
                $goods[$idx]['thumb']        = get_image_path($row['goods_id'], $row['goods_thumb'], true);
                $goods[$idx]['goods_img']    = get_image_path($row['goods_id'], $row['goods_img']);
                $goods[$idx]['url']          = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);我想在$goods[$idx]['id']           = $row['goods_id'];加入$goods[$idx]['pro_1'],想让$goods[$idx]['pro_1']为查询SQL的语句的运行结果
    SQL如下,该怎么写这句呢?SELECT attr_value FROM ecs_goods_attr WHERE attr_id = 1 AND goods_id = 32
    $goods[$idx]['id']           = ??????????
      

  6.   

    $goods[$idx]['id'] = $GLOBALS['db']->getOne($sql);//好像getOne()有这个方法吧
      

  7.   

    $sql='SELECT attr_value FROM ecs_goods_attr WHERE attr_id = 1 AND goods_id = 32';
    $goods[$idx]['pro_1']          = $GLOBALS['db']->getOne($sql);这样写?出不来啊?
      

  8.   

    感谢kyzy_yy_pm$sql='SELECT attr_value FROM ecs_goods_attr WHERE attr_id = 1 AND goods_id = 32';
    $goods[$idx]['pro_1'] = $GLOBALS['db']->getOne($sql);这样写可以了。但是还需要把SQL里的最后一个条件goods_id = 32,换成goods_id = $goods[$idx]['id']           = $row['goods_id']; goods_id为当前的goods_id,怎么换呢?
      

  9.   

    getOne()//这个有么,没有加进去一个,或者改成getRow()这个有吧,没有的话用getAll();
      

  10.   

    SQL这句写成这样吗?$sql="SELECT attr_value FROM ecs_goods_attr WHERE attr_id = 1 AND goods_id = getOne($goods[$idx]['id'] )";这样写报错啊……我想在SQL里goods_id这个是获取当前的goods_id,就像在模板里用{$goods.id}一样SQL那个地方换成什么呢?goods_id = getOne($goods[$idx]['id'] ) 这样写报错啊
      

  11.   

    SQL这句写成这样吗?$sql="SELECT attr_value FROM ecs_goods_attr WHERE attr_id = 1 AND goods_id = getOne($goods[$idx]['id'] )";
    是这样:
    $sql="SELECT attr_value FROM ecs_goods_attr WHERE attr_id = 1 AND goods_id = '.$goods[$idx]['id']);
    $res = getOne($sql);//此时$res是goods_id = $goods[$idx]['id']的商品的attr_value
      

  12.   

    $sql="SELECT attr_value FROM ecs_goods_attr WHERE attr_id = 1 AND goods_id = ".$goods[$idx]['id']);
      

  13.   

    $sql="SELECT attr_value FROM ecs_goods_attr WHERE attr_id = 1 AND goods_id = ".$goods[$idx]['id']);这句在编辑器里提示有语法错误运行报错Parse error: syntax error, unexpected ')' in \includes\lib_goods.php on line 329嗯,掉了一个(,我改成这样
    $sql="SELECT attr_value FROM ecs_goods_attr WHERE attr_id = 1 AND goods_id = ".($goods[$idx]['id']);成功了,太感谢了,太感谢了,太感谢了
      

  14.   

    没用过这些什么smart模板,jQuery模板,没接触过,见过但是看不懂。 伤心。