2012年7月22日 星期日

PHP程式設計常用指令

**index
去除html
定界符
email發送
css,javascript外部連結(thickbox.css,thickbox.js,jquery.js)
html設定
meta設定
.htaccess
語系切換:select
語系切換:click
語系切換:外部連結共用檔
php連結外部檔案
sql查詢
sql新增
sql更新
sql刪除


IP位址
回上頁
內容摘錄n字:mb_substr()
字串處理substr
字串分割split
取代字串str_replace
搜尋字串位置strpos
取餘數%
Javascript 字串處理
偵測檔案是否存在(file_exists)
switch-case
SpryTabbedPanels
jquery checkbox(複選)
去掉html碼(php)
location.href
window.location.reload()
header()
日期、時間
md5()加密編碼
時區設定date_default_timezone_set()
換行\n轉為<br />
上傳檔案(php+html)
上傳檔案(改檔名為:日期+時間+$i+原始副檔名)
上傳檔案(名稱、檔案類型、檔案大小、暫存資料夾位置、檔案上傳有錯誤--錯誤代碼)
取得4捨5入之數值:round()
檔案大小:filesize()
分頁:最前頁、上一頁、下一頁、最後頁
將訊息分享到FB、twitter、G+
停用select(不可選) or 啟用select(可選)

**停用select(不可選) or 啟用select(可選)
$('#seltype1').click(function(){
    //停用select(不可選)
    $('#searchx').attr('disabled', true);
});

$('#seltype2').click(function(){
    //啟用select(可選)
    $('#searchx').attr('disabled', true);
});

**將訊息分享到FB、twitter、G+
*facebook:
http://www.facebook.com/sharer/sharer.php?u=http://www.sunbox.com.tw/

*twitter:
http://twitter.com/home/?status=SUNBOX慧光展業http://www.sunbox.com.tw/

*google+:
程式碼設定:
http://www.google.com/webmasters/+1/button/

<!-- 將此標記放在您想要顯示 +1 按鈕的位置 -->
<g:plusone size="medium" annotation="none"></g:plusone>

<!-- 將此顯示呼叫 (render call) 放在適當位置 -->
<script type="text/javascript">
  (function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  })();
</script>

-----------------------
*facebook:
http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.mobile01.com%2Fnewsdetail.php%3Fid%3D11520&t=Samsung+Wave+Y++Bada%E6%89%8B%E6%A9%9F%E5%86%8D%E7%8F%BE+5%E5%8D%83%E5%B9%B3%E5%83%B9+%E7%8E%A9%E7%A4%BE%E7%BE%A4%E5%89%9B%E5%89%9B%E5%A5%BD

*plurk:
http://www.plurk.com/?qualifier=shares&status=http%3A%2F%2Fwww.mobile01.com%2Fnewsdetail.php%3Fid%3D11520%20(Samsung+Wave+Y++Bada%E6%89%8B%E6%A9%9F%E5%86%8D%E7%8F%BE+5%E5%8D%83%E5%B9%B3%E5%83%B9+%E7%8E%A9%E7%A4%BE%E7%BE%A4%E5%89%9B%E5%89%9B%E5%A5%BD+%2F+2012-01-18+12%3A09+%2F+Mobile01%E6%96%B0%E8%81%9E)

*twitter:
http://twitter.com/home/?status=Samsung+Wave+Y++Bada%E6%89%8B%E6%A9%9F%E5%86%8D%E7%8F%BE+5%E5%8D%83%E5%B9%B3%E5%83%B9+%E7%8E%A9%E7%A4%BE%E7%BE%A4%E5%89%9B%E5%89%9B%E5%A5%BD+%2F+2012-01-18+12%3A09+%2F+Mobile01%E6%96%B0%E8%81%9E%20http%3A%2F%2Fwww.mobile01.com%2Fnewsdetail.php%3Fid%3D11520

*來個Google +1 推文按鈕,讓文章搜尋排名往前衝
http://www.google.com/webmasters/+1/button/
http://sofree.cc/google-plus-1/

<!-- 將此標記放在您想要顯示 +1 按鈕的位置 -->
<g:plusone size="medium" annotation="none"></g:plusone>

<!-- 將此顯示呼叫 (render call) 放在適當位置 -->
<script type="text/javascript">
  (function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  })();
</script>

**分頁:最前頁、上一頁、下一頁、最後頁
<?php
$number = $_GET["number"];
$total = "28";// 資料總數量 例:總共有28筆資料
$per = "5";// 每頁顯示數量 28筆每頁只顯示5筆

$open = $total/$per;// 計算會有幾頁分頁
$open = ceil($open);// 以上算出來的答案 有小數點則去除小數點並+1
$open = $open * $per;// 以上算出來的答案 乘以 顯示頁數的數量

  if($number-$per!=0){ $am=$number-$per;
    $url_up = " <a href=\"?number=".$am."\">上一頁</a>";
  }
  if($number<$total){ $Ram=$number+$per;
    $url_bottom = " <a href=\"?number=".$Ram."\">下一頁</a>";
  }
  if(!empty($number)&&$number!=$per){
    $front = "<a href=\"?number=".$per."\">最前頁</a>";
  }
  if($number <= $total){
    $finally = " <a href=\"?number=".$open."\">最後頁</a>";
  }
  echo "<div style=\"text-align:center;\">".$front."".$url_up."".$url_bottom."".$finally."</div>";
?>

