phpcms广告按不同地区显示的效果实现

1.       数据库执行以下sql语句,以记录需要的字段值;

ALTER TABLE `phpcms_ads` ADD `areaid` SMALLINT( 5 ) UNSIGNED NOT NULL DEFAULT '0' COMMENT '广告所属地区id',
ADD `ischild` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0' COMMENT '
是否显示到下级子地区';
ALTER TABLE `phpcms_ads` ADD INDEX ( `areaid` ) ;

 

2.       找到 ,在合适的位置增加如下代码:

<tr>

         <th><strong>显示地区</strong></th>

         <td><input type="hidden" name="ads[areaid]" id="areaid" value="">

               <span id="load_areaid"></span>

               <a href="javascript:areaid_reload();"> &nbsp;重选</a>

<script type="text/javascript">

function areaid_load(id)

{

$.get("load.php?"+Math.floor(Math.random()*1000), { field: 'areaid', id: id },

function(data){

$('#load_areaid').append(data);

  });

}

function areaid_reload()

{

$('#load_areaid').html('');

areaid_load(0);

document.getElementById('areaid').value = '';

}

areaid_reload();

               </script> </td>

     </tr>

<tr>

         <th><strong>是否下级显示</strong></th>

         <td><input type='radio' name='ads[ischild]' value='1' checked> <input type='radio' name='ads[ischild]' value='0'>   注意:选择是将在下级地区显示;选择否则仅仅显示在选中地区</td>

     </tr>

 

 

3.       找到 ,在适当的位置增加以下代码:

<tr>

         <th><strong>显示地区</strong></th>

         <td><input type="hidden" name="ads[areaid]" id="areaid" value="<?php echo $_ads['areaid'];?>">

               <span id="load_areaid"><?php echo $_ads['areaid'] ? areaname($_ads['areaid']) : '';?></span>

               <a href="javascript:areaid_reload();"> &nbsp;重选</a>

<script type="text/javascript">

function areaid_load(id)

{

$.get("load.php?"+Math.floor(Math.random()*1000), { field: 'areaid', id: id },

function(data){

$('#load_areaid').append(data);

  });

}

function areaid_reload()

{

$('#load_areaid').html('');

areaid_load(0);

document.getElementById('areaid').value = '';

}

               </script> </td>

     </tr>

<tr>

         <th><strong>是否下级显示</strong></th>

         <td><input type='radio' name='ads[ischild]' value='1' <?php echo $_ads['ischild']?'checked':'';?>> <input type='radio' name='ads[ischild]' value='0' <?php echo !$_ads['ischild']?'checked':'';?>>   注意:选择是将在下级地区显示;选择否则仅仅显示在选中地区;如果想显示到湖北省以下全部地区,请直接选择湖北即可!</td>

     </tr>

 

 

4.       找到 ,删除函数 show(),改写成:

function show($placeid,$areaid)

{

        global $_username;

        $placeid = intval($placeid);

        if(!$placeid) return FALSE;

        $ip = IP;

        $time = time();

$areaid = intval($areaid);

$AREA = cache_read('area_'.$areaid.'.php');

$areastr = $areaid?" AND a.areaid IN( ".$AREA['arrparentid'].",$areaid )":'';//地区为0,全国性广告,可随意显示;

        $adses = $this->db->select("SELECT * FROM ".DB_PRE."ads a, $this->table p WHERE a.placeid=p.placeid AND p.placeid=$placeid $areastr AND a.fromdate<=UNIX_TIMESTAMP() AND a.todate>=UNIX_TIMESTAMP() AND a.passed=1 AND a.status=1 AND p.passed=1");

       if($adses[0]['option'])

        {

               foreach($adses as $ads)

               {

                      $contents[] = ads_content($ads, 1);

                      $this->db->query("INSERT INTO $this->stat_table (`adsid`, `username`, `ip`, `referer`, `clicktime`, `type`) VALUES ('$ads[adsid]', '$_username', '$ip', '$this->referer', '$time', '0')");

                      $template = $ads['template'] ? $ads['template'] : 'ads';

               }

        }

        else

        {

               do{        

               $ads = $this->db->get_one("SELECT * FROM ".DB_PRE."ads a, $this->table p WHERE a.placeid=p.placeid AND p.placeid=$placeid $areastr AND a.fromdate<=UNIX_TIMESTAMP() AND a.todate>=UNIX_TIMESTAMP() AND a.passed=1 AND a.status=1 ORDER BY rand() LIMIT 1");

//防止空白产生,A广告仅允许显示在湖北,则访问武汉的时候,会读出来,要排除掉http://www.k686.com

if($ads['ischild'] || $ads['areaid']==$areaid || !$ads['areaid']) break;

               }

               while(true);

 

               $contents[] = ads_content($ads, 1);

               $this->db->query("INSERT INTO $this->stat_table (`adsid`, `username`, `ip`, `referer`, `clicktime`, `type`) VALUES ('$ads[adsid]', '$_username', '$ip', '$this->referer', '$time', '0')");

               $template = $ads['template'] ? $ads['template'] : 'ads';

        }

        include template('ads', $template);

}

 

 

5.       找到 , $place->show($id); 改成 $place->show($id,$areaid);

6.       注意外部调用的时候,带上areaid参数,比如:

<script language="javascript" src="http://www.k686.com/data/js.php?id=1&areaid={$areaid}"></script>

你可能感兴趣的:(JavaScript,sql,PHP,unix,cache)