ado| Connection Database Similarly, I added a number to the key points, below to illustrate their meaning:
1. To establish a Connection object $conn, what you should note here is that its initial appearance is not in the form of $conn = new Adonewconnection ($dbType), that is to say
, Adonewconnection is not a class, and you can't initialize it with new. Look at its source code you'll see, it's just a function.
2. What about this? Open a news database, the host is: localhost, user name is root, password is ""
3. A query statement, note that here to the query's fields using the AS keyword to be identified, the name of the template you set in the template variable name.
4. Execute this query using execute, and the result returns a recordset dataset
5. Here's a way: $rs->getarray ($num), which is described above, is to return $num rows from the $rs dataset, resulting in a two-dimensional number that can be smarty recognized
So ADODB automatically built this structure for us, and in our previous example, we used a loop to build such an array.
6. I don't have to say that, can I?
7. Close the related resources in memory.
You can see, the entire program has not appeared any while statement, the overall structure of the program is very clear, which is why Adodb+smarty is a gold combination of reasons. But then
Said back, simple has the simple question, does not know everybody thought that did not have, here to the display news headline length does not have the control, namely, if the length of a news headline exceeds one line to display the fan
Wai, it is automatically folded line to the next line, then the entire layout will be confused, said that we have their own situation to decide whether to use it, of course, you can also use like the previous section of the intermediary
In the same way, using a circular statement to refactor the two-dimensional array, so that it fits your purpose, how to do everyone to think about it, refer to the practice in phplib, I introduced the last section ...
Take a look at the news page again.
=============================================================
news.php
=============================================================
<?php
/*********************************************
*
* FileName: news.php
* Role: News display program
*
* Author: Big bro
* email:teacherli@163.com
*
*********************************************/
Include_once ("./comm/mysmarty.class.php"); file://extension class file containing Smarty
Include_once ("./comm/adodb/adodb.inc.php"); file://contains ADODB master execution files
$smarty = new Mysmarty (); file://to establish Smarty instance objects $smarty
$conn = adonewconnection ("MySQL"); file://Initialization ADODB
$conn->connect ("localhost", "root", "" "," News "); file://Connection Database
$NewsID = $_get["id"]; FILE://Get the news number
$NewsType = $_get["type"]; file://the type of news to display
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); file://returns a one-dimensional array, labeled as a template variable name
$smarty->display ($row);
Unset ($row);
$conn->close ();
?>
=============================================================
Explain the key place, in fact, there is only one place in the news.php to explain the value.
1. $conn->getrow ($strQuery): This sentence returns a one-dimensional array, which is returned in the form of:
$array = ("Newstitle" => "xxxx", "Newscontent" => "yyyyy ...")
Understand what smarty do if you use $smarty ($array)? By the way, that's the equivalent:
$smarty->assign ("Newstitle", "xxxx");
$smarty->assign ("Newscontent", "yyyyy ...");
It's simple, really.
Now let's look at the News list:
================================================================
newslist.php
================================================================
<?php
/*********************************************
*
* FileName: newslist.php
* Role: News list Display program
*
* Author: Big bro
* email:teacherli@163.com
*
*********************************************/
Include_once ("./comm/mysmarty.class.php"); file://extension class file containing Smarty
Include_once ("./comm/adodb/adodb.inc.php"); file://contains ADODB master execution files
$smarty = new Mysmarty (); file://to establish Smarty instance objects $smarty
$conn = adonewconnection ("MySQL"); file://Initialization ADODB
$conn->connect ("localhost", "root", "" "," News "); file://Connection Database
>FILE://Get the news number
$NewsType = $_get["type"]; file://the type of news to display
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); file://This sentence for the link service in the News list
3. $smarty->assign ("Newslist", $rs);
Unset ($RS);
$conn->close ();
$smarty->display ("Newslist.tpl");
?>
================================================================
To explain the difference:
1. GetAll ($strQuery): This function is a good thing, its role is to $strquery query to all the data into a smarty can be recognized by a two-dimensional array,
Remember: It returns a two-dimensional array instead of a recordset, where you can use the program directly at 3.
2. This is done to get the parameter type=xx to link the news headlines.
Postscript:
There are several places to note when using ADODB:
1. Initialization: Initialization is not the way to use new, because it is not an object
2. Method: Basically each method is a combination of uppercase and lowercase names, this seems to be somewhat different from the *nix habits, but also different from the overall style of PHP, so
Note the case problem here.
Well, this smarty series of tutorials to the basic has been completed, I have a few of these introductory course, I hope more experts will be more experience to write out, we improve together!