SmartyʵÀý½Ìѧ ʵÀýƪ

5年以前  |  阅读数:378 次  |  编程语言:PHP 

SmartyʵÀý½Ìѧ ʵÀýƪ£¨Èý¡¢Ê¹ÓÃADODBÁ¬½ÓÊý¾Ý¿a£(C)

Ç°Á½¸oÔÂÒoΪ¹¤×÷ÉϵÄÔ­ÒoÒ»Ö±ºÜæ,ËuÒÔûÓм°Ê±Íe³ÉÕa¸o½Ì³Ì,ÕýºÃ½ñÌiÖÜÁu²»ÓüӰa,³e¸o¿ÕÍe³ÉËu°É! ÔÚ¿ªÊ¼ÐµĵĽ̵̳Äʱºo,ÎÒ

ÏÈ°ÑÒÔÇ°µÄÎÒдµÄÄǸo½Ì³ÌÖеÄÒ»Ð(C)´iÎoµÄµØ½Ð޸ĹýÀ´,ÔÚÕaÀiÒª¸Ðл nesta2001zhangÐÖµÜ,ÊÇËuÕÒ³oÁËÎÄÕÂÖеÄÒ»Ð(C)´iÎo,ñÔoÕaeµÄ±»±ðÈË

Âi"ÎoÈË×ÓµÜÁË"(˵À´ÕaeÊDzÑÀ¢,Îҵijo¸a¢²¼ºoºo¾Í¢ÏÖÔÚÒ»´o¶ÑµÄÎÊÌa,ºoÀ´Ò»Ð(C)ʱºo*¢ÖØÐÂÐ޸ĺoµÄÎļþÖоÓȻҲ³oÏÖÁË´iÎo,ÕaeÊDz»Ó¦

¸Ã...)
ÔÚÉϼ¸Æª½Ì³ÌÖеģº

while($db->next_record() && $i > 0)
{
$array[] = array("NewsID", csubstr($db->f("iNewsID"), 0, 20),
"NewsTitle", csubstr($db->f("vcNewsTitle"), 0, 20));

$i--;
}

Ó¦¸Ã¸u¸ÄΪ£º

while($db->next_record() && $i > 0)
{
$array[] = array("NewsID" => $db->f("iNewsID"),
"NewsTitle" => csubstr($db->f("vcNewsTitle"), 0, 20));

$i--;
}

ΪʲôÕaÑu¸ÄÄØ?ÒoΪµÚ¶þÖÖ½¨¸uÇaÎuÃ÷ÁËÒ»Ð(C),ʵ¼ÊÉϵÚÒ»ÖֽʽËuÖ´ÐеÄЧ¹uÓeµÚ¶þÖÖ½*¨Ã»Ê²Ã´²i±ð,¶øÇÒÄǼ¸¸o³ÌÐoÎÒ¶¼Ôø¾­µ÷ÊÔ¹ý,

ûÓÐÈκÎÎÊÌa.
ºÃÁË,ÄÇÎÒÃǽñÌi¾ÍÏÈÀ´ËµËµADODB.˵µ½ADODB,¿ÉÄÜ×o¹ýASPµÄ¶¼ÖªµÀWINDOWSƽ̨µÄADO×e¼þ,µ«ÎÒÃÇÕaÀiµÄADODB²»ÊÇ΢ÈiµÄÄǸoÊý¾Ý¿a²Ù

×÷×e¼þ,¶øÊÇÓÉphpÓiÑÔдµÄÒ»Ì×Êý¾Ý¿a²Ù×÷Àa¿a,ÏÈÈÃÎÒÃÇÀ´¿´¿´Ëuµ¹µ×ÓÐʲôÑuµÄÓŵa.
1. ÒÔ±e×¼µÄSQLÓi¾aÊeдµÄÊý¾Ý¿aÖ´ÐдuÂeÔÚ½øÐÐÊý¾Ý¿aÒÆֲʱ²»Óøu¸ÄÔ´³ÌÐo,Ò²¾ÍÊÇ˵Ëu¿ÉÒÔÖ§³Ö¶aÖÖÊý¾Ý¿a,°uÀ¨ACCESS.
2. Ìa¹(C)Óe΢ÈiADODBÏaËƵÄÓi¨¹¦ÄÜ.ÕaÒ»µa¶ÔÓÚ´ÓASPתÐе½PHPµÄÈËÃÇÊÇÒ»´o¸£Òo,ËuµÄºÜ¶a²Ù×÷¶¼ÓeWINDOWSÖеÄADODBÏaËÆ.
3. ¿ÉÒÔÉu³ÉSmartyÑ­»
ÐeÒªµÄ¶þάÊý×e,ÕaÑu»a¼o»¯smarty¿ª¢.ÕaÒ»µaÊǵȻaÎÒ¸ø´o¼ÒÑÝʾ.
4. Ö§³ÖÊý¾Ý¿aµÄ»º´ae²eѯ£¬×i´o¿ÉÄܵÄÌa¸ß²eѯÊý¾Ý¿aµÄËٶȡ£
5. ÆaËuµÄʵÓù¦ÄÜ.
ËaȻ˵ÓŵaºÜ¶a,µ«ÊÇÓÉÓÚÕa¸oÀa¿a
dz£µÄÅÓ´o,¹aËuµÄÖ÷Ö´ÐÐÀa¾Í107K,ËuÒÔÈç¹u´o¼Ò¿¼ÂÇÖ´ÐÐЧÂʵĻ°¾ÍÒªÈÏÕaeÏeÏeÁË.²»¹ý˵ʵ»°,ËuµÄ

