Following on from a previous question i have my form which is but at the moment whenever i have tried to enter a keyword the whole table is just displayed. Is there a way to get it showing only matches. For example if i searched Housing leeds. it would show the entire row in a table?
Housing Leeds,Yorkshire LL,3,2013,221,235
index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="search.php" method="get">
<label>
Search
<input type="text" name="search" autocomplete="off">
</label>
<input type="submit" name="Search">
</form>
</body>
</html>
search.php
Help from another user:
search.php
<?php
$search = isset($_GET['search']) ? (int) trim($_GET['search']) : null;
$search = isset($_GET['search']) ? trim($_GET['search']) : null;
$search = isset($_GET['search']) ? (int) trim($_GET['search']) : null;
$search = isset($_GET['search']) ? trim($_GET['search']) : null;
$search = isset($_GET['search']) ? trim($_GET['search']) : null;
define('CSV_INDEX_LANDLORD', 0);
define('CSV_INDEX_LANDLORD_GROUP', 1);
define('CSV_INDEX_QUARTER', 2);
define('CSV_INDEX_YEAR', 3);
define('CSV_INDEX_ESTIMATED_PROJECT_COST', 4);
define('CSV_INDEX_ACTUAL_PROJECT_COST', 5);
$row = 1;
$output = [];
if (($handle = fopen("data.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
// if the user tried to search on year and it doesn't match, continue skips to the next row
// casting ensures we compare integers with integers
if (!empty($search) && stripos($data[CSV_INDEX_LANDLORD], $search) !== false) {
continue;
}
if (!empty($search) && stripos($data[CSV_INDEX_LANDLORD_GROUP], $search) !== false) {
continue;
}
if (!empty($search) && stripos($data[CSV_INDEX_QUARTER], $search) !== false) {
continue;
}
if (!empty($search) && stripos($data[CSV_INDEX_ESTIMATED_PROJECT_COST], $search) !== false) {
continue;
}
if (!empty($search) && stripos($data[CSV_INDEX_ACTUAL_PROJECT_COST], $search) !== false) {
continue;
}
$output[] = $data;
}
fclose($handle);
}
?>
<?php if (!empty($output)): ?>
<table>
<tr>
<th><strong>Landlord</strong></th>
<th><strong>Landlord Group</strong></th>
<th><strong>Quarter</strong></th>
<th><strong>Year</strong></th>
<th><strong>Estimated Project Costs</strong></th>
<th><strong>Actual Project Cost</strong>
<tr>
<?php foreach ($output as $row): ?>
<tr>
<td><?=$row[0]?></td>
<td><?=$row[1]?></td>
<td><?=$row[2]?></td>
<td><?=$row[3]?></td>
<td><?=$row[4]?></td>
<td><?=$row[5]?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
I am struggling to get it to work so that if I enter either a Landlord or Landlord group data relating to it is shown in the table. Below is a line of CSV data. The bold are the headings.
**Landlord,Landlord group,Quarter,Year,Estimated project costs (000s),Actual project cost (000s)**
Housing Leeds,Yorkshire LL,3,2013,221,235