/    Sign up×
Bounties /Pin to ProfileBookmark

Need of help in making a view counter

Greetings! I am really in need of help in making a view counter for my bio page! Thank you for the answers in advance.

to post a answer
CSSFront-endHTMLJavaScript

1 Replies

Davinci•003 auto-replySure, I can help you with that. A simple way to create a view counter is by using a server-side language like PHP and a database like MySQL. Here's a basic example:

First, create a table in your MySQL database to store the page views:

``sql
CREATE TABLE page_views (
id INT AUTO_INCREMENT PRIMARY KEY,
page_name VARCHAR(255) NOT NULL,
view_count INT NOT NULL DEFAULT 0
);
`

Then, in your PHP script, you can increment the view count each time the page is loaded:

`php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

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

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$page_name = 'my_page'; // replace with your page name

// First, try to select the row for this page
$sql = "SELECT * FROM page_views WHERE page_name = '$page_name'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// If the row exists, increment the view count
$sql = "UPDATE page_views SET view_count = view_count + 1 WHERE page_name = '$page_name'";
} else {
// If the row does not exist, insert a new row with a view count of 1
$sql = "INSERT INTO page_views (page_name, view_count) VALUES ('$page_name', 1)";
}

if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}

$conn->close();
?>
``

This is a very basic example and does not include any error handling or security measures. In a real-world application, you would want to use prepared statements to prevent SQL injection attacks, and you would likely want to store more information about each view (like the user's IP address, the date and time of the view, etc.).

Was this helpful?

×

Success!

Help @BENI9166 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 2.22,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Samric24,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 10 SATS,

tipper: @neststayhome,
tipped: article
amount: 1000 SATS,
)...