¹¦ÄÜ»¹ÊǺÜÇ¿´oµÄ,ÓкܶaµÄºÜʵÓõŦÄÜ,ʹÓÃËuµÄÕaÐ(C)¹¦ÄÜ,¿ÉÒÔdz£½±aµÄʵÏÖÎÒÃÇÏeÒªµÄ¹¦ÄÜ.ËuÒÔ¶ÔÓÚÄÇÐ(C)ÀÏ°aûÓÐÌØÊaÒªÇoʱ´o¼Ò²»

*ÀÓÃÓÃËu

Ò»¡¢ÈçºÎµÃµ½ADODB? ËuµÄÔËÐл*¾³ÊÇʲô£¿
´Óhttp://sourceforge.net/project/show...ºphp4.0.5ÒÔÉÏ¡£
¶þ¡¢ÈçºÎ°²×°ADODB?
½aѹÏÂÔػصÄѹËoÎļþ£¬×¢Òa£º´o¼ÒÏÂÔØ»ØÀ´µÄ¸ñʽΪADODB.tar.gz£¬ÕaÊÇlinuxµÄѹËo¸ñʽ£¬ÔÚwindowsÏ´o¼Ò¿ÉÒÔʹÓÃwinrar¶ÔÆa½ø

Ðнaѹ£¬½aѹÍe³Éºo½«Ä¿Â¼¿½±´µ½Ö¸¶¨µÄĿ¼µÄadodbĿ¼Ï£¬ÏñÎÒÔÚÀý×ÓÖн«Ëu¿½±´µ½ÁË/comm/adodb/ÖС£
Èý¡¢ÈçºÎµ÷ÓÃADODB?
ʹÓÃinclude_once ("./comm/adodb/adodb.inc.php");ÕaÐоͲ»ÓÃ˵ÁË°É£¿°uº¬ADODBµÄÖ÷Îļþ¡£
ËÄ¡¢ÈçºÎʹÓÃADODB?
1.½øÐгoʼ»¯£º
ADODB²ÉÓÃ$conn = ADONewConnection();ÕaÑuµÄÓi¾a½øÐгoʼ»¯,¶ÔADODB½øÐгoʼ»¯ÓÐÁ½Öֽʽ£º
µÚÒ»ÖÖ
½Ê½Îª£º´«Í³½Ê½¡£ÎÒÔÝʱ³ÆËuΪÕa¸oÃu³Æ¡£ËuʹÓõĽ¨Á¢Ò»¸oÐÂÁ¬½ÓµÄ½Ê½ºÜÏñphpÖеıe×¼Á¬½Ó*½Ê½£º
$conn = new ADONewConnection($dbDriver);
$conn->Connect($host, $user, $passwd, $db);
¼oµ¥°É£¿Èç¹uʹÓùýphplibÖеÄdbÀaÓ¦¸Ã¶ÔËuºÜÊiϤµÄ¡£

µÚ¶þÖֽʽ£º²ÉÓÃdsn½Ê½£¬ÕaÑuÊǽ«Êý¾Ý¿aµÄÁ¬½ÓÓi¾aд³ÉÒ»ÌoÓi¾aÀ´½øÐгoʼ»¯£¬dsnµÄд*¨ÓÐΪ£º$dsn =

"DBType://User:Passwd@Host/DBName"; ÆaÖÐDBType±iʾÊý¾Ý¿aÀaÐÍ£¬User±iʾÓû§Ãu£¬PasswdΪÃÜÂe£¬HostΪ*þÎñÆ÷Ãu£¬DBNameΪÊý¾Ý¿aÃu

£¬ÏñÕaÑuÎÒʹÓÃoracleÊý¾Ý¿a£¬Óû§Ãu£ºoracleUser,ÃÜÂeΪoraclePasswd,Êý¾Ý¿aþÎñÆ÷Ϊlocalhost, Êý¾Ý¿aΪoradbµÄdsnÕaÑuд£º
$dsn = "oracle://oracleUserraclePasswd@localhost/oradb";
$conn = new ADONewConnection($dsn);
ÕaÖÖ
½Ê½¿ÉÄÜ´ÓASPתÐÐÀ´µÄ³ÌÐoÔ±»a¸u¸ÐÐËȤ¡£

