0% found this document useful (0 votes)
20 views

COMPILER DESIGN

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)
20 views

COMPILER DESIGN

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/ 4

<?

php
session_start();

// Database connection (Replace with your actual database credentials)


$servername = "your_servername";
$username = "your_username";
$password = "your_password";
$dbname = "autolink_motors";

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

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

// Function to fetch products from the database


function getProducts() {
global $conn;
$sql = "SELECT * FROM products";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
return $result->fetch_all(MYSQLI_ASSOC);
} else {
return array();
}
}

// Function to add products to the cart


function addToCart($productId, $productName, $productPrice) {
$_SESSION['cart'][] = array('id' => $productId, 'name' =>
$productName, 'price' => $productPrice);
}

// Function to remove products from the cart


function removeFromCart($index) {
unset($_SESSION['cart'][$index]);
}

// Function to display the cart


function displayCart() {
if (empty($_SESSION['cart'])) {
echo "No items in the cart";
} else {
foreach ($_SESSION['cart'] as $index => $item) {
echo "{$item['name']} - {$item['price']} <button
onclick=\"removeFromCart($index)\">Remove</button><br>";
}
}
}

// Function to perform checkout and redirect to checkout.php


function checkout() {
// Perform additional checkout logic here

// Redirect to checkout.php after the transaction is completed


header("Location: checkout.php");
exit();
}

// Function to filter products based on search query


function filterProducts($query) {
global $conn;
$query = mysqli_real_escape_string($conn, $query);
$sql = "SELECT * FROM products WHERE name LIKE '%$query%'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
return $result->fetch_all(MYSQLI_ASSOC);
} else {
return array();
}
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Autolink Motors</title>
<link rel="stylesheet" type="text/css" href="Cart.css">
<script src="Cart.js"></script>
<script>
// Function to dynamically filter products based on search input
function searchProducts(query) {
const productCards = document.querySelectorAll('.product-card');
query = query.toLowerCase();
productCards.forEach(card => {
const productName =
card.querySelector('h3').innerText.toLowerCase();
const shouldShow = productName.includes(query);
card.style.display = shouldShow ? 'block' : 'none';
});
}
</script>
</head>
<body>
<header>
<img class="div1" src="autoclear.png" alt="Autolink Motors Logo">
<nav>
<ul class="nav-list">
<li><a href="Index.html">Home</a></li>
<li><a href="Cart.html">Cart</a></li>
<li><a href="checkout.html">Checkout</a></li>
<li><a href="Aboutus.html">About Us</a></li>
<li><a href="contacts.html">Contacts</a></li>

</ul>
</nav>
</header>

<div class="group">
<svg class="icon" aria-hidden="true" viewBox="0 0 24 24">
<g>
<path d="M21.53 20.47l-3.66-3.66C19.195 15.24 20 13.214
20 11c0-4.97-4.03-9-9-9s-9 4.03-9 9 4.03 9 9 9c2.215 0 4.24-.804 5.808-
2.13l3.66
3.66c.147.146.34.22.53.22s.385-.073.53-.22c.295-.293.295-.767.002-
1.06zM3.5 11c0-4.135 3.365-7.5 7.5-7.5s7.5 3.365 7.5 7.5-3.365 7.5-7.5
7.5-7.5-3.365-7.5-7.5z"></path>
</g>
</svg>

<input placeholder="Search" type="search" class="input"


oninput="searchProducts(this.value)">
</div>
<!-- Your search box goes here -->

<div class="container">
<div class="product-cards">
<?php
// Fetch products from the database
$products = getProducts();

// Display each product as a card


foreach ($products as $product) {
echo '<div class="product-card">';
echo '<img src="' . $product['image_path'] . '" alt="' .
$product['name'] . '">';
echo '<h3>' . $product['name'] . '</h3>';
echo '<p>$' . $product['price'] . '</p>';
echo '<div class="cart-actions">';
echo '<button class="add-to-cart" onclick="addToCart(' .
$product['id'] . ', \'' . $product['name'] . '\', ' . $product['price'] .
')">Add to Cart</button>';
echo '</div>';
echo '</div>';
}
?>
</div>

<div class="cart">
<h2>Shopping Cart</h2>
<div id="cart-items">
<?php
// Display the cart
displayCart();
?>
</div>
<button onclick="checkout()">Checkout</button>
</div>
</div>

<script src="backend.js"></script>
</body>
</html>

<?php
$conn->close();
?>

You might also like