Chinaunix首页 | 论坛 | 博客
  • 博客访问: 29954570
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类:

2009-09-04 01:40:12

1.PHP模板的文件格式如下:
#以下是数据采集
#本月的数组记录集
$row1 = array('dianshopname'=>'一叶','dianurl'=>'shop-0102','infotitle'=>'本月供应水果'); 
$row2 = array('dianshopname'=>'生生','dianurl'=>'shop-0122','infotitle'=>'本月供应木头'); 
$row3 = array('dianshopname'=>'JAVA','dianurl'=>'shop-0132','infotitle'=>'本月供应商品'); 
$result = array(0=>$row1,1=>$row2,2=>$row3);     

#下月的数组记录集
$row4 = array('dianshopname'=>'下月一叶','dianurl'=>'shop-1102','infotitle'=>'下月供应水果'); 
$row5 = array('dianshopname'=>'下月一叶','dianurl'=>'shop-2102','infotitle'=>'下月供应水果'); 
$row6 = array('dianshopname'=>'下月一叶','dianurl'=>'shop-3102','infotitle'=>'下月供应水果'); 
$result2 = array(0=>$row4,1=>$row5,2=>$row6);     



#本月的四条随机数据
$index1 = rand(0,count($result)-1);
$index2 = rand(0,count($result)-1);
$index3 = rand(0,count($result)-1);
$index4 = rand(0,count($result)-1);

#下月的四条随机数据
$index5 = rand(0,count($result2)-1);
$index6 = rand(0,count($result2)-1);
$index7 = rand(0,count($result2)-1);
$index8 = rand(0,count($result2)-1);

#提取本月的四条记录出来
$currdianname1 = $result[$index1]['dianshopname'];
$currdianurl1 = $result[$index1]['dianurl'];
$currinfotitle1 = $result[$index1]['infotitle'];

$currdianname2 = $result[$index2]['dianshopname'];
$currdianurl2 = $result[$index2]['dianurl'];
$currinfotitle2 = $result[$index2]['infotitle'];

$currdianname3 = $result[$index3]['dianshopname'];
$currdianurl3 = $result[$index3]['dianurl'];
$currinfotitle3 = $result[$index3]['infotitle'];

$currdianname4 = $result[$index4]['dianshopname'];
$currdianurl4 = $result[$index4]['dianurl'];
$currinfotitle4 = $result[$index4]['infotitle'];

#提取下月的四条记录出来
$nextdianname1 = $result2[$index5]['dianshopname'];
$nextdianurl1 = $result2[$index5]['dianurl'];
$nextinfotitle1 = $result2[$index5]['infotitle'];

$nextdianname2 = $result2[$index6]['dianshopname'];
$nextdianurl2 = $result2[$index6]['dianurl'];
$nextinfotitle2 = $result2[$index6]['infotitle'];

$nextdianname3 = $result2[$index7]['dianshopname'];
$nextdianurl3 = $result2[$index7]['dianurl'];
$nextinfotitle3 = $result2[$index7]['infotitle'];

$nextdianname4 = $result2[$index8]['dianshopname'];
$nextdianurl4 = $result2[$index8]['dianurl'];
$nextinfotitle4 = $result2[$index8]['infotitle'];

#-----------数据提供完成---------------------------

$firstparam = (isset($_GET['firstparam']))?$_GET['firstparam']:'0';        #0表示本月生效    1表示下月生效
$secondparam = (isset($_GET['secondparam']))?$_GET['secondparam']:'1';   #0表示四条        1表示更多




if ($firstparam == 0 && $secondparam == 0) {
    echo "document.write('
本月生效
');";
    echo "document.write('
');";
}elseif ($firstparam == 0 && $secondparam == 1) {
    echo "document.write('
本月生效更多
');";
    echo "document.write('
    ";   
        for($i = 0;$i        $dianname     = $result[$i]['dianshopname'];
            $dianurl    = $result[$i]['dianurl'];
            $infotitle  = $result[$i]['infotitle'];
            echo "
  • $i.$dianname|$infotitle
  • ";
        }
        echo "
');";
   
   
}elseif ($firstparam == 1 && $secondparam == 0) {
    echo "document.write('
下月生效
');";
    echo "document.write('
');";
   
}elseif ($firstparam == 1 && $secondparam == 1) {
    echo "document.write('
下月生效更多
');";
    echo "document.write('
    ";   
        for($i = 0;$i        $dianname     = $result2[$i]['dianshopname'];
            $dianurl    = $result2[$i]['dianurl'];
            $infotitle  = $result2[$i]['infotitle'];
            echo "
  • $i.$dianname|$infotitle
  • ";
        }
        echo "
');";   
}

?>

2.程序在后台管理员保存的时候。会生成一份这样的PHP文件出来。
只是上面的数据集(数组变量所存放的值不同而已其他的内容是一样的)

数据从DB查询出来之后赋给一个数组变量的方法如下:
include_once(dirname(__FILE__)."/../../include/DBConnection.php");
$db = new dbconn();
$sql = "select a.zh_name as dianshopname,a.zh_cachedir as dianurl,b.zh_name as infotitle
from zh_shop as a,zh_prod as b,zh_infoeffect as c
where a.zh_userid = c.zh_userid and b.zh_id = c.zh_infoid and c.zh_iscommend = '1' and zh_effecttime = '9' and c.zh_userid
in (select zh_userid from zh_user_area where zh_proid = 1)";
$result = $db->fetch_row_array($sql);#得到一个数组数据集
$num = 0;
foreach ($result as $val) {
    $row[$num] = array('dianshopname'=>$val['dianshopname'],'dianurl'=>$val['dianurl'],'infotitle'=>$val['infotitle']);
    $num = $num + 1;
}
$i = 0;
for ($i;$i<$num;$i++) {
    $target_info[] = $row[$i];
}

其中:
$row1 = array('id'=>'1','name'=>'a');  相当于一条SQL查询的一个记录
相当于:
$row[$num] = array('dianshopname'=>$val['dianshopname'],'dianurl'=>$val['dianurl'],'infotitle'=>$val['infotitle']);
$result = array(1=>$row1,2=>$row2,3=>$row3);  相当于select * from 多条记录
相当于:$target_info[] = $row[$i];



2.如何将这条查询出来的SQL语句得到的数组保存到文件中去呢?
我查了一下相关的资料如下:
$arr_old = array(1,55,2,36,77);
print_r($arr_old);
/*
Array
(
[0] => 1
[1] => 55
[2] => 2
[3] => 36
[4] => 77
)
*/
$file_name_old="test.php";
$file_old=fopen($file_name_old,"w");
fwrite($file_old,serialize($arr_old)); #将其序列化出来
fclose($file_old);

$file_name_new="test.php";
$file_new=fopen($file_name_new,"r");
$file_read = fread($file_new, filesize($file_name_new));
fclose($file_new);
$arr_new = unserialize($file_read); #反序列化出来即可
print_r($arr_new);
/*
Array
(
[0] => 1
[1] => 55
[2] => 2
[3] => 36
[4] => 77
)
*/

好!
我的解决方案:
当管理员一提交就会写两份数据!
一份是保存数组的信息、另一份是写统一模板的PHP文件!

针对:本月还是下月可以分别保存两份数据分别存放
第一份是本月的
第二份是下月的




阅读(1081) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~