ÕaÁ½ÖÖ*½Ê½¶¼¿ÉÒÔʹÓã¬Òª¿´¸oÈËÏ°¹ßÀ´Ñ¡ÓÃÁË.

2. Ïa¹ØµÄ¸ÅÄi£º
ʹÓÃADODBÓÐÁ½¸o»u±¾µÄÀa£¬Ò»ÊÇÊÇADOConnectionÀa£¬ÁiÒ»¸oÊÇADORecordSetÀa£¬Ê¹ÓùýASPµÄÈË¿´µ½ÕaÁ½¸oÀa»aÃ÷°×ËuµÄº¬Òa£¬

ADOConnectionÖ¸µÄÊÇÊý¾Ý¿aÁ¬½ÓµÄÀa£¬¶øADORecordSetÖ¸µÄÊÇÓÉADOConnectionÖ´ÐвeѯÓi¾a*µ»ØµÄÊý¾Ý¼¯Àa£¬Ïa¹ØµÄ×ÊÁÏ´o¼Ò¿ÉÒÔ²eѯADODB

ÀaµÄÊÖ²a¡£

3.»u±¾µÄº¯Êý£º

¹ØÓÚADOConnectionÀaµÄÏa¹Ø½¨ÓУº
1.Connect£ºÊý¾Ý¿aÁ¬½Ó½¨£¬ÉϱßÎÒÃǽeÉܹýµÄ¡£¶ÔÓÚmysql»¹ÓÐPConnect£¬ÓePHPÓiÑÔÖеÄÓèһÑu
2.Execute($sql):Ö´ÐвeѯÓi¾a½a¹u
µ»ØÒ»¸oADORecordSetÀa¡£
3.GetOne($sql):µ»ØµÚÒ»ÐеĵÚÒ»¸o×Ö¶Î
4.GetAll($sql):
µ»ØËuÓеÄÊý¾Ý¡£Õa¸oº¯Êý¿ÉÊÇ´oÓÐÓô¦£¬¼ÇµÃ²»¼ÇµÄÎÒÔÚÒÔÇ°µÄ½Ì³ÌÖÐд¹ØÓÚÐÂÎÅÁбiµÄÊaÈeʱҪ½«ÐeÒªÔÚÒ³ÃaeÏÔʾµÄ

ÐÂÎÅÁбi×o³ÉÒ»¸o¶þάÊý×e£¿¾ÍÊÇÕaÑuµÄÓi¾a£º

while($db->next_record())
{
$array[] = array("NewsID" => $db->f("iNewsID"),
"NewsTitle" => csubstr($db->f("vcNewsTitle"), 0, 20));
}

ÕaÒ»ÐÐÊÇʲôÒa˼ÄØ£¿¾ÍÊǽ«ÒªÏÔʾµÄÐÂÎÅÀý±iÉu³É
$array[0] = array("NewsID"=>1, "NewsTitle"=>"ÕaÀiÐÂÎŵĵÚÒ»Ìo");
$array[1] = array("NewsID"=>2, "NewsTitle"=>"ÕaÀiÐÂÎŵĵڶþÌo");
...
ÕaÑuµÄÐÎʽ£¬µ«Èç¹uÎÒÃDz»ÐeÒª¶Ô±eÌa½øÐпØÖÆ£¬ÔÚADODBÖÐÎÒÃǾÍÓи£ÁË£¬ÎÒÃÇ¿ÉÒÔÕaÑuд£º

$strQuery = "select iNews, vcNewsTitle from tb_news_ch";
$array = &$conn->GetAll($strQuery);//×¢ÒaÕaÌoÓi¾a
$smarty->assign("News_CH", $array);
unset($array);

µ±È»£¬ÕaÀiµÄ$connÓ¦¸Ã½øÐгoʼ»¯¹ýÁË£¬²»Öª´o¼Ò¿´Ã÷°×ÁËûÓУ¿Ô­À´ÎÒÒªÊÖ¹¤´´½¨µÄ¶þάÊý¾ÝÔÚÕaÀiÖ±½ÓʹÓÃGetAll¾ÍÐÐÁË£¡£¡£¡ÕaÒ²ÊÇΪ

ʲôÓÐÈË»a˵ADODB+SmartyÊÇÎÞµÐ×eºÏµÄÔ­ÒoÖ®Ò»ÁË...
4.SelectLimit($sql, $numrows=-1, $offset=-1, $inputarrr=false): *µ»ØÒ»¸oÊý¾Ý¼¯£¬´o¼Ò´ÓÓi¾aÉÏÒ²²»ÄÑ¿´³oËuÊÇÒ»ÌoÏÞÁ¿²eѯÓi

