| Linux in-mum-web1499.main-hosting.eu 5.14.0-503.40.1.el9_5.x86_64 #1 SMP PREEMPT_DYNAMIC Mon May 5 06:06:04 EDT 2025 x86_64 Path : /home/u901718425/domains/contentmgm.urbanpillar.in/public_html/ |
| Current File : /home/u901718425/domains/contentmgm.urbanpillar.in/public_html/index.php |
<?php
// Database connection
$host = 'localhost';
$dbname = 'u901718425_upload_info'; // Your database name
$username = 'u901718425_upload_info'; // Your database username
$password = 'Web.Developer@1'; // Your database password
$database = 'u901718425_upload_info';
$conn = new mysqli($host, $username, $password, $database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Handle saving and updating data via AJAX
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
if ($_POST['action'] === 'save') {
$title = $conn->real_escape_string($_POST['title']);
$link = $conn->real_escape_string($_POST['link']);
$price = $conn->real_escape_string($_POST['price']);
$city = $conn->real_escape_string($_POST['city']);
$editedDate = date('Y-m-d H:i:s');
$sql = "INSERT INTO your_table (title, link, price, city, edited_date) VALUES ('$title', '$link', '$price', '$city', '$editedDate')";
if ($conn->query($sql) === TRUE) {
$newId = $conn->insert_id;
echo json_encode(['status' => 'success', 'message' => 'Row saved successfully.', 'id' => $newId]);
} else {
echo json_encode(['status' => 'error', 'message' => 'Error: ' . $conn->error]);
}
exit;
} elseif ($_POST['action'] === 'update') {
$id = intval($_POST['id']);
$title = $conn->real_escape_string($_POST['title']);
$link = $conn->real_escape_string($_POST['link']);
$price = $conn->real_escape_string($_POST['price']);
$city = $conn->real_escape_string($_POST['city']);
$editedDate = date('Y-m-d H:i:s');
$sql = "UPDATE your_table SET title = '$title', link = '$link', price = '$price', city = '$city', edited_date = '$editedDate' WHERE id = $id";
if ($conn->query($sql) === TRUE) {
echo json_encode(['status' => 'success', 'message' => 'Row updated successfully.']);
} else {
echo json_encode(['status' => 'error', 'message' => 'Error: ' . $conn->error]);
}
exit;
}
}
// Export to Excel
if (isset($_GET['export'])) {
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=data.xls");
// Fetch data from the database
$result = $conn->query("SELECT * FROM your_table");
echo "Serial No.\tTitle\tLink Upload\tPrice\tCity\tDate Edited\n";
$serialNo = 1;
while ($row = $result->fetch_assoc()) {
echo "{$serialNo}\t{$row['title']}\t{$row['link']}\t{$row['price']}\t{$row['city']}\t{$row['edited_date']}\n";
$serialNo++;
}
exit;
}
// Handle file upload (CSV or Excel)
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file_upload'])) {
$file = $_FILES['file_upload'];
// Check file type (CSV or Excel)
$fileType = pathinfo($file['name'], PATHINFO_EXTENSION);
if ($fileType === 'csv') {
$handle = fopen($file['tmp_name'], 'r');
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
// Insert data into the database
$title = $conn->real_escape_string($data[0]);
$link = $conn->real_escape_string($data[1]);
$price = $conn->real_escape_string($data[2]);
$city = $conn->real_escape_string($data[3]);
$editedDate = date('Y-m-d H:i:s');
$conn->query("INSERT INTO your_table (title, link, price, city, edited_date) VALUES ('$title', '$link', '$price', '$city', '$editedDate')");
}
fclose($handle);
echo "CSV data imported successfully.";
} elseif ($fileType === 'xlsx' || $fileType === 'xls') {
// Use PHPExcel or PhpSpreadsheet to read Excel files (for simplicity, I'll skip the detailed code for this)
// You need to install PHPExcel or PhpSpreadsheet to parse Excel files
echo "Excel file upload functionality is not implemented in this example.";
}
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dynamic Row Management</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
/* Mobile responsiveness */
@media (max-width: 768px) {
table {
font-size: 12px;
}
.form-control {
font-size: 12px;
}
.btn {
padding: 5px 10px;
font-size: 12px;
}
}
</style>
</head>
<body>
<div class="container mt-4">
<h3 class="text-center">Dynamic Row Management</h3>
<!-- Search Bar -->
<div class="row mb-3">
<div class="col-8">
<input type="text" id="search-bar" class="form-control" placeholder="Search by Title">
</div>
<div class="col-4">
<select id="city-filter" class="form-control">
<option value="">All Cities</option>
<option value="Pune">Pune</option>
<option value="Mumbai">Mumbai</option>
</select>
</div>
</div>
<!-- Export and Upload Buttons -->
<div class="mb-3">
<button class="btn btn-info" id="export-btn">Export to Excel</button>
<form id="upload-form" enctype="multipart/form-data" style="display:inline;">
<input type="file" name="file_upload" class="form-control" accept=".csv, .xlsx, .xls" />
<button type="submit" class="btn btn-primary">Upload</button>
</form>
</div>
<!-- Data Table -->
<table class="table table-bordered table-responsive" id="data-table">
<thead>
<tr>
<th>Serial No.</th>
<th>Title</th>
<th>Link Upload</th>
<th>Price</th>
<th>City</th>
<th>View Link</th>
<th>Date Edited</th>
<th>Edit/Save</th>
</tr>
</thead>
<tbody>
<?php
$result = $conn->query("SELECT * FROM your_table");
$serialNo = 0;
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$serialNo++;
echo "
<tr data-id='{$row['id']}'>
<td>{$serialNo}</td>
<td><input type='text' class='form-control title' value='{$row['title']}' disabled></td>
<td><input type='text' class='form-control link-upload' value='{$row['link']}' disabled></td>
<td><input type='number' class='form-control price' value='{$row['price']}' disabled></td>
<td>
<select class='form-control city' disabled>
<option value='Pune' " . ($row['city'] === 'Pune' ? 'selected' : '') . ">Pune</option>
<option value='Mumbai' " . ($row['city'] === 'Mumbai' ? 'selected' : '') . ">Mumbai</option>
</select>
</td>
<td><a href='{$row['link']}' target='_blank' class='btn btn-info view-link'>View</a></td>
<td class='edited-date'>{$row['edited_date']}</td>
<td>
<button class='btn btn-warning edit-row'>Edit</button>
<button class='btn btn-success save-row' disabled>Save</button>
</td>
</tr>";
}
}
?>
</tbody>
</table>
<!-- Add Row Button -->
<div class="mb-3">
<button class="btn btn-success" id="add-row">Add Row</button>
</div>
</div>
<script>
$(document).ready(function () {
// Export to Excel functionality
$("#export-btn").click(function () {
window.location.href = "?export=true";
});
// Upload functionality
$("#upload-form").submit(function (e) {
e.preventDefault();
var formData = new FormData(this);
$.ajax({
url: "",
method: "POST",
data: formData,
contentType: false,
processData: false,
success: function (response) {
alert(response);
},
error: function () {
alert("Error uploading file!");
}
});
});
});
</script>
</body>
</html>