-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_single.php
134 lines (110 loc) · 5.63 KB
/
fetch_single.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
require_once 'function.php'; // Use require instead of include for better error handling
require_once __DIR__ . '/vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
$dbHost = $_ENV['DB_HOST'];
$dbName = $_ENV['DB_NAME']; // Add this line to get the database name
$dbUser = $_ENV['DB_USER'];
$dbPass = $_ENV['DB_PASS'];
// Create a PDO instance and set error mode to exceptions
try {
// Correct the DSN format
$conn = new PDO($dbHost . ';dbname=' . $dbName, $dbUser, $dbPass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if (isset($_GET["id"])) {
$productID = $_GET["id"];
$query = "SELECT * FROM producttb p
LEFT JOIN search s ON p.product_name = s.title
WHERE s.title = :product_id
LIMIT 1";
$statement = $conn->prepare($query);
$statement->bindParam(':product_id', $productID, PDO::PARAM_STR_CHAR);
$statement->execute();
$result = $statement->fetch(PDO::FETCH_ASSOC);
if ($result) {
$output = [
"product_name" => $result["product_name"],
// "department" => $result["department"],
"description" => $result["description"],
"keywords" => $result["keywords"],
// "course" => $result["course"],
// "university" => $result["university"],
"product_price" => $result["product_price"],
// "level" => $result["level"],
// "faculty" => $result["faculty"]
];
if ($result["product_image"]) {
$output["product_image"] = '<img src="assets/Images/' . $result["product_image"] . '" class="img-thumbnail" width="100" height="100" /><input type="hidden" name="hidden_product_image" value="' . $result["product_image"] . '" />';
} else {
$output["product_image"] = '<input type="hidden" name="hidden_product_image" value="" />';
}
// Query to get options for the "University" field
$universityQuery = "SELECT DISTINCT university FROM university_faculty_department";
$universityStatement = $conn->query($universityQuery);
$universities = $universityStatement->fetchAll(PDO::FETCH_COLUMN);
// Construct the "University" select element with the preselected option
$universitySelect = '<option value="">Select university</option>';
foreach ($universities as $university) {
$selected = ($university == $result["university"]) ? 'selected' : '';
$universitySelect .= '<option value="' . $university . '" ' . $selected . '>' . $university . '</option>';
}
$output["universitySelect"] = $universitySelect;
// Construct the "Level" select element with the available options
$levelSelect = '<option value="100">100L</option>
<option value="200">200L</option>
<option value="300">300L</option>
<option value="400">400L</option>
<option value="500">500L</option>';
// Set the selected option based on the database value
$selectedOption = $result["level"];
// Add the selected attribute to the option
$levelSelect = str_replace('value="' . $selectedOption . '"', 'value="' . $selectedOption . '" selected', $levelSelect);
$output["levelSelect"] = $levelSelect;
// Construct the "Type" select element
$typeSelect = '<option value="1">Books</option><option value="0">Projects</option>';
// Set the selected option based on the database value
$selectedOption = ($result["type"] == 1) ? '1' : '0';
// Add the selected attribute to the option
$typeSelect = str_replace('value="' . $selectedOption . '"', 'value="' . $selectedOption . '" selected', $typeSelect);
$output["typeSelect"] = $typeSelect;
// Query to get options for the "faculty" field
$facultyQuery = "SELECT DISTINCT faculty FROM university_faculty_department";
$facultyStatement = $conn->query($facultyQuery);
$faculties = $facultyStatement->fetchAll(PDO::FETCH_COLUMN);
// Construct the "faculty" select element with the preselected option
$facultySelect = '<option value="">Select faculty</option>';
foreach ($faculties as $faculty) {
$selected = ($faculty == $result["faculty"]) ? 'selected' : '';
$facultySelect .= '<option value="' . $faculty . '" ' . $selected . '>' . $faculty . '</option>';
}
$output["facultySelect"] = $facultySelect;
// Query to get options for the "department" field
$departmentQuery = "SELECT DISTINCT department FROM university_faculty_department";
$departmentStatement = $conn->query($departmentQuery);
$departments = $departmentStatement->fetchAll(PDO::FETCH_COLUMN);
// Construct the "department" select element with the preselected option
$departmentSelect = '<option value="">Select department</option>';
foreach ($departments as $department) {
$selected = ($department == $result["department"]) ? 'selected' : '';
$departmentSelect .= '<option value="' . $department . '" ' . $selected . '>' . $department . '</option>';
}
$output["departmentSelect"] = $departmentSelect;
// Query to get options for the "course" field
$courseQuery = "SELECT DISTINCT course FROM university_faculty_department";
$courseStatement = $conn->query($courseQuery);
$courses = $courseStatement->fetchAll(PDO::FETCH_COLUMN);
// Construct the "course" select element with the preselected option
$courseSelect = '<option value="">Select course</option>';
foreach ($courses as $course) {
$selected = ($course == $result["course"]) ? 'selected' : '';
$courseSelect .= '<option value="' . $course . '" ' . $selected . '>' . $course . '</option>';
}
$output["courseSelect"] = $courseSelect;
// Send all HTML strings back in the response
echo json_encode($output);
}
}
} catch (PDOException $e) {
die("Database connection failed: " . $e->getMessage());
}