¾a£¬ÓemysqlÓi¾aÖеÄlimit ÓÐÒiÇuͬ¹¤Ö®Ð§£¬À´Ò»¸o¼oµ¥µÄÀý×Ó£º
$rs = $conn->SelectLimit("select iNewsID, vcNewsTitle from tb_news_CH", 5, 1);
¿´Ã÷°×ÁËÂð£¿$rsÖб£´aeµÄÊÇÊý¾Ý¿aÖдӵÚÒ»¼Ç¼¿ªÊ¼µÄ5Ìo¼Ç¼¡£ÎÒÃÇÖªµÀ£¬ÔÚoracleÊý¾Ý¿a²»Ö§³ÖÔÚSQLÓi¾aÖÐʹÓÃlimit£¬µ«ÊÇÎÒÃÇÈç¹uʹ

ÓÃADODBµÄ»°£¬ÄÇÕa¸oÎÊÌa¾ÍÈÝÒ×½a¾o¶aÁË£¡
5.Close():¹Ø±ÕÊý¾Ý¿a£¬ËaȻ˵PHPÔÚÒ³Ãae½aÊøʱ»a×Ô¶¯¹Ø±Õ£¬µ«ÎªÁ˳ÌÐoµÄÍeÕu´o¼Ò»¹ÊÇÒªÔÚÒ³Ãae½aÊø½øÐÐÊý¾Ý¿aµÄ¹Ø±Õ¡£

¹ØÓÚADORecordSet.ADORecordSetΪ$conn->Execute($sql)µ»ØµÄ½a¹u,ËuµÄ»u±¾º¯ÊýÈçÏÂ:
1. Fields($colname):
µ»Ø×ֶεÄÖµ.
2. RecordCount():Ëu°uº¬µÄ¼Ç¼Êý.Õa¸o¼Ç¼ȶ¨Êý¾Ý¼¯µÄ¼Ç¼×ÜÊý.
3. GetMenu($name, [$default_str=''], [$blank1stItem=true], [$multiple_select=false], [$size=0], [$moreAttr=''])
dz£ºÃµÄÒ»

¸oº¯Êý,ʹÓÃËu¿ÉÒÔµ»ØÒ»¸oname=$nameµÄÏÂÀ­²Ëµ¥(»o¶aÑ¡¿o)!!!µ±È»,ËuÊÇÒ»¸oHTMLµÄ×Öu´®,ÕaÊÇÒ»¸oÁiÈ˼¤¶¯µÄºÃ¶«Î÷,$nameÖ¸µÄÊÇ

optionµÄnameÊoÐÔ,$default_strÊÇĬÈÏÑ¡ÖеÄ×Ö´®,$blank1stItemÖ¸³oµÚÒ»ÏiÊÇñΪ¿Õ,$multiple_selectÖ¸³oÊÇñΪ¶aÑ¡¿o,¶øÎÒÃǵõ½Õa¸o

×Ö´®ºo¾Í¿ÉÒÔʹÓÃ$smarty->("TemplateVar", "GetMenuStr")À´ÔÚÄ£°aµÄ"TemplateVar" ´¦ÊaÈeÒ»¸oÏÂÀ­Áбi(»oÊǶaÏÈ¿o)
4. MoveNext():À´¿´Ò»¶Î´uÂe:

$rs = &$conn->Exceute($sql);
if($rs)
{
while($rs->EOF)
{
$array[] = array("NewsID" => $rs->fields["iNewsID"],
"NewsTitle" => csubstr($rs->fields["vcNewsTitle"]), 0, 20);

$rs->MoveNext();
}
}

Ã÷°×ÁËÂð?ºÜÏñMS ADODBÖеÄÄÇÒ»Ì×Âi!
5. MoveFirst(),MoveLast(), Move($to):Ò»ÑuµÄ,¿´º¯ÊýÃu´o¼Ò¾Í¿ÉÒÔÖªµÀËuÊÇʲôÒa˼ÁË.
6. FetchRow():*µ»ØÒ»ÐÐ,¿´´uÂe:

$rs = &$conn->Exceute($sql);
if($rs)
{
while($row = $rs->FetchRow())
{
$array[] = array("NewsID" => $row["iNewsID"],
"NewsTitle" => csubstr($row["vcNewsTitle"]), 0, 20);
}
}

ËuʵÏÖÁËÓe4Ò»ÑuµÄ¹¦ÄÜ,µ«¿´ÆðÀ´¸uuºÏPHPµÄÏ°¹ß,¶ø4µÄÏ°¹ß¿´ÆðÀ´¸uÏñÊÇMS ADODBµÄ°i¨£®

