0% found this document useful (0 votes)
85 views22 pages

Bars

The code samples show how to retrieve visitor data from a database and use it to populate Morris.js bar and line charts displayed on a web page. An AJAX call is made to fetch the data in JSON format from a server-side endpoint. The data is then passed to the Morris.js charts to display metrics like total visitors over time.

Uploaded by

Asna Mohamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views22 pages

Bars

The code samples show how to retrieve visitor data from a database and use it to populate Morris.js bar and line charts displayed on a web page. An AJAX call is made to fetch the data in JSON format from a server-side endpoint. The data is then passed to the Morris.js charts to display metrics like total visitors over time.

Uploaded by

Asna Mohamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

To retrieve data from the visitor table and display it in the bar chart, you need to modify the

data
array in the Morris.Bar function to contain the actual data from the database. Here's an example of
how to do that:

php

Copy code

$(document).ready(function() {

// Retrieve data from database

$.ajax({

url: 'get_visitor_data.php',

type: 'GET',

dataType: 'json',

success: function(data) {

// Bar Chart

Morris.Bar({

element: 'bar-charts',

data: data,

xkey: 'year',

ykeys: ['total_income', 'total_outcome'],

labels: ['Total Income', 'Total Outcome'],

lineColors: ['#ff9b44','#fc6075'],

lineWidth: '3px',

barColors: ['#ff9b44','#fc6075'],

resize: true,

redraw: true

});

},

error: function(xhr, status, error) {

console.log(error);

});
});

In this example, we're using an AJAX call to retrieve the data from a PHP script called
get_visitor_data.php, which would contain the SQL query to retrieve the necessary data from the
database. The data returned from the PHP script is in JSON format, which is then passed to the data
array in the Morris.Bar function.

The data returned from the PHP script should be in the following format:

yaml

Copy code

{ year: '2006', total_income: 100, total_outcome: 90 },

{ year: '2007', total_income: 75, total_outcome: 65 },

{ year: '2008', total_income: 50, total_outcome: 40 },

{ year: '2009', total_income: 75, total_outcome: 65 },

{ year: '2010', total_income: 50, total_outcome: 40 },

{ year: '2011', total_income: 75, total_outcome: 65 },

{ year: '2012', total_income: 100, total_outcome: 90 }

The xkey property should correspond to the year field in the data, and the ykeys property should
correspond to the total_income and total_outcome fields.

[email protected]

use my above details to change details just use visitor system so the charts will check the total user
as per day,per week,per month, and per year..

Sure, here's an example code that retrieves data from the visitor table to display the total number of
visitors per day, per week, per month, and per year. The code uses the Chart.js library for creating
charts.
php

Copy code

<!-- Include Chart.js library -->

<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

<!-- Create a canvas element for the bar chart -->

<canvas id="visitor-chart" width="400" height="400"></canvas>

<script>

// Retrieve visitor data from database

$.ajax({

url: 'retrieve_visitor_data.php',

dataType: 'json',

success: function(data) {

// Get total visitors per day

var visitorsPerDay = [];

for (var i = 0; i < data.length; i++) {

visitorsPerDay.push(data[i].visitors);

// Get total visitors per week

var visitorsPerWeek = [];

var weekStart = moment().startOf('week');

for (var i = 0; i < 7; i++) {

var visitors = 0;

var weekDay = weekStart.clone().add(i, 'days').format('YYYY-MM-DD');

for (var j = 0; j < data.length; j++) {

if (moment(data[j].date).format('YYYY-MM-DD') == weekDay) {

visitors = data[j].visitors;

break;

}
}

visitorsPerWeek.push(visitors);

// Get total visitors per month

var visitorsPerMonth = [];

var monthStart = moment().startOf('month');

var daysInMonth = moment().daysInMonth();

for (var i = 0; i < daysInMonth; i++) {

var visitors = 0;

var monthDay = monthStart.clone().add(i, 'days').format('YYYY-MM-DD');

for (var j = 0; j < data.length; j++) {

if (moment(data[j].date).format('YYYY-MM-DD') == monthDay) {

visitors = data[j].visitors;

break;

visitorsPerMonth.push(visitors);

// Get total visitors per year

var visitorsPerYear = [];

var yearStart = moment().startOf('year');

var daysInYear = moment().diff(yearStart, 'days');

for (var i = 0; i < daysInYear; i++) {

var visitors = 0;

var yearDay = yearStart.clone().add(i, 'days').format('YYYY-MM-DD');

for (var j = 0; j < data.length; j++) {

if (moment(data[j].date).format('YYYY-MM-DD') == yearDay) {

visitors = data[j].visitors;

break;
}

visitorsPerYear.push(visitors);

// Create the bar chart

var visitorChart = new Chart($('#visitor-chart'), {

type: 'bar',

data: {

labels: ['Day', 'Week', 'Month', 'Year'],

datasets: [{

label: 'Total Visitors',

data: [visitorsPerDay, visitorsPerWeek, visitorsPerMonth, visitorsPerYear],

backgroundColor: ['#ff9b44', '#fc6075', '#4d7e8a', '#ffbcb7'],

borderWidth: 1

}]

},

options: {

scales: {

yAxes: [{

ticks: {

beginAtZero: true

}]

});

});

</script>

Note: The above code


[email protected]

use this details to enter above code in this code below

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

<div class="row">

<div class="col-md-6 text-center">

<div class="card">

<div class="card-
body">

<h3
class="card-title">Total Visitors</h3>

<div
id="bar-charts"></div>

</div>

</div>

</div>

Consider the above code and the below javascript i need those bar to retrieve data from database
consider visitor table

$(document).ready(function() {

// Bar Chart

Morris.Bar({

element: 'bar-charts',

data: [

{ y: '2006', a: 100, b: 90 },

{ y: '2007', a: 75, b: 65 },

{ y: '2008', a: 50, b: 40 },

{ y: '2009', a: 75, b: 65 },
{ y: '2010', a: 50, b: 40 },

{ y: '2011', a: 75, b: 65 },

{ y: '2012', a: 100, b: 90 }

],

xkey: 'y',

ykeys: ['a', 'b'],

labels: ['Total Visitor', 'Total Outcome'],

lineColors: ['#ff9b44','#fc6075'],

lineWidth: '3px',

barColors: ['#ff9b44','#fc6075'],

resize: true,

redraw: true

});

// Line Chart

Morris.Line({

element: 'line-charts',

data: [

{ y: '2006', a: 50, b: 90 },

{ y: '2007', a: 75, b: 65 },

{ y: '2008', a: 50, b: 40 },

{ y: '2009', a: 75, b: 65 },

{ y: '2010', a: 50, b: 40 },

{ y: '2011', a: 75, b: 65 },

{ y: '2012', a: 100, b: 50 }

],

xkey: 'y',

ykeys: ['a', 'b'],

labels: ['Total Visitor', 'Total visitors'],

lineColors: ['#ff9b44','#fc6075'],
lineWidth: '3px',

resize: true,

redraw: true

});

});

To retrieve data from the database and display it in the Morris Bar Chart, you need to use AJAX to
fetch the data from the server. Here's an updated version of the JavaScript code with AJAX
integration:

php

Copy code

$(document).ready(function() {

// Bar Chart

$.ajax({

url: '/get-visitors-data',

dataType: 'json',

success: function(data) {

Morris.Bar({

element: 'bar-charts',

data: data,

xkey: 'y',

ykeys: ['a', 'b'],

labels: ['Total Visitor', 'Total Outcome'],

lineColors: ['#ff9b44','#fc6075'],

lineWidth: '3px',

barColors: ['#ff9b44','#fc6075'],

resize: true,

redraw: true

});

}
});

// Line Chart

$.ajax({

url: '/get-visitors-data',

dataType: 'json',

success: function(data) {

Morris.Line({

element: 'line-charts',

data: data,

xkey: 'y',

ykeys: ['a', 'b'],

labels: ['Total Visitor', 'Total visitors'],

lineColors: ['#ff9b44','#fc6075'],

lineWidth: '3px',

resize: true,

redraw: true

});

});

});

In this code, we are making an AJAX call to the /get-visitors-data URL to fetch the visitor data from
the server. The server should return the data in JSON format, which will be used to populate the
Morris Bar and Line charts.

You need to implement a server-side endpoint that returns the visitor data in the expected format.
The visitor data should include the total number of visitors and outcomes for each day, week,
month, and year. You can use the following example data format:

yaml

Copy code

[
{ y: '2022-03-10', a: 100, b: 90 },

{ y: '2022-03-09', a: 75, b: 65 },

{ y: '2022-03-08', a: 50, b: 40 },

{ y: '2022-03-07', a: 75, b: 65 },

{ y: '2022-03-06', a: 50, b: 40 },

{ y: '2022-03-05', a: 75, b: 65 },

{ y: '2022-03-04', a: 100, b: 90 }

The y property represents the date, while a and b represent the total number of visitors and
outcomes for that date, respectively. The date format can be changed as per your requirement.

[email protected]

where is php code.?

I apologize for the confusion. The code you provided earlier was only HTML and JavaScript. To
retrieve data from a database, you would need to use PHP to make a server-side request to the
database and retrieve the data.

Here's an example PHP code snippet that you could use to retrieve data from a MySQL database:

php

Copy code

<?php

// Connect to the database

$servername = "localhost";

$username = "username";

$password = "password";

$dbname = "myDB";

$conn = mysqli_connect($servername, $username, $password, $dbname);


// Check connection

if (!$conn) {

die("Connection failed: " . mysqli_connect_error());

// Perform query

$sql = "SELECT date, total_visitors, total_outcome FROM visitor";

$result = mysqli_query($conn, $sql);

// Fetch result as an associative array

$data = array();

while ($row = mysqli_fetch_assoc($result)) {

$data[] = $row;

// Close connection

mysqli_close($conn);

// Return data as JSON

echo json_encode($data);

?>

You would need to replace the $servername, $username, $password, and $dbname variables with
your own database credentials. You would also need to modify the $sql query to match your
database schema.

You could then use the $.ajax() function in JavaScript to make a request to this PHP script and
retrieve the data as a JSON object.
[email protected]

nataka wewe unitengenezee mfano wa javascript code na php ambazo zitakuwa zina vuta data
kutoka kwenye database na kuzionyesha kwa bar charts ambazo nimekupa nataka bars zionyeshe
jumla ya visitor kwa mwaka,mwezi,wiki tumia taarifa za boostrap na javascript ambazo nilikuwa
nimekupatia kabla..

translate that in english

"I want you to create an example of JavaScript code and PHP that will retrieve data from the
database and display it in bar charts using the charts that I provided. I want the bars to show the
total number of visitors per year, month, and week. Please use the Bootstrap and JavaScript
information that I provided before."

[email protected]

I want you to create an example of JavaScript code and PHP that will retrieve data from the database
and display it in bar charts using the charts that I provided. I want the bars to show the total number
of visitors per year, month, and week. Please use the Bootstrap and JavaScript information that I
provided before.

Sure, here's an example of JavaScript code and PHP that will retrieve data from a MySQL database
and display it in bar charts using the provided charts, showing the total number of visitors per year,
month, and week:

index.php:

html

Copy code

<!DOCTYPE html>

<html>

<head>

<title>Visitor Charts</title>

<!-- Include required stylesheets and scripts -->

<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/raphael/2.3.0/raphael.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.min.js"></script>

</head>

<body>

<div class="container">

<div class="row">

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

<div class="row">

<div class="col-md-6 text-center">

<div class="card">

<div class="card-body">

<h3 class="card-title">Total
Visitors</h3>

<div id="bar-charts"></div>

</div>

</div>

</div>

</div>

</div>

</div>

</div>

<!-- Include custom JavaScript code -->

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

</body>

</html>

script.js:

javascript

Copy code
$(document).ready(function() {

// Retrieve data from server-side script using AJAX

$.ajax({

url: 'data.php',

dataType: 'json',

success: function(data) {

// Bar Chart

Morris.Bar({

element: 'bar-charts',

data: data.bar_data,

xkey: 'date',

ykeys: ['visitors'],

labels: ['Total Visitors'],

barColors: ['#ff9b44'],

resize: true,

redraw: true

});

// Line Chart

Morris.Line({

element: 'line-charts',

data: data.line_data,

xkey: 'date',

ykeys: ['visitors'],

labels: ['Total Visitors'],

lineColors: ['#fc6075'],

lineWidth: '3px',

resize: true,

redraw: true

});

}
});

});

data.php:

php

Copy code

<?php

// Connect to MySQL database

$servername = "localhost";

$username = "root";

$password = "";

$dbname = "testdb";

$conn = mysqli_connect($servername, $username, $password, $dbname);

if (!$conn) {

die("Connection failed: " . mysqli_connect_error());

// Retrieve data for bar chart

$bar_data = array();

$sql = "SELECT DATE_FORMAT(date, '%Y-%m-%d') as date, COUNT(*) as visitors FROM visitor GROUP
BY DATE_FORMAT(date, '%Y-%m-%d')";

$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {

while($row = mysqli_fetch_assoc($result)) {

$bar_data[] = $row;

// Retrieve data for line chart

$line_data = array();

$sql = "SELECT DATE_FORMAT(date, '%Y-%m') as date, COUNT(*) as visitors FROM visitor GROUP BY
[email protected]

your so mad you didn't finish the code you make me to feel anger

I apologize if my previous response did not fully address your request. Here is an example of a PHP
and JavaScript code that retrieves data from a MySQL database and displays it in bar charts using the
Morris.js library:

PHP Code:

php

Copy code

<?php

// Connect to MySQL database

$host = 'localhost';

$username = 'your_username';

$password = 'your_password';

$database = 'your_database';

$conn = new mysqli($host, $username, $password, $database);

// Retrieve data from database

$year_data = array();

$month_data = array();

$week_data = array();

// Retrieve data by year

$sql = "SELECT YEAR(date) AS year, COUNT(*) AS count FROM visitors GROUP BY YEAR(date) ORDER
BY YEAR(date) ASC";

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

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

$year_data[] = array('y' => $row['year'], 'a' => $row['count']);


}

// Retrieve data by month

$sql = "SELECT CONCAT(YEAR(date), '-', MONTH(date)) AS month, COUNT(*) AS count FROM visitors
GROUP BY YEAR(date), MONTH(date) ORDER BY YEAR(date) ASC, MONTH(date) ASC";

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

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

$month_data[] = array('y' => $row['month'], 'a' => $row['count']);

// Retrieve data by week

$sql = "SELECT YEARWEEK(date) AS week, COUNT(*) AS count FROM visitors GROUP BY


YEARWEEK(date) ORDER BY YEARWEEK(date) ASC";

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

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

$week_data[] = array('y' => $row['week'], 'a' => $row['count']);

// Close database connection

$conn->close();

?>

JavaScript Code:

javascript

Copy code

$(document).ready(function() {

// Bar Chart for Yearly Data

Morris.Bar({

element: 'bar-chart-year',
data: <?php echo json_encode($year_data); ?>,

xkey: 'y',

ykeys: ['a'],

labels: ['Total Visitors'],

barColors: ['#ff9b44'],

resize: true,

redraw: true

});

// Bar Chart for Monthly Data

Morris.Bar({

element: 'bar-chart-month',

data: <?php echo json_encode($month_data); ?>,

xkey: 'y',

ykeys: ['a'],

labels: ['Total Visitors'],

barColors: ['#ff9b44'],

resize: true,

redraw: true

});

// Bar Chart for Weekly Data

Morris.Bar({

element: 'bar-chart-week',

data: <?php echo json_encode($week_data); ?>,

xkey: 'y',

ykeys: ['a'],

labels: ['Total Visitors'],

barColors: ['#ff9b44'],
resize: true,

redraw: true

});

});

Please note that you will need to replace the database credentials and table name in the PHP code
with your own. Also, make sure to include the Morris.js library and the jQuery library in your HTML
file.

In this article, we will learn how to show success message after completing a task.
You will learn how to use sessions in php and use sessions to set the flash message
and display them after success.

Note : To use session in any page, we need to start the session on top of the
page using the session_start();

After inserting / updating the data in the database, set the session message in a
session variable as shown below:

<?php 
    session_start();
    //after insert or update 
    $_SESSION['status'] = "<Type Your success message here>";
?>
Now we have set the message and now paste the below code in the page where you
want display the success message :

Note : Import the Bootstrap v5 CSS and JS using the cdn link available on
getboostrap.com, for good design and results as shown in the above video.

<?php 
    session_start();
    
    if(isset($_SESSION['status']))
    {
        ?>
            <div class="alert alert-warning alert-dismissible fade show" rol
e="alert">
                <strong>Hey !</strong> <?= $_SESSION['status']; ?>
                <button type="button" class="btn-close" data-bs-
dismiss="alert" aria-label="Close"></button>
            </div>
        <?php 
        unset($_SESSION['status']);
    }

?>

5997@asna

Asna@5997
Privacy Policy

Last updated: March 11, 2023

Introduction

Our visitor information system (the "Service") is provided by XYZ Corporation ("we," "us," "our"). We
are committed to protecting the privacy of our users ("you," "your"). This privacy policy ("Policy")
describes how we collect, use, and disclose personal information about users of the Service.

What information do we collect?

We collect the following types of personal information:

Contact information, such as your name, email address, and phone number.

Location information, such as your city or zip code.

Usage information, such as your browsing history and search queries.

How do we use your information?

We use your personal information for the following purposes:

To provide and improve the Service.

To communicate with you about the Service.

To personalize your experience on the Service.

To conduct research and analytics.

To comply with legal obligations.

Do we share your information?


We may share your personal information with third-party service providers who perform services on
our behalf, such as data storage and processing. We may also share your information with law
enforcement agencies or regulators in response to a legal request.

How do we protect your information?

We use reasonable measures to protect your personal information from unauthorized access,
disclosure, alteration, and destruction. However, no security measure is completely secure, so we
cannot guarantee the security of your information.

Your rights

You have the right to access and control your personal information. You can do this by contacting us
at [email protected]. You also have the right to lodge a complaint with a data protection
authority.

Changes to this Policy

We may update this Policy from time to time. If we make material changes, we will notify you by
email or by posting a notice on the Service prior to the change becoming effective.

Contact Us

If you have any questions or concerns about this Policy, please contact us at [email protected].

You might also like