CSV Search Tool

Searching through large data sets can be a time-consuming and daunting task. For organizations that manage a lot of data, it can be challenging to keep track of everything. One effective solution to this problem is to store the data in a CSV file, which can then be searched and filtered to quickly find the desired information. In this blog post, we will explore some use cases for a PHP code that allows users to search a CSV file and output the results onto a webpage.

Use Case 1: Product Catalog

An online store that sells a large variety of products can benefit greatly from a search functionality that allows customers to easily find the products they are looking for. By storing product information in a CSV file, such as product name, description, price, and image URL, a PHP code can be used to search the CSV file and output the relevant results onto the webpage. This not only enhances the user experience for customers, but also makes it easier for the store owner to manage and update the product catalog.

Use Case 2: Employee Directory

For organizations with a large number of employees, it can be challenging to keep track of everyone’s information. By storing employee information in a CSV file, such as name, department, job title, and contact information, a PHP code can be used to search the CSV file and output the relevant results onto a webpage. This makes it easier for employees to find and contact their colleagues, as well as for HR departments to manage and update employee records.

Use Case 3: Inventory Management

Businesses that manage a large inventory of products can benefit greatly from a search functionality that allows them to quickly find the products they need. By storing inventory information in a CSV file, such as product name, SKU, quantity, and location, a PHP code can be used to search the CSV file and output the relevant results onto a webpage. This makes it easier for businesses to manage and track their inventory, as well as to quickly locate the products they need.

Use Case 4: Supplier Directory

For organizations that work with multiple suppliers, it can be challenging to keep track of their contact information and product offerings. By storing supplier information in a CSV file, such as name, contact information, and product offerings, a PHP code can be used to search the CSV file and output the relevant results onto a webpage. This makes it easier for organizations to find and contact their suppliers, as well as to quickly locate the products they need.

Conclusion

A PHP code that allows users to search a CSV file and output the results onto a webpage can be a valuable tool for organizations that manage a lot of data. By implementing this functionality, businesses can enhance the user experience for their customers and employees, as well as make it easier to manage and update their data. The use cases discussed in this blog post are just a few examples of how this technology can be used, and the possibilities are endless.

The App


 

Tip: Search for cat or dog , or simply clicking search will show all rows.

Search Results

ProviderCategoryNameBirthdayDescriptionAttribue1Attribute2
ManchesterVetsRobin the cat03/01/2020likes fishFriendlyGrey
LondonShoppingRoger the dog04/03/2017Like a boneNastyBlack

The Code

csv search tool by cw.is is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.

You need to edit the path to your uploaded csv file , currently set to dummy.csv

You also need to change the image.jpg path to your own background image.

I have added in basic styling and google fonts.

The web link button will only work if you have a column in your csv file called url and the entries are complete hyperlinks.

The script will also hide columns currently set to header(s) labelled  hide2 and hide3 but you can add/remove or rename as you see fit.

The first column is shown in the output as this tends to by a key field.

You should be easily to adjust the code to suit your needs, such as hiding certain columns etc.

A download is available here as a zip file , which contains the php file & dummy.csv


<?php
// Define path to CSV file
$csv_path = "dummy.csv";

// Define which columns to exclude from the search results
$exclude_columns = array(array_search('url', fgetcsv(fopen($csv_path, 'r'))), array_search('hide2', fgetcsv(fopen($csv_path, 'r'))) !== false ? array_search('hide2', fgetcsv(fopen($csv_path, 'r'))) : null, array_search('hide3', fgetcsv(fopen($csv_path, 'r'))) !== false ? array_search('hide3', fgetcsv(fopen($csv_path, 'r'))) : null); // Excludes 'url', 'hide2', and 'hide3' columns if they exist in the CSV file

// Check if search has been submitted
if (isset($_GET['search'])) {
  // Get search query
  $search_query = $_GET['search'];

  // Open CSV file and read data
  $csv_file = fopen($csv_path, 'r');
  $csv_data = array();
  while (($row = fgetcsv($csv_file)) !== false) {
    $csv_data[] = $row;
  }
  fclose($csv_file);

  // Get header row from CSV data
  $header_row = $csv_data[0];

  // Remove header row from CSV data
  $csv_data = array_slice($csv_data, 1);

  // Search CSV data for matching rows
  $matching_rows = array();
  foreach ($csv_data as $row) {
    foreach ($row as $index => $value) {
      if (!in_array($index, $exclude_columns) && stripos($value, $search_query) !== false) {
        $matching_rows[] = $row;
        break;
      }
    }
  }

  // Display matching rows with header row
  echo "<h2 style='font-family: \"Open Sans\", sans-serif;'>Search Results</h2>";
  if (count($matching_rows) > 0) {
    echo "<table style='font-family: \"Roboto\", sans-serif;'>";
    echo "<tr style='background-color: #eee;'>";
    foreach ($header_row as $index => $value) {
      if ($index == array_search('url', $header_row)) {
        // Skip 'url' column
        continue;
      } else if ($index == 0) {
        // Include 'provider' column
        echo "<th style='padding: 10px;'>" . htmlspecialchars($value) . "</th>";
      } else if (!in_array($index, $exclude_columns)) {
        // Include other columns
        echo "<th style='padding: 10px;'>" . htmlspecialchars($value) . "</th>";
      }
    }
    echo "<th></th>"; // Include empty header for button column
    echo "</tr>";
    foreach ($matching_rows as $row) {
      echo "<tr>";
      foreach ($row as $index => $value) {
        if ($index == array_search('url', $header_row)) {
          // Skip 'url' column
          continue;
        } else if ($index == 0) {
          // Include 'provider' column
          echo "<td style='padding: 10px;'>" . htmlspecialchars($value) . "</td>";
        } else if (!in_array($index, $exclude_columns)) {
          // Include other columns
          echo "<td style='padding: 10px;'>" . htmlspecialchars($value) . "</td>";
        }
      }
      echo "<td><a href='" . htmlspecialchars($row[array_search('url', $header_row)]) . "' target='_blank'><button style='background-color: #007bff; color: #fff; border: none; border-radius: 5px; padding: 10px;'>Go to Website</button></a></td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "<p>No matching results found.</p>";
}
}

// Display search form
echo "<form method='get'>";
echo "<label for='search'>Search:</label>";
echo "<input type='text' name='search' id='search'>";
echo "<input type='submit' value='Search' style='background-color: #007bff; color: #fff; border: none; border-radius: 5px; padding: 10px;'>";
echo "</form>";
?>