7.GetArray($num):µ»ØÊý¾Ý¼¯ÖеÄ$numÐÐÊý¾Ý,½«Æa×eºÏ³É¶þάÊý×e.Õa¸o½*¨ÎÒÃÇÔÚÀý×Óindex.phpÒªÓõ½.

8. Close():ͬmysql_free_result($rs);Ça³ýÄÚÈÝÕ¼ÓÃ.

ºÃÁË,³o²½µÄº¯Êý¾Í½eÉܵ½ÕaÀi,¹»ÎÒÃÇÓõÄÀ²!ʵ¼ÊÉÏADODB»¹ÓкܶaʵÓõļ¼Êo,°uÀ¨¸ñʽ»¯ÈÕÆÚʱ¼a,¸ñʽ»¯²eѯÓi¾a,Êa³o±i¸ñ,¸u¸ß¼¶

µaµÄCache²eѯ,´ø²Î²eѯµÈµÈ,´o¼Ò¿ÉÒÔ×ÔÐвe¿´ÊÖ²a.

ÏÂÃaeÎÒÃÇ¿ªÊ¼Ñ§Ï°ÎÒÃǵijÌÐo,ͬÑu»¹ÊÇÄǸoWeb³ÌÐo,ÎÒ½«ÆaÖеÄcommĿ¼ÖØÐÂ×eÖ¯ÁËÒ»ÏÂ,ͬʱΪÁËÌa¸ßЧÂʶÔSmartyÖØнøÐÐÁË*a×°

,mySmarty.class.phpÊÇ*a×°ºoµÄÀa,Ëu¼Ì³Ð×ÔSmarty,ËuÒÔÒÔºoËuÓеijÌÐoÎļþÖÐÖ»µ÷ÓÃеÄÀaMySmarty,ÏÈÀ´¿´¿´Ä¿Â¼½a¹¹:
+Web (Õ¾µa¸uĿ¼)

|----+comm (SmartyÏa¹ØÎĵµÄ¿Â¼)

| |----+smarty (SmartyԭʼÎļþĿ¼)
| |----+adodb (adodbԭʼÎÄĿ¼)
| |-----mySmarty.class.php (À(C)Õ¹ºoµÄsmartyÎļþ)
| |-----csubstr.inc (½ØÈ¡ÖÐÎÄ×Ö*u)

|----+cache (Smarty»º´aeĿ¼£¬*nixϱ£Ö¤¶ÁдȨÏÞ)

|----+templates (Õ¾µaÄ£°aÎļþ´ae*ÅĿ¼)

| |----header.tpl(Ò³ÃaeÒ³Í*Ä£°aÎļþ)
| |----index.tpl(Õ¾µaÊ×ҳģ°aÎļþ)
| |----foot.tpl(Ò³ÃaeÒ³½ÅÄ£°aÎļþ)
| |----news.tpl (ÐÂÎÅҳģ°aÎļþ)

|
|----+templates_c (Ä£°aÎļþ±aÒeºo´aeÅĿ¼£¬nixϱ£Ö¤¶ÁдȨÏÞ)

|----+css (Õ¾µaCSSÎļþĿ¼)

|----+image (Õ¾µaͼƬĿ¼)

|----+media (Õ¾µaFlash¶¯»­´ae*ÅĿ¼)

|----indexbak.htm (Ê×ҳԭʼЧ¹uͼ)

|----newsbak,htm (ÐÂÎÅҳԭʼЧ¹uͼ)

|----index.php (SmartyÊ×Ò³³ÌÐoÎļþ)

|----news.php (SmartyÐÂÎÅÏÔʾÎļþ)

|----newsList.php (ÏÔʾÐÂÎÅÁбi)

|----Àý³Ì˵Ã÷.txt (±¾Îĵµ)

Ïa¶ÔÓÚÇ°Á½¸o½Ì³Ì,Óн«commĿ¼ÖØÐÂ×eÖ¯ÁËÒ»ÏÂ,ÆaËuµÄÎļþ½a¹¹Ã»Óбa»¯,Õu¸oÕ¾µaÏa¶ÔÓÚÉÏÁ½¸o½Ì³ÌÀ´½²,¸Ä±aµÄµØ*½Ö»ÓÐcommĿ¼Óe

index.phpÓenews.php,ͬʱÔo¼ÓÁËÐÂÎÅÁбi,´o¼Ò¿ÉÒÔÔÚindex.phpÖ´ÐкoµÄÒ³ÃaeÖеa»÷"¹uÄÚÐÂÎÅ","¹u¼ÊÐÂÎÅ", "ÓeÀÖÐÂÎÅ"À´*Ö±ð²e¿´¸÷×ÔµÄ

ÐÂÎÅÁбi, ÎÒÃÇÏÈÀ´¿´¿´index.php:

