0% found this document useful (0 votes)
14 views7 pages

Bi

Uploaded by

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

Bi

Uploaded by

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

<?

php
$host = "localhost:3307"; // ‫اسم مضيف قاعدة البيانات‬
$dbname = "products"; // ‫اسم قاعدة البيانات‬
$username = "root"; // ‫اسم المستخدم‬
$password = ""; // ‫كلمة المرور‬

try {
// ‫ االتصال بقاعدة البيانات باستخدام‬PDO
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// ‫ معالجة تحميل ملف‬CSV


if (isset($_FILES['csv_file'])) {
$file = $_FILES['csv_file']['tmp_name'];
$handle = fopen($file, 'r');

// ‫تخطي عنوان العمود‬


fgetcsv($handle);

// ‫ قراءة البيانات من ملف‬CSV ‫وإدخالها إلى قاعدة البيانات‬


while (($data = fgetcsv($handle)) !== FALSE) {
$product_id = $data[0]; // ‫معرف المنتج‬
$product_name = $data[1]; // ‫اسم المنتج‬
$aisle_id = $data[2]; // ‫معرف الممر‬
$department_id = $data[3]; // ‫معرف القسم‬
$price = $data[4]; // ‫السعر‬

// ‫إدخال البيانات في الجدول‬


$stmt = $pdo->prepare("INSERT INTO products (product_id, product_name,
aisle_id, department_id, price) VALUES (?, ?, ?, ?, ?)");
$stmt->execute([$product_id, $product_name, $aisle_id, $department_id,
$price]);
}

fclose($handle);
echo "Data imported successfully!";
} else {
echo "No file uploaded.";
}
} catch (PDOException $e) {
// ‫في حال حدوث خطأ‬
echo 'Connection failed: ' . $e->getMessage();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Search Products from MySQL</title>
<style>
#suggestions {
margin-top: 10px;
display: flex;
flex-wrap: wrap;
gap: 5px;
}
#suggestions div {
padding: 5px;
cursor: pointer;
background-color: #f0f0f0;
border-radius: 5px;
white-space: nowrap;
}
#suggestions div:hover {
background-color: #d0d0d0;
}
</style>
</head>
<body>
<h1>Upload CSV File</h1>
<form action="upload_csv.php" method="post" enctype="multipart/form-data">
<input type="file" name="csv_file" accept=".csv" required>
<button type="submit">Upload</button>
</form>
<br>
<a href="display_products.php">View Products</a> <!-- ‫رابط إلى صفحة عرض‬
‫ المنتجات‬-->
<h2>Search Products</h2>
<input type="text" id="search" placeholder="Start typing product name..."
oninput="searchProduct()">
<div id="suggestions"></div>

<script>
let products = [];

// ‫ جلب البيانات من‬PHP ‫عند تحميل الصفحة‬


window.onload = function() {
fetch('get_products.php') // ‫ هذا هو ملف‬PHP ‫الذي كتبناه‬
.then(response => response.json())
.then(data => {
products = data.map(product => product.product_name);
})
.catch(error => console.error('Error fetching products:', error));
};

// ‫ حساب مسافة‬Levenshtein
function levenshteinDistance(a, b) {
const matrix = [];

for (let i = 0; i <= b.length; i++) {


matrix[i] = [i];
}

for (let j = 0; j <= a.length; j++) {


matrix[0][j] = j;
}

for (let i = 1; i <= b.length; i++) {


for (let j = 1; j <= a.length; j++) {
if (b.charAt(i - 1) === a.charAt(j - 1)) {
matrix[i][j] = matrix[i - 1][j - 1];
} else {
matrix[i][j] = Math.min(
matrix[i - 1][j - 1] + 1,
matrix[i][j - 1] + 1,
matrix[i - 1][j] + 1
);
}
}
}

return matrix[b.length][a.length];
}

// ‫البحث في المنتجات بناًء على إدخال المستخدم‬


function searchProduct() {
const query = document.getElementById('search').value.trim();
if (query.length > 2 && products.length > 0) {
const suggestions = products
.map(product_name => ({
name: product_name,
distance: levenshteinDistance(query, product_name)
}))
.sort((a, b) => a.distance - b.distance)
.slice(0, 10); // ‫ نتائج‬10 ‫اختيار أفضل‬

displaySuggestions(suggestions);
} else {
document.getElementById('suggestions').innerHTML = '';
}
}

