How to output all the data of a field in the database in table form in PHP
To help you understand how to connect to the MySQL database, the following is a sample code: $conn = mysql_connect(.....);.
//Write the database connection yourself
echo '
';
$i = 0;
Let’s take a look at the specific puzzle-solving methods. To help players who haven't passed the level yet, you can use the following code to query the fields in the database:
```php
$Sql = mysql_query('SELECT field name FROM table name');
```
This code will query the field names from the specified table. Hope this solution helps you!
In order to help players who have not passed the level yet, let us learn about the specific puzzle solving methods. In the code, we use a loop statement "while($Rs = mysql_fetch_array($Sql))", which is used to traverse and obtain each row of data in the query result set. This statement will be executed continuously until all data has been processed. Inside the loop, we can perform corresponding operations on each row of data, such as output, calculation, or storage. By understanding and
$i ;
echo '
' . $Rs['field name']. ' | ';
if ( $i % 3 == 0 ) {
echo '
';
}
}
echo '
';
php output form
This is a table loop problem
The principle is a large table with three columns in it, and each column contains a table. This table is displayed in three rows
think:
Wrap every 3 columns to achieve 3 columns in one row and 3 columns in multiple rows
//Loop from this place, use
and
to branch
$i = 0;
while($myrow=mysql_fetch_array($rs)){
?>
//Internal table loop starts
1 |
1(1) |
1(2) |
//End of inner loop
|
}
//Since we encounter 3 columns and a new line, then /3
if ($i / 3) echo "";
//The above sentence is the most important. If it can be /3, then will be displayed. Then, as soon as is displayed, it will be a new line.
?>
You can try
The principle of using FOR statement to output tables in PHP!
Your code above is a bit awkward to write, so it seems difficult to understand. I added some comments. You can see if you can understand it. Pay attention to the two IFs and { } so that the scope is obvious
echo "
";
for($i=0;$i<=11;$i ) //This is changed to start from 0 and end at 11. A total of 12 cells will be output
{
if($i%3==0){ //This is actually to determine whether it is the head of the line, because the head of the line needs to output
divisible by 3 is the head of the line. The head of the line is the 0th, 3rd, 6th and 9th cells respectively. Description of each There are 3 cells in the row and 4 rows in total
echo "
";
}
echo "
cell | "; //No matter what conditions come in, a cell will be output here
if($i%3==2){ //To determine whether the end of the line is 2 5 8 11
echo "
";
}
}
echo "
";
?>
Please teach PHP how to loop the output form
In this case, try to use floating divs. Using table is more troublesome, the code is as follows:
PHP code
$sql = "xxx limit 40";
$query = mysql_db_query($sql);
if(mysql_num_rows($query) > 0){
?>
$ct = 0;
$cols = 8;
while($rs = mysql_fetch_array($query)){
//Output the beginning of the line
if( $ct % $cols == 0 ){
echo '
';
}
?>
Record |
$ct;
//Output line tail
if( $ct % $cols == 0 ){
echo '
';
}
}
//There are not enough cells, complete the cells and the end of the line
if( $ct % $cols != 0 ){
$lack = ($cols - $ct % $cols);
for($i = 0; $i
echo '
| ';
}
echo '';
}
?>
}
?>
The above is the detailed content of How to output all data of a field in a database in tabular form in PHP. For more information, please follow other related articles on the PHP Chinese website!