Access the database using PHP -- Implement the paging function and multi-condition query function, and access data Paging

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.