======================================================
index.php

<?php
/*****

  • ÎļþÃu£º index.php
  • ×÷ Ó㺠ÏÔʾʵÀý³ÌÐo
  • ×÷ Õߣº ´oʦÐÖ
  • Email£º teacherli@163.com
  • *****/
    include_once("./comm/mySmarty.class.php"); //°uº¬smartyµÄÀ(C)Õ¹ÀaÎļþ
    include_once("./comm/adodb/adodb.inc.php"); //°uº¬ADODBÖ÷Ö´ÐÐÎļþ
    include_once("./comm/csubstr.inc"); //°uº¬ÖÐÎĽØÈ¡Àa

define ("NEWS_NUM", 5); //¶¨ÒaÐÂÎÅÁбiÏÔʾÊýÄ¿

$smarty = new MySmarty(); //½¨Á¢smartyʵÀý¶ÔÏo$smarty

1. $conn = ADONewConnection("mysql"); //³oʼ»¯ADODB
2. $conn->Connect("localhost", "root", "", "News"); //Á¬½ÓÊý¾Ý¿a

//ÕaÀi½«´¦Ài¹uÄÚÐÂÎŲ¿*Ö
3. $strQuery = "SELECT iNewsID AS NewsID, vcNewsTitle AS NewsTitle FROM tb_news_CH ORDER BY iNewsID DESC";
4. $rs = &$conn->Execute($strQuery);
5. $smarty->assign("News_CH", $rs->GetArray(NEWS_NUM));
6. unset($rs);

//ÕaÀi´¦Ài¹u¼ÊÐÂÎŲ¿*Ö
$strQuery = "SELECT iNewsID AS NewsID, vcNewsTitle AS NewsTitle FROM tb_news_IN ORDER BY iNewsID DESC";
$rs = &$conn->Execute($strQuery);
$smarty->assign("News_IN", $rs->GetArray(NEWS_NUM));
unset($rs);

//ÕaÀi½«´¦ÀiÓeÀÖÐÂÎŲ¿*Ö
$strQuery = "SELECT iNewsID AS NewsID, vcNewsTitle AS NewsTitle FROM tb_news_MU ORDER BY iNewsID DESC";
$rs = &$conn->Execute($strQuery);
$smarty->assign("News_MU", $rs->GetArray(NEWS_NUM));
unset($rs);

7. $conn->close();

//±aÒe²¢ÏÔʾλÓÚ./templatesϵÄindex.tplÄ£°a
$smarty->display("index.tpl");
?>

ͬÑu,ÎÒÔڹؼuµÄµØ*½¼ÓÁËÊý±e,ÏÂÃaeÀ´ËµÃ÷Ò»ÏÂËuÃǵĺ¬Òa:

1. ½¨Á¢Ò»¸oÁ¬½Ó¶ÔÏo$conn,´o¼ÒÔÚÕaÀiҪעÒaµÄÊÇËuµÄ³oʼ²»ÊÇÒÔ$conn = new ADONewConnection($dbType)ÕaÑuµÄÐÎʽ³oÏÖµÄ,Ò²¾ÍÊÇ˵

,ADONewConnection²»ÊÇÒ»¸oclass,Äa²»ÄÜʹÓÃnew ¶ÔËu½øÐгoʼ»¯.¿´¿´ËuµÄÔ´ÂeÄa¾Í»aÃ÷°×,ÕaÖ»²»¹ýÊÇÒ»¸oº¯Êý.

2. Õa¸o¾Í²»ÓÃ˵ÁË°É?´o¿ªÒ»¸oNewsµÄÊý¾Ý¿a,Ö÷»uΪ:localhost, Óû§ÃuΪroot, ÃÜÂeΪ""

3. Ò»¸o²eѯÓi¾a,×¢Òa,ÕaÀiÒª½«²eѯµÄ×Ö¶ÎʹÓÃAS¹Ø¼u×ÖÀ´ÖØбeʶ,Ãu³ÆΪÄaÔÚÄ£°aÖÐÉeÖõÄÄ£°a±aÁ¿µÄÃu³Æ.

4. ʹÓÃExecuteÀ´Ö´ÐÐÕa¸o²eѯ,½a¹u*µ»ØÒ»¸oRecordSetÊý¾Ý¼¯

5. ÕaÀiÓиo½¨:$rs->GetArray($num) Õa¸oÔÚÉϱ߽eÉܹý,ËuÊÇÒª´Ó$rsÕa¸oÊý¾Ý¼¯ÖÐ*µ»Ø$numÐÐ,½a¹uΪһ¸o¿É±»SmartyËuʶ±ðµÄ¶þάÊý