**檔案大小:filesize
filesize("test.txt")/1024; //單位KB
round(filesize("admin/upload/$q2[photox]")/1024,2) //取小數點第二位,單位KB

**取得4捨5入之數值:round()
$a=round(傳數值,[位數])
round(1.95583, 2)
得到1.96

**上傳檔案(名稱、檔案類型、檔案大小、暫存資料夾位置、檔案上傳有錯誤--錯誤代碼)
$_FILES["file"]["name"]:上傳檔案的原始名稱。
$_FILES["file"]["type"]:上傳的檔案類型。
$_FILES["file"]["size"]:上傳的檔案原始大小。
$_FILES["file"]["tmp_name"]:上傳檔案後的暫存資料夾位置。
$_FILES["file"]["error"]:如果檔案上傳有錯誤,可以顯示錯誤代碼。

<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
    檔案名稱:<input type="file" name="file" id="file" /><br />
    <input type="submit" name="submit" value="上傳檔案" />
</form>
</body>
</html>

<?php
//upload.php
if ($_FILES["file"]["error"] > 0){
echo "Error: " . $_FILES["file"]["error"];
}else{
echo "檔案名稱: " . $_FILES["file"]["name"]."<br/>";
echo "檔案類型: " . $_FILES["file"]["type"]."<br/>";
echo "檔案大小: " . ($_FILES["file"]["size"] / 1024)." Kb<br />";
echo "暫存名稱: " . $_FILES["file"]["tmp_name"];

if (file_exists("upload/" . $_FILES["file"]["name"])){
echo "檔案已經存在,請勿重覆上傳相同檔案";
}else{
move_uploaded_file($_FILES["file"]["tmp_name"],"upload/".$_FILES["file"]["name"]);
}
}
?>

**內容摘錄n字:mb_substr()
$content_qn2=mb_substr($qn2[content.$langs3],0,-4,'utf8') ; //內容去掉</p>
$content_qn2=mb_substr($content_qn2,0,$word,'utf8').'...</p>' ; //內容摘錄n字

**搜尋字串位置strpos
$str1='xxbcd';
$find='bc';
$pos=strpos($str1,$find);
if ($pos === false){
    echo "在字串1沒有找到字串: $find<br>";
}else{
    echo "有找到字串$find,位置在 $pos";
}
  
**取代字串str_replace
str_replace('\\','',$_POST[editor1]); //去掉斜線\

**上傳檔案(改檔名為:日期+時間+$i+原始副檔名)
$filename = $_FILES["photo"]["name"][$i]; //$filename =原始檔名
$time=time()+(8*60*60);                 //取得格林威治+8時間
$time=date("YmdHis",$time);         //取得目前時間,例如 20101101-121303
$subname=split("[.]",$filename);
$fname = $time.$i.".".$subname[1]; //日期+時間+$i+原始副檔名($i為序號)

$in1=sprintf("update indexadmin set title='%s',content='%s',photo='%s' where id='%d'",$_POST['title'][$i],$_POST['content'][$i],$fname,$i+1);
mysql_query($in1);
move_uploaded_file($_FILES["photo"]["tmp_name"][$i], "image/" . $fname); //把上傳檔案放進image檔案夾

**上傳檔案(php+html)
<?php
$e=1;
if($_POST[upload]=="upload"){
    for($i=0;$i<3;$i++){
        if($_POST[title][$i]=='' or $_POST[content][$i]==''){
            $e=0;
            break;
        }
    }
}

if($_POST[upload]=="upload" && $e==1){
    for($i=0;$i<3;$i++){
        //改檔名(日期+時間+$i+原始副檔名)
        $filename = $_FILES["photo"]["name"][$i]; //$filename =原始檔名
        $time=time()+(8*60*60);                 //取得格林威治+8時間
        $time=date("YmdHis",$time);         //取得目前時間,例如 20101101-121303
        $subname=split("[.]",$filename);
        // echo "aa".$subname[1].'<br>';
        // $fname = $time.'_'.$filename; //目前時間_原始副檔名
        // $fname = $time.floor(microtime()*1000).$i.".".$subname[1]; //日期+時間+毫秒+$i+原始副檔名
        $fname = $time.$i.".".$subname[1]; //日期+時間+$i+原始副檔名
        // echo $fname."<br>";

        if($_FILES[photo][name][$i]==''){
            $in1=sprintf("update indexadmin set title='%s',content='%s' where id='%d'",$_POST['title'][$i],$_POST['content'][$i],$i+1);
        }else{
            $in1=sprintf("update indexadmin set title='%s',content='%s',photo='%s' where id='%d'",$_POST['title'][$i],$_POST['content'][$i],$fname,$i+1);
            // move_uploaded_file($_FILES["photo"]["tmp_name"][$i], "image/" . $_FILES["photo"]["name"][$i]); //把上傳檔案放進image檔案夾
            move_uploaded_file($_FILES["photo"]["tmp_name"][$i], "image/" . $fname); //把上傳檔案放進image檔案夾
            //copy($_FILES["goods_img"]["tmp_name"],"goodsimg\\".$_FILES["goods_img"]["name"]); //把上傳的檔案以原始名稱放進goodsimg檔案夾
            // unlink("image/".$_POST[photo2][$i]); //刪除舊圖檔
            //$msg=" 修改成功!!";               
        }
        mysql_query($in1);
    }
    $msg='<div class="hint">資料已更新!</div>';
}
?>