// ‫عرض االقتراحات‬
function displaySuggestions(suggestions) {
const suggestionsDiv = document.getElementById('suggestions');
suggestionsDiv.innerHTML = '';
suggestions.forEach(suggestion => {
const div = document.createElement('div');
div.textContent = suggestion.name;
div.onclick = () => {
document.getElementById('search').value = suggestion.name;
suggestionsDiv.innerHTML = '';
};
suggestionsDiv.appendChild(div);
});
}
</script>
</body>
</html>
<?php
$host = "localhost:3307"; // ‫اسم مضيف قاعدة البيانات‬
$dbname = "products"; // ‫اسم قاعدة البيانات‬
$username = "root"; // ‫اسم المستخدم‬
$password = ""; // ‫كلمة المرور‬

try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $pdo->query('SELECT product_name FROM products');


$products = $stmt->fetchAll(PDO::FETCH_ASSOC);

echo json_encode($products);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>

<?php
$host = "localhost:3307"; // ‫اسم مضيف قاعدة البيانات‬
$dbname = "products"; // ‫اسم قاعدة البيانات‬
$username = "root"; // ‫اسم المستخدم‬
$password = ""; // ‫كلمة المرور‬

try {
// ‫ االتصال بقاعدة البيانات باستخدام‬PDO
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// ‫استعالم لجلب جميع المنتجات‬


$stmt = $pdo->query("SELECT product_id, product_name, aisle_id, department_id,
price FROM products");
$products = $stmt->fetchAll(PDO::FETCH_ASSOC); // ‫جلب جميع النتائج كمصفوفة‬

} catch (PDOException $e) {


// ‫في حال حدوث خطأ‬
echo 'Connection failed: ' . $e->getMessage();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Products List</title>
<style>
table {
width: 80%;
margin: 20px auto;
border-collapse: collapse;
}
th, td {
padding: 10px;
text-align: left;
border: 1px solid #ccc;
}
th {
background-color: #f2f2f2;
}
#search {
width: 80%;
padding: 10px;
margin: 20px auto;
display: block;
border: 1px solid #ccc;
border-radius: 5px;
}
#suggestions {
position: absolute;
z-index: 1000;
width: 80%;
background-color: white;
border: 1px solid #ccc;
display: none;
}
#suggestions div {
padding: 10px;
cursor: pointer;
}
#suggestions div:hover {
background-color: #f0f0f0;
}
</style>
</head>
<body>
<h1>List of Products</h1>
<input type="text" id="search" placeholder="Start typing product name..."
oninput="filterProducts()">
<div id="suggestions"></div> <!-- ‫ قسم االقتراحات‬-->

<?php if (count($products) > 0): ?>


<table>
<thead>
<tr>
<th>Product ID</th>
<th>Product Name</th>
<th>Aisle ID</th>
<th>Department ID</th>
<th>Price</th>
</tr>
</thead>
<tbody id="productsTable">
<?php foreach ($products as $product): ?>
<tr>
<td><?php echo htmlspecialchars($product['product_id']); ?
></td>
<td><?php echo
htmlspecialchars($product['product_name']); ?></td>
<td><?php echo htmlspecialchars($product['aisle_id']); ?
></td>
<td><?php echo htmlspecialchars($product['department_id']);
?></td>
<td><?php echo htmlspecialchars($product['price']); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php else: ?>
<p>No products found.</p>
<?php endif; ?>

<script>
const products = <?php echo json_encode($products); ?>; // ‫تحويل مصفوفة‬
‫ المنتجات إلى‬JSON

function filterProducts() {
const query = document.getElementById('search').value.toLowerCase();
const suggestionsDiv = document.getElementById('suggestions');
suggestionsDiv.innerHTML = ''; // ‫تفريغ االقتراحات السابقة‬
suggestionsDiv.style.display = 'none'; // ‫إخفاء قسم االقتراحات‬

if (query.length > 0) {
const filteredProducts = products.filter(product =>
product.product_name.toLowerCase().includes(query)
);

if (filteredProducts.length > 0) {
filteredProducts.forEach(product => {
const div = document.createElement('div');
div.textContent = product.product_name;
div.onclick = () => {
document.getElementById('search').value =
product.product_name;
suggestionsDiv.innerHTML = ''; // ‫تفريغ االقتراحات‬
suggestionsDiv.style.display = 'none'; // ‫إخفاء قسم‬
‫االقتراحات‬
};
suggestionsDiv.appendChild(div);
});
suggestionsDiv.style.display = 'block'; // ‫عرض قسم االقتراحات‬
}
}
}
</script>
</body>
</html>

You might also like