TOP

[PHP] include其他php檔時,中文出現亂碼

利用mb_convert_encoding解決中文出現亂碼 的問題

$ABC = mb_convert_encoding($ABC, "UTF-8", "BIG5");
TOP

[PHP][轉貼]PHP內使用mysql相關語法

[轉貼]PHP內使用mysql相關語法

原文出處:http://austintodo.pixnet.net/blog/post/23821431-php---mysql-%E6%95%99%E5%AD%B8

這邊有個php mysql 教學的網站
http://www.php-mysql-tutorial.com/
用php+mysql也是行之有年了...
紀錄一下在php內怎麼使用mysql相關語法
More »
TOP

[PHP] [轉貼] PHP 分頁 函數

轉貼兩個不錯的PHP 分頁函數

(1)參考資料:http://blog.xuite.net/kinds/PHPandCSS/13159453

<?
//為了避免重復包含文件而造成錯誤,加了判斷函數是否存在的條件:
if(!function_exists(pageft)){
//定義函數pageft(),三個參數的含義為:
//$totle:資訊總數;
//$displaypg:每頁顯示資訊數,這裏設置預設是20;
//$url:分頁瀏灠中的鏈結,除了加入不同的查詢資訊"page"外的部分都與這個URL相同。
//   預設值本該設為本頁URL(即$_SERVER["REQUEST_URI"]),但設置預設值的右邊只能為常量,所以該預設值設為空字串,在函數內部再設置為本頁URL。
function pageft($totle,$displaypg=20,$url=''){
//定義幾個總體變數:
//$page:當前頁碼;
//$firstcount:(資料庫)查詢的起始項;
//$pagenav:頁面導航條代碼,函數內部並沒有將它輸出;
//$_SERVER:讀取本頁URL"$_SERVER["REQUEST_URI"]"所必須。
global $page,$firstcount,$pagenav,$_SERVER;
//為使函數外部可以訪問這裏的"$displaypg",將它也設為總體變數。注意一個變數重新定義為總體變數後,原值被覆蓋,所以這裏給它重新賦值。
$GLOBALS["displaypg"]=$displaypg;
if(!$page) $page=1;
//如果$url使用預設,即空值,則賦值為本頁URL:
if(!$url){ $url=$_SERVER["REQUEST_URI"];}
//URL分析:
$parse_url=parse_url($url);
$url_query=$parse_url["query"]; //單獨取出URL的查詢字串
if($url_query){
//因為URL中可能包含了頁碼資訊,我們要把它去掉,以便加入新的頁碼資訊。
//這裏用到了正則運算式,請參考本站的相關文章
$url_query=ereg_replace("(^|&)page=$page","",$url_query);
//將處理後的URL的查詢字串替換原來的URL的查詢字串:
$url=str_replace($parse_url["query"],$url_query,$url);

//在URL後加page查詢資訊,但待賦值:
if($url_query) $url.="&page"; else $url.="page";
}else {
$url.="?page";
}
頁碼計算:
$lastpg=ceil($totle/$displaypg); //最後頁,也是總頁數
$page=min($lastpg,$page);
$prepg=$page-1; //上一頁
$nextpg=($page==$lastpg ? 0 : $page+1); //下一頁
$firstcount=($page-1)*$displaypg;
//開始分頁導航條代碼:
$pagenav="顯示第 <B>".($totle?($firstcount+1):0)."</B>-<B>".min($firstcount+$displaypg,$totle)."</B> 條記錄,共 $totle 條記錄<BR>";
//如果只有一頁則跳出函數:
if($lastpg<=1) return false;
$pagenav.=" <a href='$url=1'>首頁</a> ";
if($prepg) $pagenav.=" <a href='$url=$prepg'>前頁</a> "; else $pagenav.=" 前頁 ";
if($nextpg) $pagenav.=" <a href='$url=$nextpg'>後頁</a> "; else $pagenav.=" 後頁 ";
$pagenav.=" <a href='$url=$lastpg'>尾頁</a> ";
//下拉跳轉列表,迴圈列出所有頁碼:
$pagenav.=" 到第 <select name='topage' size='1' onchange='window.location=\"$url=\"+this.value'>\n";
for($i=1;$i<=$lastpg;$i++){
if($i==$page) $pagenav.="<option value='$i' selected>$i</option>\n";
else $pagenav.="<option value='$i'>$i</option>\n";
}
$pagenav.="</select> 頁,共 $lastpg 頁";
}
}
?>
好,分頁函數已經寫好了,保存為"pageft.php",要分頁時就包含它並調用pageft()函數。不過它並沒有輸出任何東西,但產生幾個總體變數供使用:$firstcount、$displaypg、$pagenav。


下面舉例說明它的用法:




<?
//(前面程式略)
include("pageft.php"); //包含"pageft.php"文件
//取得總資訊數
$result=mysql_query("select * from mytable");
$total=mysql_num_rows($result);
//調用pageft(),每頁顯示10條資訊(使用預設的20時,可以省略此參數),使用本頁URL(預設,所以省略掉)。
pageft($total,10);
//現在產生的總體變數就派上用場了:
$result=mysql_query("select * from mytable limit $firstcount,$displaypg ");
while($row=mysql_fetch_array($result)){
//(列表內容略)
}
//輸出分頁導航條代碼:
echo $pagenav;
//(後面程式略)
?>

備註:
發現文章有點問題,無法正確顯示後面分頁,因為程式根本無法獲取$page變數,解決方法:
include("pageft.php"); //包含“pageft.php”文件
$page=$_GET['page'];//加上這句即可


又一個錯誤,當數據記錄為0時,php報錯,加入以下代碼:

$total=mysql_num_rows($result);
if ($total==0 )die("暫時沒有資訊!");//如果記錄為0


參考資料:http://big5.china.com/gate/big5/zehao123.blog.china.com/201103/7878436.html

(2)參考資料:http://peeress.pixnet.net/blog/post/27760247
潔西卡 寫的分頁函數。
Call sample:
#計算筆數先
$offset = $_REQUEST["offset"] == "" ? 0 ; $_REQUEST["offset"];
$mrows = $conn->Execute("select count(a_account) as T from $TABLE_NAME $WHERE_STR ");
$mrow = $mrows->FetchObject();
$numrows = $mrow->T;
$pagestr = pages($numrows, $offset, $num_of_rows_per_page,"type=$type","no9", 2);
print $pagestr; // 印出分頁
$rows = $conn->SelectLimit($sql,$num_of_rows_per_page,$offset);
while ($row = $rows->fetchRow()) {
// 略 這裡是SHOW 資料
}
print $pagestr; // 印出分頁


結束
############# 下面是函數 弄成一個INCLUDE FILE 吧 ############
function pages($total_rows, $offset, $limit_row,$url_str='', $class="page", $mod="2") {
$current_page = ($offset/$limit_row) + 1;
$total_pages = ceil($total_rows/$limit_row);
if ($mod == "1") {
$current_page = ($offset/$limit_row) + 1;
$total_pages = ceil($total_rows/$limit_row);
$str2 = "";
if ($offset != 0) $str2 .="| <a href=\"$PHP_SELF?offset=0&$url_str\" class=\"$class\">FirstPage</a> | ";
if (($offset - $limit_row) >= 0) {
$prev_offset = $offset - $limit_row;
$str2 .= " <a href=\"$PHP_SELF?offset=$prev_offset&$url_str\" class=\"$class\">Previous</a> | ";
}
$str2 .= " [ $current_page / $total_pages ] ";
$last_row = (($total_pages-1) * $limit_row);
if (($offset + $limit_row) < $total_rows) {
$next_offset = $offset + $limit_row;
$str2 .= "<a href=\"$PHP_SELF?offset=$next_offset&$url_str\" class=\"$class\">NEXT</a> | ";
$str2 .= "<a href=\"$PHP_SELF?offset=$last_row&$url_str\" class=\"$class\">LastPage</a> | ";
}
}
elseif ($mod == "2") {

$str2 = "";
$i = ceil($current_page / 10 ) - 1 ;

if ($i >= 1 ) {
$of = max(0, $offset - ( $limit_row * 10)) ;
$str2.= "<a href=\"$PHP_SELF?offset=$of&$url_str\" class=\"$class\">上10頁</a> ";
}
$a=min($total_pages, ($i*10)+10);
for ($i = 1+($i*10); $i <= $a; $i++) {
$of = $i * $limit_row - $limit_row;
if ($i == $current_page)
$str2.= "[ $i ] ";
else
$str2.= "<a href=\"$PHP_SELF?offset=$of&$url_str\" class=\"$class\">$i</a> ";
}
if ($i < $total_pages ) {

$of = min($total_rows, $offset + ( $limit_row * 10));
$str2.= "<a href=\"$PHP_SELF?offset=$of&$url_str\" class=\"$class\">下10頁</a>";
}
}
else {
$str2 = "Page:";
for ($i =1; $i <= $total_pages; $i++) {
$of = $i * $limit_row - $limit_row;
if ($i == $current_page)
$str2.= "[ $i ] ";
else
$str2.= "<a href=\"$PHP_SELF?offset=$of&$url_str\" class=\"$class\">$i</a> ";
}

}
return "<span class=$class>$str2</span>";
}