<form id="form1" name="form1" method="post" action="index_admin.php" enctype="multipart/form-data">
 <table width="100%" border="1" cellpadding="5" id="index_table">
  <tr>
    <th width="7%">序號</th>
    <th colspan="2">圖檔標題 / 圖檔
         &nbsp; <em class="s_note">圖片尺寸:590x400。單一圖檔大小限制3MB。</em></th>

    <th width="24%">說明</th>
  </tr>
<?php while($q2=mysql_fetch_assoc($q)){ ?>
  <tr>
    <th rowspan="2" align="center"><?php echo $s++; ?></th>
    <th width="7%" align="center">標題</th>
    <td width="62%" align="center"><input type="text" name="title[]" id="title[]" class="input_04" value="<?php echo $q2[title]; ?>"/></td>
    <td rowspan="2" align="center" valign="top"><textarea name="content[]" id="content[]" class="input_04 input_05"><?php echo $q2[content]; ?></textarea></td>
  </tr>
  <tr>
    <th align="center"><p>圖檔</p></th>
    <td align="center"><img src="image/<?php echo $q2[photo]; ?>" width="300" height="200" />
      <p>
        <input name="photo[]" id="photo[]" type="file" size="20" /><?php echo ' '.$q2[photo]; ?>
      </p>
    </td>
  </tr>
  <input name="photo2[]" type="hidden" id="photo2[]" value="<?php echo $q2['photo']; ?>" />
<?php } ?>

 </table>
      <div class="btn">
      <input type="submit" name="button" id="button" value="送出" class="submit"/>
      </div>
      <input type="hidden" name="upload" id="upload" value="upload" />
</form>

**字串分割split
$datetime=split(" ",$n2[opendate]);

**換行\n轉為<br />
$reply=nl2br($reply);

**時區設定date_default_timezone_set
date_default_timezone_set('Asia/Taipei');

**md5()加密編碼
md5($psw)

**日期、時間
date("Y-m-d H:i:s")
-----------------------------------------------------------
*拆日期、時間
$s2[opendate]="2011-04-08 15:04:14";

1.拆日期、時間
$datetime=split(" ",$s2[opendate]);

2.拆日期
$date=split("-",$datetime[0]);

3.拆時間
$time=split(":",$datetime[1]);


*編輯商品 product_editor.php

$opendate=$_POST[year]."-".$_POST[month]."-".$_POST[day];

$sql="select * from news where id=$id";
$s=mysql_query($sql);
$s2=mysql_fetch_assoc($s);

$date=split("-",$s2[opendate]);

<tr>
    <th>發佈日期:</th>
    <td width="36%">
    <input type="text" name="year" id="year" class="input_date" value="<?php echo $date[0]; ?>"/>

  <select name="month" id="month">
    <?php
    for($i=1;$i<13;$i++){
        if($date[1]==$i){
          echo "<option selected>".$i."</option>";
        }else{
          echo "<option>".$i."</option>";
        }
    }
    ?>
  </select>

<select name="day" id="day">
    <?php
    for($i=1;$i<32;$i++){
        if($date[2]==$i){
          echo "<option selected>".$i."</option>";
        }else{
          echo "<option>".$i."</option>";
        }
    }
    ?>
</select>
日</td>
    <th width="11%">置頂設定:</th>
    <td width="40%"><input type="checkbox" name="top" id="top" <?php if($s2[top]=='on') echo 'checked'; ?>/>消息置頂</td>
</tr>

*新增商品 product_add.php

$year=date('Y'); //年
$month=date('n'); //月
$day=date('j'); //日
$hour=date('H'); //時
$min=date('i'); //分

$opendate=$_POST[year]."-".$_POST[month]."-".$_POST[day];

<tr>
    <th>發佈日期:</th>
    <td width="36%">
    <input type="text" name="year" id="year" class="input_date" value="<?php echo $year; ?>"/>

  <select name="month" id="month">
  <?php for($m=1;$m<=12;$m++){ ?>
    <option <?php echo ($m==$month)?'selected':''; ?>><?php echo $m; ?></option>
  <?php } ?>
  </select>

<select name="day" id="day">
  <?php for($m=1;$m<=31;$m++){ ?>
    <option <?php echo ($m==$day)?'selected':''; ?>><?php echo $m; ?></option>
  <?php } ?>
</select>
日</td>
    <th width="11%">置頂設定:</th>
    <td width="40%"><input type="checkbox" name="top" id="top" />消息置頂</td>
</tr>
--------------------------------------------------------------------
PHP DATE 格式化日期時間
http://tw.myblog.yahoo.com/jessica-bird/article?mid=-2&prev=1119&l=a&fid=7

a - "am" 或是 "pm"
A - "AM" 或是 "PM"
相關參數使用如下:

d - 几日,二位數字,若不足二位則前面補零; 如: "01" 至 "31"
D - 星期几,三個英文字母; 如: "Fri"
F - 月份,英文全名; 如: "January"
h - 12 小時制的小時; 如: "01" 至 "12"
H - 24 小時制的小時; 如: "00" 至 "23"
g - 12 小時制的小時,不足二位不補零; 如: "1" 至 12"
G - 24 小時制的小時,不足二位不補零; 如: "0" 至 "23"
i - 分鐘; 如: "00" 至 "59"
j - 几日,二位數字,若不足二位不補零; 如: "1" 至 "31"
l - 星期几,英文全名; 如: "Friday"
m - 月份,二位數字,若不足二位則在前面補零; 如: "01" 至 "12"
n - 月份,二位數字,若不足二位則不補零; 如: "1" 至 "12"
M - 月份,三個英文字母; 如: "Jan"
s - 秒; 如: "00" 至 "59"
S - 字尾加英文序數,二個英文字母; 如: "th","nd"
t - 指定月份的天數; 如: "28" 至 "31"
U - 總秒數
w - 數字型的星期几,如: "0" (星期日) 至 "6" (星期六)
Y - 年,四位數字; 如: "1999"
y - 年,二位數字; 如: "99"
z - 一年中的第几天; 如: "0" 至 "365"


