What is Faceted Navigation in SEO?

Faceted Navigation is a technique that allows users to filter and narrow down search results based on different attributes or facets. In this example, we will be using PHP and Apache to build a faceted navigation system.

How would I implement Faceted Navigation using PHP and Apache?

Faceted Navigation is a technique that allows users to filter and narrow down search results based on different attributes or facets. In this example, we will be using PHP and Apache to build a faceted navigation system.

Assuming that you have already set up PHP and Apache on your local machine, here is an example code:

index.php:

<?php

// Connect to the database
$db = mysqli_connect('localhost', 'username', 'password', 'database_name');

// Define the initial query
$query = "SELECT * FROM products";

// Check if a filter has been applied
if (isset($_GET['filter'])) {
  // Build the filter query
  $filter = $_GET['filter'];
  $query .= " WHERE $filter";
}

// Execute the query
$result = mysqli_query($db, $query);

// Build the facet navigation
$facets = array(
  'category' => 'Category',
  'brand' => 'Brand',
  'color' => 'Color'
);

?>

<!DOCTYPE html>
<html>
<head>
  <title>Faceted Navigation Example</title>
</head>
<body>

  <h1>Faceted Navigation Example</h1>

  <div class="facets">
    <h2>Facets</h2>
    <ul>
      <?php foreach ($facets as $key => $label): ?>
        <li>
          <h3><?php echo $label; ?></h3>
          <ul>
            <?php
              // Build the facet options
              $facet_query = "SELECT DISTINCT $key FROM products";
              $facet_result = mysqli_query($db, $facet_query);
              while ($facet_row = mysqli_fetch_assoc($facet_result)):
            ?>
              <li>
                <a href="?filter=<?php echo "$key='$facet_row[$key]'"; ?>">
                  <?php echo $facet_row[$key]; ?>
                </a>
              </li>
            <?php endwhile; ?>
          </ul>
        </li>
      <?php endforeach; ?>
    </ul>
  </div>

  <div class="results">
    <h2>Results</h2>
    <ul>
      <?php while ($row = mysqli_fetch_assoc($result)): ?>
        <li>
          <h3><?php echo $row['name']; ?></h3>
          <p><?php echo $row['description']; ?></p>
          <p>Price: <?php echo $row['price']; ?></p>
        </li>
      <?php endwhile; ?>
    </ul>
  </div>

</body>
</html>

This code example defines a MySQL database connection, builds the initial query, checks if a filter has been applied, executes the query, and builds the facet navigation based on the categories, brands, and colors available in the database.

  • When a user clicks on a facet option, the filter query is updated, and the results are reloaded based on the selected filter.

Note that this code is just an example, and you will need to modify it to fit your specific requirements and database schema.

  • Additionally, you will need to style the HTML and CSS to match your design.

Here’s an example of Faceted Navigation using JSP and Spring MVC: