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
You have several options here.
Make an input for every search-item you wanna use:
<input name="Landlord">
<input name="LandlordGroup">
<input name="Quarter">
...
then you can keep the php as is.
This of course is not very neat for the user.
Another option is to add a select
item, which lets the user selct what to search for:
<input name="search">
<select name="searchItem">
<option value="LANDLORD">Landlord
<option value="LANDLORD_GROUP">Landlord Group
<option value="QUARTER">Quarter
//.. more items here to add
</select>
then you would do something like that in your php script:
<?php
$search = $_GET['search'];
$searchItem = $_GET['searchItem'];
// another way here to define the insexes as ass.Array, makes it easier later on.
// NOTE that the keys have to match the values of the select-options
$csvIndexes = array("LANDLORD"=>0,
"LANDLORD_GROUP"=>1,
"QUARTER"=>2,
//...
);
// $row = 1; // not needed here
$output = [];
if ($handle = fopen("data.csv", "r")) {
while ($data = fgetcsv($handle, 1000, ",")) {
// $num = count($data);
// $row++;
if (!empty($search) && stripos($csvIndexes[$searchItem], $search)) {
$output[] = $data;
}
}
fclose($handle);
}
// leave the rest as is.
?>
I haven't tested this, since I don't have your csv, but I hope you get the idea!
A third option would be to search in every item per default.