例如:

取得現在時間---------------
echo date("Y/m/d H:i",time());    //其結果為2010/03/24 14:32


格式化資料庫裡取得的時間------------------

echo date("Y/m/d H:i",strtotime($row['datetime']));

------------------------------------------------------------------------
接著是抓過去或未來的時間‧參考資料
首先,先把時間的各個部分分開來(date函數的各個參數):
$Y=date(Y);
$m=date(m);
$d=date(d);
$H=date(H);
$i=date(i);
$s=date(s);

再來,就是把東西組合起來,然後用mktime函數來做啦~
date( "Y年m月d日H時i分s秒", mktime($H,$i,$s,$m,$d,$Y) );
//將變數帶進mktime()裡,+、-可以調整
Ex.
date( "Y年m月d日H時i分s秒", mktime($H-1,$i,$s,$m,$d,$Y+1) );
//一年後的一小時前

**header()
header("Location:contact_list.php?n=1&page=1");

**location.href
location.href='pro01_edit.php?id=12';

**window.location.reload()
onclick="window.location.reload();"

**去掉html碼(php)
strip_tags($q2[$langs]);

**jquery checkbox(複選)
$("input[name='chk[]']").each(function(){
    // $(this).attr("checked", true);
    if($(this).attr("checked")){
        // alert($(this).val());
        selx+=$(this).val()+',';
    }
});
selx=selx.substring(0,selx.length-1);

**SpryTabbedPanels
<script src="SpryAssets/SpryTabbedPanels.js" type="text/javascript"></script>
<link href="SpryAssets/SpryTabbedPanels.css" rel="stylesheet" type="text/css" />

<div id="TabbedPanels1" class="TabbedPanels">
  <ul class="TabbedPanelsTabGroup">
    <li class="TabbedPanelsTab" tabindex="0">繁體中文</li>
    <li class="TabbedPanelsTab" tabindex="0">简体中文</li>
    <li class="TabbedPanelsTab" tabindex="0">English</li>
    <li class="TabbedPanelsTab" tabindex="0">日本語</li>
    <li class="TabbedPanelsTab" tabindex="0">español</li>
    <li class="TabbedPanelsTab" tabindex="0">português</li>
  </ul>
  <div class="TabbedPanelsContentGroup">
    <div class="TabbedPanelsContent tab01">
        <?php echo $q2[cht]; ?>
    </div>
   
    <div class="TabbedPanelsContent tab02">
        <?php echo $q2[chs]; ?>
    </div>
   
    <div class="TabbedPanelsContent tab03">
        <?php echo $q2[english]; ?>
    </div>
   
    <div class="TabbedPanelsContent tab04">
        <?php echo $q2[japan]; ?>
    </div>
   
    <div class="TabbedPanelsContent tab05">
        <?php echo $q2[spanish]; ?>
    </div>
   
    <div class="TabbedPanelsContent tab06">
        <?php echo $q2[portuguese]; ?>
    </div>
  </div>
</div>
<script type="text/javascript">
var TabbedPanels1 = new Spry.Widget.TabbedPanels("TabbedPanels1",{defaultTab:<?php echo $tabx; ?>});
</script>

**switch-case
switch ($langs){
    case 'cht':
        $tabx=0;
        break;
    case 'chs':
        $tabx=1;
        break;
    case 'english':
        $tabx=2;
        break;
    case 'japan':
        $tabx=3;
        break;
    case 'spanish':
        $tabx=4;
        break;
    case 'portuguese':
        $tabx=5;
        break;
    default:
        $tabx=0;
}

**偵測檔案是否存在(file_exists)
<?php
$filename = '/path/to/foo.txt';

if (file_exists($filename)) {
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}
?>

**取餘數%
$i2=$i%2;
if($i2!=0){echo "<tr>";} //奇數
if($i2==0){echo "</tr>";} //偶數

**字串處理substr
mb_substr($c2[title],0,25,'utf8').'...'; //取25個中文字

$pro01_7fuku=substr($pro01_7fuku,0,-1);

$content=substr($c2[content.$langs3],0,-4) ; //內容去掉</p>
$content=mb_substr($content,0,100,'utf8').'...</p>' ; //內容摘錄100字
$content=substr($content,0,100).'...</p>' ; //內容摘錄100字

**sql刪除
$sql="delete from pro01_7fuku where id in($pro01_7fuku)";
mysql_query($sql);

**sql更新
$sql="update pro01 set pclass01='$qa2' where id=$id";
mysql_query($sql);

**回上頁
<a href="javascript:history.back()"><div class="bleft"><div class="bright">回上頁</div></div></a>
javascript:history.back()
javascript:history.go(-1)
header('Location:上一頁的 .php 檔');
----------
php关于这个header("Cache-control: private")的用法
问:为什么我在点击浏览器的后退按钮后,所有字段的信息都被清空了?
答:这是由于你在你的表单提交页面中使用了 session_start 函数。
该函数会强制当前页面不被缓存。解决办法为,在你的 Session_start 函数
后加入 header("Cache-control: private"); 注意在本行之前你的PHP程序不能有任何输出。
(功能:有使用session_start()時"回上頁"不用重新整理或避免IE造成網頁過期)
----------
解決 session_start() 引起的 "回上一頁表單被清空"

