This Tutorial will teach you how to make the Sales Chart using php and mysql. In order to create the project i have used editor as PHPStrom.
First Step Establish the Database Connection
Create the Page db.php.
1 2 3 4 5 6 7 8 9 10 11 | <?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "salesdb"; $conn = mysqli_connect($servername,$username,$password,$dbname); ?> |
After that design the index page.
Index.php
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | <?php include("db.php"); $query = "select jan,feb,march,april,may,june,july from saleschart"; $result = mysqli_query($conn,$query); if(mysqli_num_rows($result) >= 1) { while ($row = mysqli_fetch_assoc($result)) { $jan = $row['jan']; $feb = $row['feb']; $march = $row['march']; $april = $row['april']; $may = $row['may']; $june = $row['june']; $july = $row['july']; } } else { echo "somting went wrong"; } ?> <html> <head> <link href="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.css" rel="stylesheet"> <link href="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.css" rel="stylesheet"> </head> <body> <canvas id="myChart" style="height: auto; width: 500px;"></canvas> <?php echo "<input type='hidden' id= 'jan' value = '$jan' >"; echo "<input type='hidden' id= 'feb' value = '$feb' >"; echo "<input type='hidden' id= 'march' value = '$march' >"; echo "<input type='hidden' id= 'april' value = '$april' >"; echo "<input type='hidden' id= 'may' value = '$may' >"; echo "<input type='hidden' id= 'june' value = '$june' >"; echo "<input type='hidden' id= 'july' value = '$july' >"; ?> <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.bundle.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.bundle.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js"></script> <script> var jan = document.getElementById("jan").value; var feb = document.getElementById("feb").value; var march = document.getElementById("march").value; var april = document.getElementById("april").value; var may = document.getElementById("may").value; var june = document.getElementById("june").value; var july = document.getElementById("july").value; window.onload = function() { var randomScalingFactor = function() { return Math.round(Math.random() * 100); }; var config = { type: 'bar', data: { borderColor : "#fffff", datasets: [{ data: [ jan, feb, march, april, may, june, july ], borderColor : "#fff", borderWidth : "3", hoverBorderColor : "#000", label: 'Monthly Sales Report', backgroundColor: [ "#0190ff", "#56d798", "#ff8397", "#6970d5", "#f312cb", "#ff0060", "#ffe400" ], hoverBackgroundColor: [ "#f38b4a", "#56d798", "#ff8397", "#6970d5", "#ffe400" ] }], labels: [ 'Jan', 'Feb', 'March', 'April', 'May', 'June', 'July' ] }, options: { responsive: true } }; var ctx = document.getElementById('myChart').getContext('2d'); window.myPie = new Chart(ctx, config); }; </script> </body> </html> |
i have attached the video link below. which will do this tutorials step by step.