AJAX for PHP簡單表數(shù)據(jù)查詢實(shí)例
更新時間:2007年01月02日 00:00:00 作者:
功能介紹:AJAX WebShop 3從Beta2開始支持PHP的開發(fā)了,AJAX WebShop集成了PHP5的開發(fā)環(huán)境,因此不需要額外安裝配置PHP,本例將實(shí)現(xiàn)一個AJAX for PHP的簡單數(shù)據(jù)查詢操作,這個例子是單表操作,也可以實(shí)現(xiàn)主從表的數(shù)據(jù)查詢。
一.數(shù)據(jù)表說明
例子采用了Access數(shù)據(jù)庫,當(dāng)然你也可以使用mysql 或其他類型數(shù)據(jù)庫,數(shù)據(jù)庫名稱為:demo.mdb,表名為product,創(chuàng)建字段分別是PRODUCT_ID, PRODUCT_NAME, PRODUCT_PRICE, PRODUCT_AREA。
二.實(shí)現(xiàn)數(shù)據(jù)查詢
首先啟動AJAX WebShop 3,在File中選擇“New Project”建立新工程。
圖1
在彈出的New Project對話框中設(shè)置projectname 和web server本例中設(shè)置projectname為:php_example和設(shè)置web server為:PHP。如果要修改工程路徑,請?jiān)贒irectory中設(shè)置要存放的路徑。
圖2
設(shè)置好New Project后,再打開“File”選擇“New .PHP Service”,在彈出的對話框內(nèi)目錄輸入子目錄demo,設(shè)置”Class Name”為:simple_query;在ServiceType中選擇“Query Data”點(diǎn)擊“OK“后,向?qū)茨J(rèn)模板生成php單表查詢的代碼。
圖3
輸入以下數(shù)據(jù)庫連接及sql代碼(demo.mdb文件放在當(dāng)前代碼目錄下):$c.realpath($_SERVER['PATH_TRANSLATED'])."\demo.mdb";
$sql = "select * from product";
$sqlcount = "select count(*) from product";
分析testquery.php代碼,主要的實(shí)現(xiàn)如下:
1. 循環(huán)數(shù)據(jù)結(jié)構(gòu),用addField向$xmlRequest填充字段(列): //fill metadata fields
2. 循環(huán)結(jié)果集記錄,調(diào)用Append() ,SetValue方法,向xmlrequest每一行對應(yīng)字段填充數(shù)據(jù): //fill data rows
3. 設(shè)置分頁的相關(guān)參數(shù),$xmlRequest->recNo是開始記錄,$xmlRequest->maxRows是每頁記錄數(shù),通過執(zhí)行"select count(*) from product"得到$record_count記錄總數(shù):
未完)
原文出自http://cn.joyistar.com
一.數(shù)據(jù)表說明
例子采用了Access數(shù)據(jù)庫,當(dāng)然你也可以使用mysql 或其他類型數(shù)據(jù)庫,數(shù)據(jù)庫名稱為:demo.mdb,表名為product,創(chuàng)建字段分別是PRODUCT_ID, PRODUCT_NAME, PRODUCT_PRICE, PRODUCT_AREA。
二.實(shí)現(xiàn)數(shù)據(jù)查詢
首先啟動AJAX WebShop 3,在File中選擇“New Project”建立新工程。
圖1
在彈出的New Project對話框中設(shè)置projectname 和web server本例中設(shè)置projectname為:php_example和設(shè)置web server為:PHP。如果要修改工程路徑,請?jiān)贒irectory中設(shè)置要存放的路徑。
圖2
設(shè)置好New Project后,再打開“File”選擇“New .PHP Service”,在彈出的對話框內(nèi)目錄輸入子目錄demo,設(shè)置”Class Name”為:simple_query;在ServiceType中選擇“Query Data”點(diǎn)擊“OK“后,向?qū)茨J(rèn)模板生成php單表查詢的代碼。
圖3
輸入以下數(shù)據(jù)庫連接及sql代碼(demo.mdb文件放在當(dāng)前代碼目錄下):$c.realpath($_SERVER['PATH_TRANSLATED'])."\demo.mdb";
$sql = "select * from product";
$sqlcount = "select count(*) from product";
分析testquery.php代碼,主要的實(shí)現(xiàn)如下:
1. 循環(huán)數(shù)據(jù)結(jié)構(gòu),用addField向$xmlRequest填充字段(列): //fill metadata fields
復(fù)制代碼 代碼如下:
for ($i=1; $i<=$fields_count; $i++) {
$fieldname = odbc_field_name($result_id, $i);
$datatype = odbc_field_type($result_id, $i);
$xmlRequest->addField($fieldname, $datatype);
}
$fieldname = odbc_field_name($result_id, $i);
$datatype = odbc_field_type($result_id, $i);
$xmlRequest->addField($fieldname, $datatype);
}
2. 循環(huán)結(jié)果集記錄,調(diào)用Append() ,SetValue方法,向xmlrequest每一行對應(yīng)字段填充數(shù)據(jù): //fill data rows
復(fù)制代碼 代碼如下:
for ($i=0; $i<$record_count; $i++) {
odbc_fetch_row($result_id);
if($i>=$recNo && $i<$recNo+$maxRows) {
$xmlRequest->append();
for ($j=1; $j<=$fields_count; $j++) {
$xmlRequest->setValueByIndex($j-1, odbc_result($result_id, $j));
}
}
if($i>=$recNo+$maxRows) break;
}
odbc_fetch_row($result_id);
if($i>=$recNo && $i<$recNo+$maxRows) {
$xmlRequest->append();
for ($j=1; $j<=$fields_count; $j++) {
$xmlRequest->setValueByIndex($j-1, odbc_result($result_id, $j));
}
}
if($i>=$recNo+$maxRows) break;
}
3. 設(shè)置分頁的相關(guān)參數(shù),$xmlRequest->recNo是開始記錄,$xmlRequest->maxRows是每頁記錄數(shù),通過執(zhí)行"select count(*) from product"得到$record_count記錄總數(shù):
復(fù)制代碼 代碼如下:
$sqlcount = "select count(*) from product";
$result_id = @odbc_do($connid, $sqlcount);
if($result_id==null)
throw new Exception($sqlcount);
odbc_fetch_row($result_id);
$record_count = odbc_result($result_id,1);
$xmlRequest->setRecordCount($record_count);
$recNo = $xmlRequest->recNo;
$maxRows = $xmlRequest->maxRows;
if($maxRows==-1) $maxRows = $record_count;
后臺數(shù)據(jù)訪問類建立好后,在“File”中選擇“New Page”打開“New Page”對話框在“File Name”中設(shè)置頁面名稱,如本例“simple.htm”點(diǎn)擊ok完成設(shè)置。 $result_id = @odbc_do($connid, $sqlcount);
if($result_id==null)
throw new Exception($sqlcount);
odbc_fetch_row($result_id);
$record_count = odbc_result($result_id,1);
$xmlRequest->setRecordCount($record_count);
$recNo = $xmlRequest->recNo;
$maxRows = $xmlRequest->maxRows;
if($maxRows==-1) $maxRows = $record_count;
未完)
原文出自http://cn.joyistar.com
您可能感興趣的文章:
- PHP+Ajax 網(wǎng)站SEO查詢工具 提供代碼
- php+mysql+ajax實(shí)現(xiàn)單表多字段多關(guān)鍵詞查詢的方法
- PHP中使用jQuery+Ajax實(shí)現(xiàn)分頁查詢多功能操作(示例講解)
- ThinkPHP使用心得分享-ThinkPHP + Ajax 實(shí)現(xiàn)2級聯(lián)動下拉菜單
- php+ajax做仿百度搜索下拉自動提示框(有實(shí)例)
- PHP+Jquery與ajax相結(jié)合實(shí)現(xiàn)下拉淡出瀑布流效果【無需插件】
- 使用PHP+MySql+Ajax+jQuery實(shí)現(xiàn)省市區(qū)三級聯(lián)動功能示例
- PHP+原生態(tài)ajax實(shí)現(xiàn)的省市聯(lián)動功能詳解
- Thinkphp結(jié)合AJAX長輪詢實(shí)現(xiàn)PC與APP推送詳解
- php+ajax實(shí)現(xiàn)仿百度查詢下拉內(nèi)容功能示例
相關(guān)文章
PHP編程最快明白(第一講 軟件環(huán)境和準(zhǔn)備工作)
PHP我使用的是ESSamp集成開發(fā)環(huán)境,版本PHP5以上,代碼經(jīng)windowsXP系統(tǒng)調(diào)試通過。2010-10-10用PHP將數(shù)據(jù)導(dǎo)入到Foxmail
用PHP將數(shù)據(jù)導(dǎo)入到Foxmail...2006-10-10php auth_http類庫進(jìn)行身份效驗(yàn)
前提是要安裝auth類庫和auth_http類庫2009-03-03PHP調(diào)用三種數(shù)據(jù)庫的方法(3)
PHP調(diào)用三種數(shù)據(jù)庫的方法(3)...2006-10-10