• 10147阅读
  • 2回复

一个简单的格子程序,PHP写的 [复制链接]

上一主题 下一主题
离线XChinux
 

只看楼主 倒序阅读 楼主  发表于: 2006-06-21
一个简单的格子程序,PHP写的,没后台,需要的话,各位可以拿去自己改。附件中是源码
有四个文件,config.php, adlist.php, function.php,index.php
分别用做默认配置,广告配置,功能函数,显示页面,另外会自动生成一个data.html的缓存文件
config.php

<?php
$CELL_WIDTH = 50;    // 单元格子宽度
$CELL_HEIGHT = 50;    // 单元格子高度
$MAX_X_NUM = 10;    // 每行格子数
$MAX_Y_NUM = 100;    // 每列柜子数
$default_img = "default.jpg";    // 格子默认显示图像
$default_url = "http://www.url.com";    // 格子默认链接网址
?>


adlist.php

<?php
$adlist = array();
// 每一条广告的格式,
// $adlist[] = array(x1, y1, x2, y2, imagefile, linkurl);
// 即:每条广告占用从(x1, y1)到(x2,y2)这两个格子之间的所有格子(包括x1,y1,x2,y2),
// 显示图像为imagefile,链接url为linkurl
// 每个格子可以以(x,y)的坐标唯一确定,x, y分别为从左边和上边数的坐标,下标都从0开始
$adlist[] = array(1, 1, 2, 2, "1122.jpg", "http://www.1122.org/");
$adlist[] = array(4, 5, 6, 5, "4565.jpg", "http://www.4565.org/");
?>


function.php

<?php
function drawcell($i, $j, $ilen, $jlen, $img, $url)
{
   global $CELL_WIDTH, $CELL_HEIGHT, $MAX_X_NUM, $MAX_Y_NUM;
   $top = $j*$CELL_HEIGHT;
   $left = $i*$CELL_WIDTH;
   $width = $ilen*$CELL_WIDTH;
   $height = $jlen*$CELL_HEIGHT;
   $strret = <<<EOT
<div style="top:{$top}px;left:{$left}px;width:{$width}px;height:{$height}px;">
<a href="{$url}"><img src="{$img}" border="0" height="{$height}" width="{$width}"></a>
</div>\n
EOT;
   return $strret;
}

function is_used($i, $j)
{
   global $adlist;
   $ret = false;
   foreach ($adlist as $ad)
   {
       if ($i >= $ad[0] && $i <= $ad[2] && $j >= $ad[1] && $j <= $ad[3])
       {
           $ret = true;
           break;
       }
   }
   return $ret;
}

function drawtable(&$data)
{
   global $CELL_WIDTH, $CELL_HEIGHT, $MAX_X_NUM, $MAX_Y_NUM, $adlist, $default_img, $default_url;
   for ($i = 0; $i < $MAX_X_NUM; $i++)
   {
       for ($j = 0; $j < $MAX_Y_NUM; $j++)
       {
           if (!is_used($i, $j))
           {
               $data .= drawcell($i, $j, 1, 1, $default_img, $default_url);
           }
       }
   }
   
   foreach ($adlist as $ad)
   {
       $data .= drawcell($ad[0], $ad[1], $ad[2] - $ad[0] + 1, $ad[3] - $ad[1] + 1, $ad[4], $ad[5]);
   }
}
?>


index.php

<?php
$update_cache = false;
if (file_exists("data.html"))
{
   $time_data = filemtime("data.html");
   if (filemtime("config.php") > $time_data ||
       filemtime("adlist.php") > $time_data ||
       filemtime("function.php") > $time_data)
   {
           $update_cache = true;
   }
}
else
{
   $update_cache = true;
}

if ($update_cache)
{
   include_once("config.php");
   include_once("adlist.php");
   include_once("function.php");
   $output = "";
   drawtable($output);
   file_put_contents("data.html", $output);
}
else
{
   $output = file_get_contents("data.html");
}
?>
<html>
<head>
<style type="text/css">
body {font-size:12px}
div {position:absolute;border:#FF0000;border-width:1px;border-style:solid;text-align:center;}
</style>
</head>
<body>
<?=$output?>
</body>
</html>
附件: bwgz.rar (4 K) 下载次数:16
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线oogoogle
只看该作者 1楼 发表于: 2006-08-21

我安装后怎么显示这个?
Fatal error: Call to undefined function: file_put_contents() in /home/content/y/s/b/ysbetnet/html/2/index.php on line 25
离线XChinux

只看该作者 2楼 发表于: 2006-08-21
PHP5才有这个函数.
楼上的可检查一下PHP的版本.
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
快速回复
限100 字节
 
上一个 下一个