Home Backend Development PHP Tutorial Use PHP and Bootstrap to build an efficient management backend to achieve data visualization

Use PHP and Bootstrap to build an efficient management backend to achieve data visualization

Jun 27, 2023 am 11:37 AM
php bootstrap data visualization

With the continuous development of the Internet, various websites and applications continue to emerge, and the management backend of these websites and applications is an indispensable part. The management backend is not only a data management platform for websites and applications, but also a platform for data visualization, allowing managers to see data changes and trends more clearly, thereby providing stronger support for corporate decision-making.

Here, this article will introduce how to use PHP and Bootstrap to build an efficient management backend to realize the function of data visualization. The following will be divided into the following parts to explain.

1. Environment setup

Before starting, you need to set up the PHP and MySQL environments first. You can choose an integrated environment, such as XAMPP, WAMP, etc., or you can build it yourself. After installation, you can test whether the environment is successfully established by accessing http://localhost.

2. Use Bootstrap to build the management backend interface

Bootstrap is a popular front-end framework. It provides some very convenient styles and components that can quickly build a beautiful and responsive website. Before using Bootstrap, you need to download its files.

After completion, the management backend interface can be built according to actual needs. The specific steps are as follows:

1. Create a new page and introduce the Bootstrap style file and JavaScript file, as shown in the following code:

1

2

3

4

5

6

7

8

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>管理后台</title>

    <link rel="stylesheet" href="css/bootstrap.min.css">

    <script src="js/jquery.min.js"></script>

    <script src="js/bootstrap.min.js"></script>

</head>

Copy after login

2. Add a navigation bar, as shown in the following code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

<body>

    <nav class="navbar navbar-expand-lg navbar-light bg-light">

        <a class="navbar-brand" href="#">管理后台</a>

        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">

            <span class="navbar-toggler-icon"></span>

        </button>

        <div class="collapse navbar-collapse" id="navbarNav">

            <ul class="navbar-nav">

                <li class="nav-item active">

                    <a class="nav-link" href="#">首页 <span class="sr-only">(current)</span></a>

                </li>

                <li class="nav-item">

                    <a class="nav-link" href="#">数据可视化</a>

                </li>

                <li class="nav-item">

                    <a class="nav-link" href="#">用户管理</a>

                </li>

                <li class="nav-item">

                    <a class="nav-link" href="#">设置</a>

                </li>

            </ul>

        </div>

    </nav>

</body>

Copy after login

3. Add the main content area, as shown in the following code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

<div class="container-fluid">

    <div class="row">

        <div class="col-md-3">

            <div class="card">

                <div class="card-body">

                    <h5 class="card-title">数据可视化</h5>

                    <p class="card-text">这里是数据可视化的介绍。</p>

                    <a href="#" class="btn btn-primary">进入</a>

                </div>

            </div>

        </div>

        <div class="col-md-3">

            <div class="card">

                <div class="card-body">

                    <h5 class="card-title">用户管理</h5>

                    <p class="card-text">这里是用户管理的介绍。</p>

                    <a href="#" class="btn btn-primary">进入</a>

                </div>

            </div>

        </div>

        <div class="col-md-3">

            <div class="card">

                <div class="card-body">

                    <h5 class="card-title">设置</h5>

                    <p class="card-text">这里是设置的介绍。</p>

                    <a href="#" class="btn btn-primary">进入</a>

                </div>

            </div>

        </div>

    </div>

</div>

Copy after login

Through the above code, a simple management background interface can be quickly built. Next, you can operate the data in PHP and display the data visually on the page.

3. Integrate PHP and MySQL into the management background

1. Create a database and create a table named "data" with the following table structure:

1

2

3

4

5

6

7

CREATE TABLE `data` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `name` varchar(255) NOT NULL,

  `value` int(11) NOT NULL,

  `date` datetime NOT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Copy after login

2. Connect to the database in PHP, query the data, and return the data to the front-end page in JSON format to realize the data visualization function. The code is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<?php

$servername = "localhost";

$username = "root";

$password = "";

$dbname = "test";

 

$conn = new mysqli($servername, $username, $password, $dbname);

 

if ($conn->connect_error) {

    die("连接失败: " . $conn->connect_error);

}

 

$sql = "SELECT * FROM data";

$result = $conn->query($sql);

 

$rows = array();

while($row = $result->fetch_assoc()) {

    array_push($rows, array('name'=>$row['name'], 'value'=>intval($row['value']), 'date'=>$row['date']));

}

 

echo json_encode($rows);

$conn->close();

?>

Copy after login

4. Use JavaScript for data visualization

In the management background, you can use Chart.js, a very popular JavaScript library, for data visualization. Chart.js provides some commonly used data display methods, such as line charts, column charts, pie charts, etc. In this article, we will use line charts to implement data visualization capabilities.

The code is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

<body>

    <canvas id="myChart"></canvas>

    <script src="js/Chart.min.js"></script>

    <script src="js/data.php"></script>

    <script>

        $(function() {

            $.get('data.php', function(data) {

                var labels = [];

                var values = [];

                var dates = [];

                $.each(JSON.parse(data), function(index, item) {

                    labels.push(item.name);

                    values.push(item.value);

                    dates.push(item.date);

                });

                var ctx = document.getElementById('myChart');

                var myChart = new Chart(ctx, {

                    type: 'line',

                    data: {

                        labels: labels,

                        datasets: [{

                            label: '数据可视化',

                            data: values,

                            backgroundColor: [

                                'rgba(255, 99, 132, 0.2)',

                                'rgba(54, 162, 235, 0.2)',

                                'rgba(255, 206, 86, 0.2)',

                                'rgba(75, 192, 192, 0.2)',

                                'rgba(153, 102, 255, 0.2)',

                                'rgba(255, 159, 64, 0.2)'

                            ],

                            borderColor: [

                                'rgba(255, 99, 132, 1)',

                                'rgba(54, 162, 235, 1)',

                                'rgba(255, 206, 86, 1)',

                                'rgba(75, 192, 192, 1)',

                                'rgba(153, 102, 255, 1)',

                                'rgba(255, 159, 64, 1)'

                            ],

                            borderWidth: 1

                        }]

                    },

                    options: {

                        scales: {

                            yAxes: [{

                                ticks: {

                                    beginAtZero: true

                                }

                            }]

                        }

                    }

                });

            });

        });

    </script>

</body>

Copy after login

Through the above code, the data in the database can be displayed on the page in the form of a line chart.

Summary

This article introduces how to use PHP and Bootstrap to build an efficient management backend to realize the data visualization function. First, we use Bootstrap to build the management backend interface; then, we use PHP to connect to the database and return the data to the front-end page in JSON format; finally, we use the JavaScript library Chart.js to display the data on the page in the form of a line chart. The method in this article is applicable to most types of management backends. It not only allows administrators to see data changes and trends more clearly, but also improves work efficiency.

The above is the detailed content of Use PHP and Bootstrap to build an efficient management backend to achieve data visualization. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

How to get the bootstrap search bar How to get the bootstrap search bar Apr 07, 2025 pm 03:33 PM

How to use Bootstrap to get the value of the search bar: Determines the ID or name of the search bar. Use JavaScript to get DOM elements. Gets the value of the element. Perform the required actions.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to do vertical centering of bootstrap How to do vertical centering of bootstrap Apr 07, 2025 pm 03:21 PM

Use Bootstrap to implement vertical centering: flexbox method: Use the d-flex, justify-content-center, and align-items-center classes to place elements in the flexbox container. align-items-center class method: For browsers that do not support flexbox, use the align-items-center class, provided that the parent element has a defined height.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

See all articles