備註:
  • $conn 是 adodb 的object
    如果你沒有使用adodb
    你應該是要改成

    $mrows = $conn->Execute("select count(a_account) as T from $TABLE_NAME $WHERE_STR ");
    $mrow = $mrows->FetchObject();
    $numrows = $mrow->T;
    上面這三行程式.

    你要改成下面


    $mrows = mysql_query("select count(a_account) as T from $TABLE_NAME $WHERE_STR);
    $mrow = mysql_fetch_object($mrow);
    $numrows = $mrow->T;
  • $mrow = mysql_fetch_object($mrow); 裡面是放$mrows
TOP

[C#] MS Chart Line

         private void CreateChart(DataTable dt)
        {
            // xValues 為日期; yValues 為點擊數量;
            string[] xValues = new string[dt.Rows.Count];
            int[] yValues = new int[dt.Rows.Count];

            int rowCount = 0;
            foreach (DataRow row in dt.Rows)
            {
                xValues[rowCount] = row["Date"].ToString();
                yValues[rowCount] = Convert.ToInt32(row["Count"]);
                rowCount++;
            }

            Chart1.Legends.Add("Legends1"); //圖例集合說明
            Chart1.Legends["Legends1"].DockedToChartArea = "ChartArea1"; //顯示在圖表內
            //Chart1.Legends["Legends1"].Docking = Docking.Bottom; //自訂顯示位置
            Chart1.Legends["Legends1"].BackColor = Color.FromArgb(235, 235, 235); //背景色
            //斜線背景
            Chart1.Legends["Legends1"].BackHatchStyle = ChartHatchStyle.DarkDownwardDiagonal;
            Chart1.Legends["Legends1"].BorderWidth = 1;
            Chart1.Legends["Legends1"].BorderColor = Color.FromArgb(200, 200, 200);



            Chart1.ChartAreas["ChartArea1"].AxisY.ScaleBreakStyle.CollapsibleSpaceThreshold = 20;
            Chart1.ChartAreas["ChartArea1"].AxisY.ScaleBreakStyle.LineColor = Color.Red;
            Chart1.ChartAreas["ChartArea1"].BackGradientStyle = GradientStyle.VerticalCenter;

            //3D設定
            Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = false; //3D效果
            //Chart1.ChartAreas["ChartArea1"].Area3DStyle.IsClustered = true; //並排顯示
            //Chart1.ChartAreas["ChartArea1"].Area3DStyle.Rotation = 40; //垂直角度
            //Chart1.ChartAreas["ChartArea1"].Area3DStyle.Inclination = 50; //水平角度
            //Chart1.ChartAreas["ChartArea1"].Area3DStyle.PointDepth = 30; //數據條深度
            //Chart1.ChartAreas["ChartArea1"].Area3DStyle.WallWidth = 0; //外牆寬度
            //Chart1.ChartAreas["ChartArea1"].Area3DStyle.LightStyle = LightStyle.Realistic; //光源

            Chart1.ChartAreas["ChartArea1"].BackColor = Color.FromArgb(240, 240, 240); //背景色
            //Chart1.ChartAreas["ChartArea1"].AxisX.Enabled = AxisEnabled.False; //隱藏 X 標示
            //Chart1.ChartAreas["ChartArea1"].AxisY.Enabled = AxisEnabled.False; //隱藏 Y 標示
            Chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.Enabled = false;   //隱藏 X 軸線
            //Chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.Enabled = false;   //隱藏 Y 軸線


            //Y 軸線顏色
            Chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineColor = Color.FromArgb(150, 150, 150);
            //X 軸線顏色
            Chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineColor = Color.FromArgb(150, 150, 150);

            // 讓 X 軸的座軸值能完全 show 出來
            Chart1.ChartAreas["ChartArea1"].AxisX.IntervalAutoMode = IntervalAutoMode.VariableCount;
            Chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle.IsStaggered = false;
            // 讓 Y 軸的座軸值能完全 show 出來
            //Chart1.ChartAreas["ChartArea1"].AxisY.IntervalAutoMode = IntervalAutoMode.VariableCount;
            //Chart1.ChartAreas["ChartArea1"].AxisY.LabelStyle.IsStaggered = false;
            Chart1.Series["Series1"].Legend = "Legends1";
            Chart1.Series["Series1"].IsValueShownAsLabel = false; //顯示數據

            //設定 Series1-----------------------------------------------------------------------
            Chart1.Series["Series1"].Points.DataBindXY(xValues, yValues); // 利用 DataBindXY 分別把數列的 X 及 Y 值繫結至不同的來源
            Chart1.Series["Series1"].LegendText = "Hits";
            //Chart1.Series["Series1"].LabelFormat = "#,###"; //金錢格式
            //顯示X軸資料
            //Chart1.Series["Series1"].LegendText = "#VALX";
            //顯示Y軸資料
            //Chart1.Series["Series1"].Label = "#VALY";

            //圖型
            Chart1.Series["Series1"].ChartType = SeriesChartType.Line;
            //Chart1.Series["Series1"].Color = Color.Blue;
            //Chart1.Series["Series1"].IsValueShownAsLabel = true;

            //字體顏色設定
            //Chart1.Series["Series1"].LabelForeColor = Color.Red;
            //Chart1.Series["Series1"].MarkerBorderColor = Color.Silver;
            //字體設定
            Chart1.Series["Series1"].Font = new System.Drawing.Font("Trebuchet MS", 10, System.Drawing.FontStyle.Bold);
            Chart1.Series["Series1"].BorderColor = Color.FromArgb(255, 101, 101, 101);
            //數值顯示在圓餅外
            //Chart1.Series["Series1"]["PieLabelStyle"] = "Outside";
            //設定圓餅效果,除 Default 外其他效果3D不適用
            ////Chart1.Series["Series1"]["PieDrawingStyle"] = "Default";
        }

參考資料:
MS Chart Control 學習手記(二) - 圓餅圖
http://www.dotblogs.com.tw/suehilary/archive/2011/10/24/46163.aspx
[修練營_MSChart, C#][005] 簡易的圖表「複製」功能
http://www.dotblogs.com.tw/nobel12/archive/2009/12/31/12750.aspx

ASP.NET 3.5 Chart Code With C# 使用範例
http://www.wretch.cc/blog/jakeuj/16431027
[立体,3D]MS Chart Control 學習手記(一) - 直條圖&橫條圖
http://www.cnblogs.com/-clq/archive/2012/02/09/2344273.html
C# Asp.net chart的圖表設定方式
http://fongzih.blogspot.tw/2011/09/c-aspnet-chart.html
[ASP.NET] MS Chart (2)
http://www.dotblogs.com.tw/shunnien/archive/2013/04/22/102049.aspx

MS Chart Control 學習手記(一) - 直條圖&橫條圖(這篇寫的非常的好)
http://www.dotblogs.com.tw/suehilary/archive/2011/10/22/45430.aspx

chart鼠标悬停时显示数据和checkBox的默认状态
http://blog.csdn.net/lllljz/article/details/7616935

C#chart柱形图时,如何设置可以在柱子顶上显示数值
http://bbs.csdn.net/topics/390522713?page=1#post-395081594

TOP

[C#] DataView RowFilter null 清除RowFilter的條件

RowFilter 完之後,如果需要再調用該Dataview的資料,要記得將RowFilter資料清除。

//利用RowFilter 去查詢需要的資料
dv.RowFilter = string.Format("Date = '{0}'", startDate.ToString("yyyy/MM/dd"));

//清除RowFilter的條件
dv.RowFilter = null;

如果沒有清除RowFilter的條件,使用dv時的資料,都只會有條件式下的資料。
TOP

[C#]DataView string and int sort 字串及數值排序

原本的Count為文字型態,在排序時會遇到排序不正確的問題。
因此需增加一個數字型態,再依數字型態排序。

//資料來源 DataSet
DataTable dt = ds.Tables["Log"];

//增加newCount欄位型態為Int32,資料來源從Count取得。
dt.Columns.Add("newCount", typeof(int), "Convert(Count,'System.Int32')");

//將資料轉存至DataView
DataView dv = dt.DefaultView;

//依數字型態提供排序使用
dv.Sort = "newCount DESC";

參考資料:http://blog.csdn.net/lin304510260/article/details/7852518
TOP

[C#]DataTable Top 10 Data 取10筆資料

//DataTable的資料來源 DataSet
DataTable dt = ds.Tables["Data"];

//建立一個新的DataTable dtTop10,並且將dt內10筆資料copy至dtTop10內。
//Take(10)就表示10筆。
DataTable dtTop10 = dt.Rows.Cast<DataRow>().Take(10).CopyToDataTable();

參考資料:
http://www.aspforums.net/Threads/550065/Get-Top-N-Rows-from-Datatable-or-DataSet-in-C/
TOP

[C#]DataTable New Row 新增一筆資料

//建立一個新的Datatable
DataTable dt = new DataTable();

//從DataSet取得資料,並存至DataTable的dt內
dt = ds.Tables["Data"];

//建立一筆新的DataRow,並且等於新的dt row
DataRow row = dt.NewRow();

//指定每個欄位要儲存的資料
      row["Date"] = startDate.ToString("yyyy/MM/dd");
      row["Count"] = "0";

//新增資料至DataTable的dt內
 dt.Rows.Add(row);    

參考資料:http://social.msdn.microsoft.com/Forums/zh-TW/07dda425-e1b7-4202-8fca-31c3c31a0159/datatable-

http://edward.msize.tw/2010/11/c-data-table.html