+ -
当前位置:首页 → 问答吧 → 【第四天学习PHP的过程和总结】

【第四天学习PHP的过程和总结】

时间:2008-05-30

来源:互联网

  • 今天实在是忙碌的一天,早上在宿舍学习PHP。书看到第三章了,开始有点吃力了,呵呵,不过还是一点点啃下来了。
  • 下午是从1点中开始的模拟电子线路实验,把6个小时的分量缩减到5个小时,但是还是有够忙的。晚上还是校选课英语影评的最后一节课,得过去交结课论文!
  • 回来就开始face up to the problem: ubuntu下的声卡可以识别但是不发声!这个问题已经搁置很久了,决定今天把它解决了,之后再在ubuntu下配置个apache,不过php编辑器应该选择什么呢? 大家给个建议吧~ :)呵,当然是linux下的~
  • 好啦,开始进入正题吧~
  • 首先,让我们回忆一下C语言中几乎每个程序都有的 : #include <stdio.h> 。然后我们就比较好理解PHP中的文件包含功能了。
  • header.html
  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  • <html xmlns="http://www.w3.org/1999/xhtml">

  • <head>

  • <meta http-equiv="Content-Type" content="text/html; charset=gb2312">

  • <title><?php echo $page_title; ?></title>

  • <style type="text/css" media="all">@import "./includes/layout.css";</style>
  •     //在此引入了CSS样式,没有也不要紧,就不要设置这个吧~外观问题
    </head>

  • <body>
      <div id="wraper">
        <div id="content">
          <div id="nav">
            <h3>Menu</h3>
            <ul>   //unordered list  对应的是 <ol></ol>
              <li class="navtop"><a href="index.php" title="Go to the Home Page">Home</a></li>
              <li><a href="calculator.php" title="Use the Sales Calculator">Calculator</a></li>
              <li><a href="dateform.php" title="Check out the Date Form">Date Form</li>
              <li><a href="redister.php" title="Register">Register</a></li>
            </ul>
      </div>
  • 把它存在includes文件夹里面, :)
  • footer.html
  •     </div>
       </div id="footer"><p>&copy; Copyright 2005 by Larry E. Ullman & DMCInsights. Inc.</p> </div>
      </div>
    </body>
    </html>
  • 同理,存在includes文件夹里
  • 然后新建一个php页面,比如叫index.php
  • <?php
    $page_title = 'Welcome to the Site!';
    include('./includes/header.html');  //注意哦!
    ?>
  • <h1 id="mainhead">Big Header</h1>

  •   <p>This is where you'll put the main page content. This content will differ for each page.</p>

  •   <p>This is where you'll put the main page content. This content will differ for each page.</p>

  •   <p>This is where you'll put the main page content. This content will differ for each page.</p>

  •   <p>This is where you'll put the main page content. This content will differ for each page.</p>

  • <h2>Subheader</h2>

  •   <p>This is where you'll put the main page content. This content will differ for each page.</p>

  •   <p>This is where you'll put the main page content. This content will differ for each page.</p>

  •   <p>This is where you'll put the main page content. This content will differ for each page.</p>

  •   <p>This is where you'll put the main page content. This content will differ for each page.</p>
      
    <?php
    include('./includes/footer.html');   //注意!
    ?>
  • 保存吧,我们假设includes文件夹与index.php平行,就是在同一个文件夹里面。
  • 呵呵,看看效果吧!
  • 呵呵,还记得我们注册时如果哪里搞错了回去重新填写时一大堆数据都没了,是不是很郁闷啊!?
  • 嘿嘿,黏性表单应运而生:
  • 我们先回忆一下:  if(isset($_POST['$var']))  是检查值有没有设置的。
  • is_numeric($_POST['$var']) 检查是否是数字
  • <?php
    $page_title = 'Widget Cost Calculator';
    include('./includes/header.html');   //包含文件
  • if (isset($_POST['submitted'])){

  • if(is_numeric($_POST['quantity']) && is_numeric($_POST['price']) && is_numeric($_POST['tax']) ){   //检查三者是否为数字

  •   $taxrate = $_POST['tax'] / 100 ; //通过POST传递值

  •   $total = ($_POST['quantity'] * $_POST['price'] ) * ($texrate + 1);

  •   echo '&lt;h1 id="mainhead">Total Cost</h1>

  •    <p>The total cost of purchasing ' .$_POST['quantity'] .

  •    ' widget(s) at $' . number_format($_POST['price'],2) .

  •     ' each, including a tax rate of ' . $_POST['tax'] . '%,

  •     is $' . number_format($total,2) . '.</p><p><br></p>';

  • }else{

  •   echo '<h1 id="mainhead">Error!</h1>

  •   <p class = "error">Please enter a valid quantity, price, and
  • tax.</p><p><br></p>';

  • }

  • }     // End of main isset() IF.
  • ?&gt;
  • <h2>Widget Cost Calculator</h2>

  • <form action="calculator.php" method="post">

  •   <p>Quantity: <input type="text" name="quantity" size="5" maxlength="10"
      value="&lt;?php if(isset($_POST['quantity'])) echo $_POST['quantity']; ?> "&gt;</p>
  • // 注意 value开始的句子, 如果已经设置了值就输出曾经设置的,密码除外
      <p>Price: <input type="text" name="price" size="5" maxlength="10"
      value="&lt;?php if(isset($_POST['price'])) echo $_POST['price']; ?> "&gt;</p>

  •   <p>Tax(%): <input type="text" name="tax" size="5" maxlength="10"
      value="&lt;?php if(isset($_POST['tax'])) echo $_POST['tax']; ?>"&gt;</p>

  •   <p><input type="submit" name="submit" value="Calculate!"></p>

  •   <input type="hidden" name="submitted" value="TRUE">

  • </form>
  • <?php
    include('./includes/footer.html');
    ?>

作者: casual0402   发布时间: 2008-05-29

接下来书上是学习了函数,不过不知道大家记不记得前两天的一个函数:
    首先,我们复习一下函数的格式:

    function function_name(){
               code;
    }

    接着看一个函数:

    function tablebgcolor(){   //定义函数;

        static $bgcolor;   //设置静态变量

        if ($bgcolor == "00ffff")   //判断语句,如果背景颜色为 #00ffff则为真

        {
                $bgcolor = "ffff00";
        }

        else { $bgcolor = "00ffff"; }  //如果不为真
        return ($bgcolor);  //返回背景颜色
}


然后看看插入页面代码的效果吧~~

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
</head>
<body>
<?php
function tablebgcolor(){
        static $bgcolor;
        if ($bgcolor == "00ffff")
        {
                $bgcolor = "ffff00";
        }
        else { $bgcolor = "00ffff"; }
        return ($bgcolor);
}
echo '<table border=1><br>';
for ($i = 1; $i <= 10; $i++){
        $bgcolor = tablebgcolor();
        echo '<tr><td bgcolor = '.$bgcolor.'>this the '.$i.' line</td></tr><br>';
}
echo '</table>';
?>
</body>
</html>

作者: casual0402   发布时间: 2008-05-29

今天挺累的,可能文章很粗糙,对不住大家……

作者: casual0402   发布时间: 2008-05-29

不止是粗糙

作者: xiaoer88   发布时间: 2008-06-05

热门下载

更多