範例:
form.php
<?php
session_start();
header("Cache-control:private");
?>
<form name="form1" method="post" action="back.php">
    <input type="text" name="t1">
    <input type="submit" name="submit" value="Next">
</form>

back.php
<?php
echo $_POST['t1'];
?>
<a href="javascript:history.go(-1);">Back</a>
----------
Javascript回上一頁表單被清空,問題處理
我們有時遇到判斷資料錯誤時,會使用javascript:history.go(-1);,但是如果我們有啟動session時,表單中的資料會被清掉。(如果沒有啟動,browser會記住表單的內容)
在php中使用下面的語法,就可以解決掉這個問題
1.
<?php
session_start();
header("Cache-control:private");
?>

2.
<?php
session_cache_limiter(‘private’);
session_start();
?>

3.此法IE Only (這個方法蠻笨的,不建議使用,因為每一個form都要做調整,上面兩個方法比較簡便)
<?php
session_start();
?>
<meta name="save" content="history">
<form name="form1″ method="post" action="test1.php">
<input type="text" name="t1″ style="behavior:url(#default#savehistory)">
<input type="submit" name="submit" value="Next">
</form>

**sql新增
$sql="insert into contact(name,country,company,product,email,phone,fax,content,ip) value('$name','$country','$company','$product','$email','$phone','$fax','$content','$ip')";
$q=mysql_query($sql);
//$q2=mysql_fetch_assoc($q);

**IP位址
if (!empty($_SERVER['HTTP_CLIENT_IP']))
    $ip=$_SERVER['HTTP_CLIENT_IP'];
else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
    $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
else
    $ip=$_SERVER['REMOTE_ADDR'];

**語系切換:外部連結共用檔
<?php
//語系
$langs=$_GET[lang]; //語系全名
if($langs==''){
    $langs='cht';
}

$langs3=substr($langs,0,3); //取語系前3個字

//SEO:title,meta,footer
$sql="select * from seo where lang='$langs'";
$seo=mysql_query($sql);
$seo2=mysql_fetch_assoc($seo);

//語系資料
$sql="select * from language where lang='$langs'";
$lan=mysql_query($sql);
$lan2=mysql_fetch_assoc($lan);

?>


**sql查詢
$sql="select * from indextxt where lang='$langs'";
$q=mysql_query($sql);
$q2=mysql_fetch_assoc($q);

$sql="select * from news where top='文章置頂' and up='上架' order by date desc,id desc limit 0,2";
$qn=mysql_query($sql);
$qn2=mysql_fetch_assoc($qn);
$qn=mysql_num_rows($c1);

**php連結外部檔案
require "admin/conn.php";
require "filec.php";
<?php require_once("header.php"); ?>

**語系切換:click
<script type="text/javascript">
$(function(){
    $('.xcht').click(function(){
        var lang=$(this).attr("alt");
        var url=location.href;
        url=url.split("?");
        location.href=url[0]+'?lang='+lang;
    });
});
</script>

<a href="#" alt="cht" onclick="return false" class="xcht">繁體中文</a>
<a href="#" alt="chs" onclick="return false" class="xcht">简体中文</a>
<a href="#" alt="english" onclick="return false" class="xcht">English</a>
<a href="#" alt="japan" onclick="return false" class="xcht">日本語</a>

**語系切換:select
<script type="text/javascript">
function langx(lan){
    var url=location.href;
    url=url.split("?");
    location.href=url[0]+'?lang='+lan;
}
</script>

<select name="select" id="select" onChange="langx(this.value)">
    <option>請選擇</option>
    <option value="cht">繁體中文</option>
    <option value="chs">簡體中文</option>
    <option value="japan">日文</option>
    <option value="english">英文</option>
    <option value="spanish">西班牙文</option>
    <option value="portuguese">葡萄牙文</option>
</select>

**.htaccess
RewriteEngine on

Options -Indexes

RewriteCond %{HTTP_HOST} ^7\-fukusewing\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.7\-fukusewing\.com$
RewriteRule ^/?$ "http\:\/\/www\.7\-fukusewing\.com\/cht\/" [R=302,L]

RewriteCond %{HTTP_HOST} ^7\-fukusewing\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.7\-fukusewing\.com$
RewriteRule ^404\.shtml$ "http\:\/\/www\.7\-fukusewing\.com\/404\.html" [R=302,L]

**去除html:
$str="aaa<br/>bbb";
strip_tags($str)

**定界符:
echo <<<EOD
<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript" src="../js/thickbox.js"></script>
<script type="text/javascript">
$(function(){
    location.href="javascript:tb_show('','#TB_inline?height=100&width=200&inlineId=reply_ok&modal=true',false)";
});
</script>
EOD;

**email發送:
if($_POST[replymail]=='reply' and $_GET[id]!='' and $_POST[rmail]!=''){
    $rmail=$_POST[rmail];
    $cid=$_GET[id];
    $reply=nl2br($_POST[reply]); //回覆內容
   
    //附檔的名稱
    $file1=$_FILES['file1']['name'];
    $file2=$_FILES['file2']['name'];
    $file3=$_FILES['file3']['name'];
   
    //附檔的暫存路徑
    $file1tmp=$_FILES['file1']['tmp_name'];
    $file2tmp=$_FILES['file2']['tmp_name'];
    $file3tmp=$_FILES['file3']['tmp_name'];
   
    $sql="insert into contact_reply(cid,reply,file1,file2,file3) value('$cid','$reply','$file1','$file2','$file3')";
    $q=mysql_query($sql);
   
    //發送email設定==============================================
    $sql="select * from contact where id=$_GET[id]";
    $em=mysql_query($sql);
    $em2=mysql_fetch_assoc($em);
    $name=$em2[name]; //留言人名字
    $content=$em2[content]; //留言內容
    $to=$em2[email]; //收信人(留言人)email
    $from='aaaaa<service@aaa.com>'; //寄件人
    $subject="來自aaa的回覆"; //信件標題

    //定義分界線
    $boundary = uniqid( "");
    $headers = "Content-type: multipart/mixed; boundary= $boundary\r\n";
    $headers .= "From:$from\r\n";

    //確定上傳檔案的MIME類型
    if($_FILES['file1']['type'])
    $file1mimeType = $_FILES['file1']['type'];
    else
    $file1mimeType ="application/unknown";
   
    if($_FILES['file2']['type'])
    $file1mimeType = $_FILES['file2']['type'];
    else
    $file1mimeType ="application/unknown";
   
    if($_FILES['file3']['type'])
    $file1mimeType = $_FILES['file3']['type'];
    else
    $file1mimeType ="application/unknown";
   
    //讀取附加檔案
    if($file1tmp!=''){
        $fp = fopen($file1tmp, "r"); // 打開檔案
        $read = fread($fp, filesize($file1tmp)); // 把整個檔案讀入一個變數
        $read = base64_encode($read); //用base64方法把它編碼
        $read = chunk_split($read); //把這個長字串切成由每行76個字元組成的小塊
    }
   
    if($file2tmp!=''){
        $fp2 = fopen($file2tmp, "r"); // 打開檔案
        $read2 = fread($fp2, filesize($file2tmp)); // 把整個檔案讀入一個變數
        $read2 = base64_encode($read2); //用base64方法把它編碼
        $read2 = chunk_split($read2); //把這個長字串切成由每行76個字元組成的小塊
    }
   
    if($file3tmp!=''){
        $fp3 = fopen($file3tmp, "r"); // 打開檔案
        $read3 = fread($fp3, filesize($file3tmp)); // 把整個檔案讀入一個變數
        $read3 = base64_encode($read3); //用base64方法把它編碼
        $read3 = chunk_split($read3); //把這個長字串切成由每行76個字元組成的小塊
    }

//附件處理
$att='';
if($file1!=''){
$att= <<<EOD
--{$boundary}
Content-type: {$file1mimeType}; name={$file1}
Content-disposition: attachment; filename={$file1}
Content-transfer-encoding: base64
{$read}
EOD;
}

if($att=='' and $file2!=''){
$att= <<<EOD
--{$boundary}
Content-type: {$file2mimeType}; name={$file2}
Content-disposition: attachment; filename={$file2}
Content-transfer-encoding: base64
{$read2}
EOD;
}elseif($att!='' and $file2!=''){
$att.= <<<EOD
--{$boundary}
Content-type: {$file2mimeType}; name={$file2}
Content-disposition: attachment; filename={$file2}
Content-transfer-encoding: base64
{$read2}
EOD;
}

if($att=='' and $file3!=''){
$att= <<<EOD
--{$boundary}
Content-type: {$file3mimeType}; name={$file3}
Content-disposition: attachment; filename={$file3}
Content-transfer-encoding: base64
{$read3}
EOD;
}elseif($att!='' and $file3!=''){
$att.= <<<EOD
--{$boundary}
Content-type: {$file3mimeType}; name={$file3}
Content-disposition: attachment; filename={$file3}
Content-transfer-encoding: base64
{$read3}
EOD;
}

$str1="<div style='font-weight:bold;'>這是管理者回覆的內容</div>"; // 要放在Content-transfer-encoding: 8bit 空一行 的下面

//建立郵件的主體($body內容不要縮排否則會有亂碼或錯誤)
//html+css版email
$body = "--$boundary
Content-type: text/html; charset=iso-8859-1
Content-transfer-encoding: 8bit

<table width='760' align='center' cellpadding='0' cellspacing='0' style='border:2px solid #E9E9E9;'>
  <tr>
    <td><img src='http://www.7-fukusewing.com/eric/images/email_logo_01.gif' width='760' height='91' /></td>
  </tr>
  <tr>
    <td>
    <!-- 從聯絡我們寄信寄給系統的信件格式 start-->
    <div style='line-height:1.4em;font-size:15px;color:#666;padding:20px 10px;'>
    <p>(繁中版)</p>
      <ul>
        <li>您的姓名:{$e2[name]}</li>
        <li>所在國家:{$e2[country]}</li>
        <li>公司名稱:{$e2[company]}</li>
        <li>詢問產品:{$e2[product]}</li>
        <li>電子郵件:{$e2[email]}</li>
        <li>聯絡電話:{$e2[phone]}</li>
        <li>傳真電話:{$e2[fax]}</li>
        <li>留言內容:<br />{$e2[content]}</li>
      </ul>
    </div>
    <!-- 從聯絡我們寄信寄給系統的信件格式 end-->   
    <!-- 從聯絡我們後台回覆給留言者的信件格式 start-->  
    <div style='line-height:1.4em;font-size:15px;color:#666;padding:20px 10px;'>
      <p style='font-weight:bold;'>$name 您好:</p>
      <p>您來信詢問的內容如下:</p>
      <div style='background:#f8f8f8;margin:0 5px;padding:10px;'>
      $content
      </div>
      <p>回覆如下:</p>
      <div style='border:1px solid #690;background:#f8f8f8;margin:0 5px;padding:10px;'>
      $reply</div>
      <p>謝謝您的來信。</p>
      <p>TEL:(+852)2333-3333 FAX:(+852)2444-4445<br />
        aaaa INTERNATIONAL CO., LTD.<br />
        BLOCK G. 6/F, aaa bbb, No. 101, vvvv ROAD, ddddd</p>
    </div>
    <!-- 從聯絡我們後台回覆給留言者的信件格式 end-->
    </td>
  </tr>
  <tr>
    <td bgcolor='#E9E9E9'>&nbsp;</td>
  </tr>
</table>
$att
--$boundary--";

/*html版email(無css)
$body = "--$boundary
Content-type: text/html; charset=iso-8859-1
Content-transfer-encoding: 8bit
PS.本信件由系統寄出,請勿直接回覆。<br/><br/>
$name 您好:<br/><br/>
您來信詢問的內容如下:<br/>
$content<br/><br/>
回覆如下:<br/>
$reply<br/><br/>
謝謝您的來信。<br/><br/>
TEL:(+852)4444-5517 FAX:(+852)6666-0066<br/>
xcxc  (HK) INTERNATIONAL CO., LTD.<br/>
BLOCK G. 6/F, fff sss, No. 141, dfdfdf ROAD,dfdfdf.<br/>
$att
--$boundary--";
*/

    //發送郵件
    mail($to, $subject,$body,$headers);
   
    //把上傳檔案放進mailup檔案夾
    move_uploaded_file($_FILES["file1"]["tmp_name"],"mailup/$file1");
    move_uploaded_file($_FILES["file2"]["tmp_name"],"mailup/$file2");
    move_uploaded_file($_FILES["file3"]["tmp_name"],"mailup/$file3");
   
echo <<<EOD
<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript" src="../js/thickbox.js"></script>
<script type="text/javascript">
$(function(){
    location.href="javascript:tb_show('','#TB_inline?height=100&width=200&inlineId=reply_ok&modal=true',false)";
});
</script>
EOD;
}

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(function(){
    $('#ok').click(function(){
        var reply=$('#reply').val();
        var file1=$('#file1').val();
        var file2=$('#file2').val();
        var file3=$('#file3').val();
        if(reply!=''){
            $('#form3a').submit();
            // location.href="javascript:tb_show('','#TB_inline?height=50&width=200&inlineId=reply_ok&modal=true',false)";
        }else{
            location.href="javascript:tb_show('','#TB_inline?height=100&width=200&inlineId=reply_hint&modal=true',false)";
        }
    });
});
</script>

<!--thickbox-->
<div id="reply_hint" style="display:none;">
    <p style="margin:0px;text-align:center;font-size:15px;">
        請輸入回覆內容 !
    </p>
    <p style="text-align:center;margin:0px;">
        <input type="submit" name="button4" id="button4" value="了解" onclick="tb_remove()"/>
    </p>
</div>
<div id="reply_ok" style="display:none;">
    <p style="margin:0px;text-align:center;font-size:15px;">
        回覆成功 !
    </p>
    <p style="text-align:center;margin:0px;">
        <input type="submit" name="button4" id="button4" value="了解" onclick="location.href='contact_list.php?n=1&page=1';"/>
    </p>
</div>

<div id="all">
<div id="content_wrap">
<?php require_once("menu.php"); ?>
    <h1>聯絡我們管理</h1>
    <form id="form3a" name="form3a" method="post" action="" enctype="multipart/form-data">
    <table width="100%" border="1" cellpadding="5" class="replay">
      <tr>
        <th colspan="2">回覆內容</th>
      </tr>
      <tr>
        <th width="13%">回覆</th>
        <td>
            <textarea name="reply" id="reply" cols="45" rows="5" class="input_03 hfix"></textarea>
        </td>
      </tr>
      <tr>
        <th>附加檔案</th>
        <td width="87%">
          <table width="100%">
            <tr>
              <td width="86%"><input type="file" name="file1" id="file1" /></td>
            </tr>
            <tr>
              <td><input type="file" name="file2" id="file2" /></td>
            </tr>
            <tr>
              <td><input type="file" name="file3" id="file3" /></td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
    <div class="btn">
        <input type="hidden" name="replymail" id="replymail" value="reply" class="btn"/>
        <input type="hidden" name="rmail" id="rmail" value="<?php echo $e2[email]; ?>" class="btn"/>
        <input type="button" name="ok" id="ok" value="送出" class="submit"/>
    </div>
    </form>
    <table width="100%" border="1" cellpadding="5" class="replay">
      <tr>
        <th colspan="6">客戶信件內容</th>
      </tr>
      <tr>
        <th>編號</th>
        <td width="17%"><?php echo $_GET[no]; ?></td>
        <th width="11%">所在國家</th>
        <td width="20%"><?php echo $e2[country]; ?></td>
        <th width="11%">來源IP</th>
        <td width="30%"><?php echo $e2[ip]; ?></td>
      </tr>
      <tr>
        <th width="11%">您的姓名</th>
        <td><?php echo $e2[name]; ?></td>
        <th>公司名稱</th>
        <td><?php echo $e2[company]; ?></td>
        <th>詢問產品</th>
        <td><?php echo $e2[product]; ?></td>
      </tr>
      <tr>
        <th>聯絡電話</th>
        <td><?php echo $e2[phone]; ?></td>
        <th>傳真電話</th>
        <td><?php echo $e2[fax]; ?></td>
        <th>電子郵件</th>
        <td><?php echo $e2[email]; ?></td>
      </tr>
      <tr>
        <th valign="top">留言內容</th>
        <td colspan="5" valign="top"><?php echo $e2[content]; ?></td>
      </tr>
      <tr>
        <th>時間</th>
        <td colspan="5"><?php echo $e2[date]; ?></td>
      </tr>
    </table>
</div>
</div>

**css,javascript外部連結(thickbox.css,thickbox.js,jquery.js)
<link href="css/thickbox.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/thickbox.js"></script>
<script type="text/javascript" src="js/jquery.js"></script>

**html設定
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>公司簡介</title>
<meta name="description" content="abcd">
<meta name="keywords" content="aaa,bbb,ccc">
<link href="css/thickbox.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/thickbox.js"></script>
<script type="text/javascript">
$(function(){

});
</script>
</head>
<body>

</body>
</html>

**meta設定
<meta name="description" content="xx牌的高品質xx,憑藉著臺灣xx公司強大的機械加工能力,並採用最先進的日本進口加工設備">
<meta name="keywords" content="xx..cc,xx">

**Javascript 字串處理 (String Operations)
    尋找字串(search)
    stringObject.search(searchstring)
    stringObject.search(尋找的字串)
    大小寫必須相符

    var str = "test String";
    alert(str.search("Str"));
    alert(str.search("str"));
    輸出結果:5
    輸出結果:-1


    傳回字串內相符的字串(match)
    無相符字串則傳回null,大小寫要相符

    var str = "test String";
    alert(str.match("TEST"));
    alert(str.match("Str"));
    輸出結果:null
    輸出結果:Str

    取得字串長度(length)
    var str = "test String";
    alert(str.length);
    輸出結果:11


    尋找某字串在字串內的位置(indexOf)
    stringObject.indexOf(searchvalue,fromindex)
    stringObject.indexOf (尋找的字串,開始尋找的位置)

    var str = "test String";
    alert(str.indexOf("Str"));
    alert(str.indexOf("Str",6));
    輸出結果:5
    輸出結果:-1


    從後往前找某字串在字串內的位置(lastIndexOf)
    stringObject.lastIndexOf(searchvalue,fromindex)
    stringObject.lastIndexOf (尋找的字串,開始尋找的位置)

    var str = "test String";
    alert(str.lastIndexOf("Str"));
    alert(str.lastIndexOf("Str",2));
    alert(str.lastIndexOf("Str",10));
    輸出結果:5
    輸出結果:-1
    輸出結果:5


    變更英文字大小寫
    toUpperCase() 大寫
    toLowerCase() 小寫

    var str = "test String";
    alert(str.toUpperCase());
    alert(str.toLowerCase());
    輸出結果:TEST STRING
    輸出結果:test string


    傳回字串內某位置的字元(charAt)
    stringObject.charAt(index)
    位置由0開始

    var str = "test String";
    alert(str.charAt(3));
    alert(str.charAt(7));
    輸出結果:t
    輸出結果:r


    傳回參數中以Unicode值所代表的字元組成的字串(fromCharCode)
    String.fromCharCode(numX,numX,...,numX)
    alert(String.fromCharCode(65,66,67));
    輸出結果:ABC


    傳回字串內某位置區間的字串(substring & substr)
    stringObject.substring(start,stop)
    stringObject.substring(開始位置,結束位置)
    stringObject.substr(start,length)
    stringObject.substr(開始位置,字串長度)
    (開始位置)若為負數則由後往前數,但不適用於IE

    alert("0123456789".substring(4,6))
    alert("0123456789".substr(4,6))
    輸出結果:45
    輸出結果:456789


    字串取代(replace)
    stringObject.replace(findstring,newstring)
    stringObject.replace(要找尋的字串,換成新的字串)
    大小寫必須相符

    var str = "test String";
    alert(str.replace("string", "Text"));
    alert(str.replace("String", "Text"));
    輸出結果:test String
    輸出結果:test Text


    組合字串(concat)
    stringObject.concat(stringA,stringB,...,stringX)
    stringObject.concat(字串A,字串B,...,字串X)

    var strA = "test ";
    var strB = "String";
    alert(str1.concat(str2));
    輸出結果:test String


    取得切割字串(slice)
    stringObject.slice(start,end)
    stringObject.slice(開始位置,結束位置)

    var str = "test String";
    alert(str.slice(5));
    alert(str.slice(0,4));
    輸出結果:String
    輸出結果:test

   
    分割字串(split)
    stringObject.split(separator, howmany)
    stringObject.split(分割字串, 分割後各字串的字元數)

    var str = "test String";
    alert(str.split(""));
    alert(str.split(" ",4));
    輸出結果:t,e,s,t, ,S,t,r,i,n,g
    輸出結果:test,String

您或許對下列文章會感興趣:
Facebook應用程式框架
建立新的Facebook應用程式
加入Facebook應用程式開發人員
Facebook 預約發文功能,可以在你設定的時間自動發表文章!