LOSearch

LOSearch is a compact, adaptable PHP class that streamlines database searches through programmable criteria. Part of a cohesive toolkit with LOForm (for form processing) and LODatabase (for database operations), it enables a flexible, component-based method for developing dynamic, data-centric PHP applications.

With LOSearch, you can specify search parameters directly in code, effortlessly filtering and retrieving database records. It supports various matching scenarios—from precise matches to partial matches and intricate conditions—offering comprehensive search capabilities.

LOSearch Demo

Input

<?php
require_once 'lodatabase.php';
require_once 'loform.php';
require_once 'losearch.php';

// --- Step 1: Initialize Database ---
$db = new LODatabase('products.json');

// Add sample data
$db->insert([
    'name' => 'Laptop',
    'price' => 999.99,
    'tags' => ['electronics', 'computing']
]);
$db->insert([
    'name' => 'Smartphone',
    'price' => 699.99,
    'tags' => ['electronics', 'mobile']
]);
$db->insert([
    'name' => 'Headphones',
    'price' => 149.99,
    'tags' => ['electronics', 'audio']
]);
$db->commit();

// --- Step 2: Initialize LOSearch ---
$search = new LOSearch($db);

// Define search criteria
$search->criteria(['column' => 'name', 'operator' => '%i']);
$search->criteria(['column' => 'price', 'operator' => '<']);
$search->criteria(['column' => 'tags', 'exact_match' => false]);

// --- Step 3: Create Search Form ---
$form = new LOForm();

$form->field([
    'type' => 'text',
    'name' => 'name',
    'title' => 'Product Name',
    'holder' => 'Enter product name...'
]);

$form->field([
    'type' => 'number',
    'name' => 'price',
    'title' => 'Max Price',
    'holder' => 'Enter max price...'
]);

$form->field([
    'type' => 'select',
    'name' => 'tags',
    'title' => 'Tags',
    'options' => [
        'electronics' => 'Electronics',
        'computing' => 'Computing',
        'mobile' => 'Mobile',
        'audio' => 'Audio'
    ]
]);

$form->field([
    'type' => 'button',
    'holder' => 'Search',
    'buttontype' => 'submit'
]);

// Load form values from GET
$form->load($_GET);

// --- Step 4: Perform Search and Display Results ---
if (!empty($_GET)) {
    $results = $search->search($_GET);
    
    echo "<h2>Search Results</h2>";
    if (empty($results)) {
        echo "<p>No products found.</p>";
    } else {
        echo "<ul>";
        foreach ($results as $product) {
            echo "<li>{$product['name']} - \${$product['price']} (Tags: " . implode(', ', $product['tags']) . ")</li>";
        }
        echo "</ul>";
    }
}

// Render the form
$form->form([
    'method' => 'get'
]);

// Output headers and footers for LOForm (if needed)
LOForm::header();
LOForm::footer();

Output