Home  >  Article  >  Backend Development  >  Comparison of Several Methods of Displaying Data_PHP Tutorial

Comparison of Several Methods of Displaying Data_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 16:02:031122browse

When used to display database data, a loop body is generally used. Commonly used methods include while() and for() statements. Let’s talk about their usage in different situations.
Let’s introduce them separately:
The while() statement can display all the data, which is especially convenient when the number of loops is not known. The for() statement can output the display starting from the specified position to the specified The data at the end of the position is useful when outputting and displaying data within a certain range. Let’s take a look at a programming example:
We first create a database for backup: database name: mydb, table name: tbl.
Use the following statement: create table tal (idx int(3),url char (100), freetext char(100))
You can use the phpmyadmin tool to insert several data into the database table.
Start programming:

$id=mysql_connect("localhost") or die("Unable to establish database link");#Link database
$result=mysql_db_query("mydb","select * from tbl",$id);#Query results and store them in variables
$rows=mysql_num_rows($result);#Get the total number of rows in the data table, which is the total number of data
echo"

";#Prepare to output in the form of a table
echo "
";#End of table

Insert output statements in the above two sentences, corresponding to different situations, output Statements are divided into several situations:
If all data is output, use for() first

for($i=0;$i<$rows;$i++){
$total =mysql_fetch_array($result);
echo "$total[freetext]< /a>$total[idx]";
    }
Use while() to do it
while($total=mysql_fetch_array($result))
{ echo "
$total[freetext]$total[idx]< ;/tr>";
}

When we want to display it in pages, that is, we cannot display all the data at once, then we can use for( ) to complete this task.
We assume that every time 10 data are output, $page is used to represent the current page number. $pagesize=10 is used to represent the number of data on page. The statement is as follows:

for ($i=0; $i<$pagesize;$i++)
{
$start=($page-1)*$pagesize+$i;#Count the starting number of data rows
if ($start<$rows)
$idx=mysql_result($result,$start,"idx");
$url=mysql_result($result,$start,"url");
$freetext=mysql_result($result,$ start,"freetext");
echo "$freetext$idx";

The above statements use for() to obtain the values ​​of each field in the data table and store them in variables, and use the echo statement to display them.

The above program runs in apache+mysql+php4 and passes

[The copyright of this article is jointly owned by the author and Aosuo.com. If you need to reprint, please indicate the author and source]

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/316724.htmlTechArticleWhen used to display database data, it is usually done with a loop body. Commonly used methods include while() and for () There are two kinds of statements. Let’s talk about their respective usages in different situations. We...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn