Access the database using PHP -- Implement the paging function and multi-condition query function, and access data Paging
1. Implement Paging
<Body>
<Table width = "100%" border = "1">
<Thead>
<Tr>
<Th> Code </th>
<Th> name </th>
<Th> price </th>
</Tr>
</Thead>
<Tbody>
<? Php
Require_once "./DBDA. class. php"; // load the encapsulation file of the database metadata class
Require_once "page. class. php"; // load the encapsulated file of the paging class
$ Db = new DBDA ();
$ SQL = "select count (*) from car ";
$ Arr = $ db-> query ($ szts );
$ Page = new Page ($ arr [0] [0], 5); // view the total data and the row data displayed on each page
$ SQL = "select * from car". $ page-> limit; // splice the data displayed on each page
$ Arr = $ db-> query ($ SQL );
Foreach ($ arr as $ v ){
Echo "<tr>
<Td >{$ v [0]} </td>
<Td >{$ v [1]} </td>
<Td >{$ v [2]} </td>
</Tr> ";
}
?>
</Tbody>
</Table>
<Div>2. Multi-condition Query
<Body>
<? Php
Require_once "./DBDA. class. php ";
Require_once "page. class. php ";
$ Db = new DBDA ();
// Create a constant Standing Condition
$ Tj = "1 = 1 ";
$ Tj2 = "1 = 1 ";
$ Name = "";
$ Brand = "";
// When the submitted data is not empty, you need to modify the conditions.
If (! Empty ($ _ GET ["name"]) {
$ Name = $ _ GET ["name"];
$ Tj = "name like '% {$ name} %'"; // use fuzzy search to query key information
}
If (! Empty ($ _ GET ["brand"]) {
$ Brand = $ _ GET ["brand"];
$ Tj2 = "brand = '{$ brand }'";
}
?>
<Table width = "100%" border = "1">
<Form action = "test. php" method = "get">
<Input type = "text" name = "name" placeholder = "Enter the name" value = "<? Php echo $ name?> "Style =" max-width: 200px; float: left ">
<Input type = "text" name = "brand" placeholder = "Please input series" value = "<? Php echo $ brand?> "Style =" max-width: 200px; float: left ">
<Button type = "submit" style = "float: left; margin-left: 10px"> query </button>
</Form>
<Thead>
<Tr>
<Th> Code </th>
<Th> name </th>
<Th> series </th>
<Th> time </th>
<Th> price </th>
</Tr>
</Thead>
<Tbody>
<? Php
$ Zts = "select count (*) from car where {$ tj} and {$ tj2}"; // The total number of data records queried by multiple conditions
$ Ats = $ db-> query ($ arr );
$ Page = new Page ($ ats [0] [0], 2 );
$ SQL = "select * from car where {$ tj} and {$ tj2}". $ page-> limit;
$ Arr = $ db-> query ($ SQL );
Foreach ($ arr as $ v ){
// Add the font color to the query keyword
$ N = str_replace ($ name, "<span style = 'color: red' >{$ name} </span>", $ v [1]);
Echo "<tr>
<Td >{$ v [0]} </td>
<Td >{$ n} </td>
<Td >{$ v [2]} </td>
<Td >{$ v [3]} </td>
<Td >{$ v [7]} </td>
</Tr> ";
}
?>
</Tbody>
</Table>
<Div>
<? Php
Echo $ page-> fpage ();
?>
</Div>
</Body>