¾Ý.ÕaÑuADODB¾Í×Ô¶¯ÎªÎÒÃǹ¹½¨ÆðÁËÕaÑuµÄ½a¹¹,¶øÔÚÎÒÃÇÒÔÇ°µÄÀý×ÓÖÐ,¶¼ÊÇʹÓÃÒ»¸oÑ­»*¹¹½¨ÕaÑuµÄÊý×eµÄ.

6. ÕaÒ»¾aÎÒ¿´Ò²²»ÓÃ˵ÁË°É?

7. ¹Ø±ÕÄÚ´aeÖеÄÏa¹Ø×ÊÔ´.

´o¼Ò¿ÉÒÔ¿´¿´,Õu¸o³ÌÐoÖÐÔÙûÓгoÏÖʲôwhileÓi¾a,³ÌÐoÕuÌa½a¹¹ÏÔµÄ*dz£Ça³þ,Õa¾ÍÊÇΪʲôADODB+SmartyÊǻƽð×eºÏµÄÔ­Òo.²»¹ý»°Ò²

˵»ØÀ´ÁË,¼oµ¥Óмoµ¥µÄÎÊÌa,²»Öª´o¼ÒÏe¹ýûÓÐ,ÕaÀi¶ÔÏÔʾµÄÐÂÎűeÌaµÄ³¤¶ÈûÓпØÖÆ,Ò²¾ÍÊÇ˵,Èç¹uijÌoÐÂÎűeÌaµÄ³¤¶È³¬³oÒ»ÐÐÏÔʾµÄ*¶

Χ,Ëu¾ÍÊÇ×Ô¶¯ÕÛÐе½ÏÂÒ»ÐÐ,ÄÇôÕu¸oµÄ°aeÃae¾Í»a±aÂÒ,Ëu˵´o¼Ò×ÔÒÑÊÊ×Ô¼ºµÄÇe¿oÀ´¾o¶¨ÊÇ*ñÕaÑuʹÓðɵ±È»,ÄaÒ²¿ÉÒÔʹÓÃÏñÉÏÒ»½ÚÖнe

ÉܵÄÄÇÑu,ʹÓÃÒ»¸oÑ­»Ói¾aÖع¹Õa¸o¶þάÊý×e,ʹËuuºÏÄaµÄÓÃ;,Ôoô×o´o¼Ò×Ô¼ºÈ¥Ïe°É,²Î¿¼PHPLIBÖеÄ×o*¨,ÉϽÚÎÒ½eÉܹýÁË...

ÔÙÀ´¿´¿´ÐÂÎÅÒ³°É

=============================================================
news.php

<?php
/*****

  • ÎļþÃu£º news.php
  • ×÷ Ó㺠ÐÂÎÅÏÔʾ³ÌÐo
  • ×÷ Õߣº ´oʦÐÖ
  • Email£º teacherli@163.com
  • *****/
    include_once("./comm/mySmarty.class.php"); //°uº¬smartyµÄÀ(C)Õ¹ÀaÎļþ
    include_once("./comm/adodb/adodb.inc.php"); //°uº¬ADODBÖ÷Ö´ÐÐÎļþ

$smarty = new MySmarty(); //½¨Á¢smartyʵÀý¶ÔÏo$smarty

$conn = ADONewConnection("mysql"); //³oʼ»¯ADODB
$conn->Connect("localhost", "root", "", "News"); //Á¬½ÓÊý¾Ý¿a

$NewsID = $_GET["id"]; //»ñÈ¡ÐÂÎűaºÅ
$NewsType = $_GET["type"]; //ÒªÏÔʾµÄÐÂÎÅÀaÐÍ
switch($NewsType)
{
case 1:
$dbName = "tb_news_CH";
break;
case 2:
$dbName = "tb_news_IN";
break;
case 3:
$dbName = "tb_news_MU";
break;
}

$strQuery = "SELECT vcNewsTitle AS NewsTitle, ltNewsContent AS NewsContent FROM " . $dbName;
1. $row = &$conn->GetRow($strQuery); //*µ»ØÒ»¸oһάÊý×e,ϱeΪģ°a±aÁ¿Ãu

$smarty->display($row);
unset($row);

$conn->Close();
?>

˵Ã÷һϹؼuµÄµØ½,ÆaʵÔÚnews.phpÖÐÒ²Ö»ÓÐÒ»¸oµØ½ÖµµÄ˵Ã÷Ò»ÏÂÁË.

1. $conn->GetRow($strQuery):ÕaÒ»¾aµ»ØÒ»¸oһάÊý×e,µ»ØµÄÐÎʽΪ:

$array = ("NewsTitle"=>"xxxx", "NewsContent"=>"yyyyy...")
Ã÷°×Èç¹uʹÓÃ$smarty($array)ºoSmarty»a¸ÉʲôÂð?¶ÔÁË,¾ÍÊÇÏaµ±ÓÚ:
$smarty->assign("NewsTitle", "xxxx");
$smarty->assign("NewsContent", "yyyyy...");

¼oµ¥°É,È*ʵºÜ¼oµ¥

ÏÂÃaeÔÙÀ´¿´¿´ÐÂÎÅÁбi:

newsList.php

<?php
/*****

  • ÎļþÃu£º newsList.php
  • ×÷ Ó㺠ÐÂÎÅÁбiÏÔʾ³ÌÐo
  • ×÷ Õߣº ´oʦÐÖ
  • Email£º teacherli@163.com
  • *****/
    include_once("./comm/mySmarty.class.php"); //°uº¬smartyµÄÀ(C)Õ¹ÀaÎļþ
    include_once("./comm/adodb/adodb.inc.php"); //°uº¬ADODBÖ÷Ö´ÐÐÎļþ

$smarty = new MySmarty(); //½¨Á¢smartyʵÀý¶ÔÏo$smarty

$conn = ADONewConnection("mysql"); //³oʼ»¯ADODB
$conn->Connect("localhost", "root", "", "News"); //Á¬½ÓÊý¾Ý¿a

$NewsID = $_GET["id"]; //»ñÈ¡ÐÂÎűaºÅ
$NewsType = $_GET["type"]; //ÒªÏÔʾµÄÐÂÎÅÀaÐÍ
switch($NewsType)
{
case 1:
$tbName = "tb_news_CH";
break;
case 2:
$tbName = "tb_news_IN";
break;
case 3:
$tbName = "tb_news_MU";
break;
}

$strQuery = "SELECT iNewsID AS NewsID, vcNewsTitle AS NewsTitle FROM " . $tbName;
1. $rs = &$conn->GetAll($strQuery);
2. $smarty->assign("NewsType", $NewsType); //ÕaÒ»¾aΪÐÂÎÅÁбiÖеÄÁ´½Ó*þÎñ
3. $smarty->assign("NewsList", $rs);
unset($rs);
$conn->close();

$smarty->display("newsList.tpl");
?>

*Ö±ðÀ´ËµÃ÷Ò»ÏÂ:

1. GetAll($strQuery):Õa¸oº¯Êý¿ÉÊǸoºÃ¶«¶«,ËuµÄ×÷ÓÃÊǽ«$strQuery²eѯµ½µÄËuÓÐÊý¾Ý×eºÏ³ÉΪһ¸oÄܹ»±»SmartyËuʶ±ðµÄ¶þάÊý×e,

¼Çס:Ëu*µ»ØµÄÊÇÒ»¸o¶þάÊý×e¶ø²»ÊÇÒ»¸oRecordSet,ËuÔÚÄa¿ÉÒÔ³ÌÐoÖÐÖ±½ÓÔÚ3´¦Ê¹ÓÃ.
2. ÕaÀiÊÇΪÁ˸øÐÂÎűeÌa×oÁ´½ÓʱҪGET²ÎÊýtype=XX¶ø×oµÄ

ºo¼Ç:
´o¼ÒÔÚʹÓÃADODBʱÓм¸¸oµØ½Òª×¢Òa:
1. ³oʼ»¯: ³oʼ»¯µÄ
½Ê½²»ÊÇʹÓÃnew,ÒoΪËu²»ÊÇÒ»¸o¶ÔÏo
2. ½ ¨: »u±¾ÉÏÿ¸o½¨¶¼ÊÇÒÔ´oд×Öĸ¿ªÍ´oСд»iºÏµÄÃu³Æ,ÕaÒ»µaºÃÏñÓeNIXµÄÏ°¹ßÓÐÐ(C)²»Í¬,Ò²²»Í¬ÓÚPHPµÄÕuÌa*ç¸ñ,ËuÒÔ

×¢ÒaÕaÀiµÄ´oСдÎÊÌa.

ºÃÁË,Õa¸oSmartyµÄϵÁн̵̳½ÕaÀi¾Í»u±¾ÒѾ­Íe³ÉÁË,ÎÒµÄÕa¼¸Æª³o¼¶½Ì³Ì¾ÍËaÊÇÅ××(C)ÒýÓñ°É,Ï£Íu¸u¶aµÄ¸ßÊÖ½«¸u¶aµÄ¾­Ñeд³oÀ´,´o¼Ò

¹²Í¬Ìa¸ß!ÒoΪ¹«Ë¾²»ÔÊÐi¿ªQQ,Èç¹u´o¼ÒÓÐÏeÒªÓeÎÒ½øÐн»Á÷µÄ»°,Çe´o¼Ò¼ÓÎÒµÄMSN:teacherli@ceua.org,»¶Ó­´o¼ÒÒ»ÆðÌÖÂÛ!

£¨³o´¦£ºViphot£(C)

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例