3
Contributors
5
Replies
22
Views
2 Years
Discussion Span
Last Post by diafol
0

Your question is not all that understandable but the little knowledge I have is that you are trying to filter the results returned from a search field.

My suggestion is that you can make this asynchronously with AJAX. But if you have tried something post your codes so that we might help.

0

Hello okay let me make you understanaable about it thoug

have a lok to this website and on the left hand side you will see search filter this is what i am tring to create but have no idea so that is why i am looking for sugesstion and tutorial help though i have not started writing any code though but will do so by you guys help me out.

0

Usually, you create the code and we give tips on that.

There are hundreds if not thousands of search scripts out there.

Filtering by word(s) will be different to filtering by category and by individual item.

0

Yes I know that but I am unable to find any such tutorials so that is why i posted here and tring to get idea though so i would be able to impletemnt it well i will try it using codes and if helped or got any problem will post here but would you be able to give me just an idea so i can get started??

Here is my products code

<div class="col-md-3">
        <form id="form1" name="form1" method="post" action="products.php">

        <input type="checkbox" name="string" id="string" /> Samsung
        <input type="submit" name="button" id="button" value="Filter" />
        </label>
        </form>
    </div>

    <div class="col-md-8">

        <div class="row">
            <?php
                $result = mysqli_query($connection, "select * from products");
                while ($row=mysqli_fetch_array($result)){
            ?>
            <div class="prod">
                <div class="img">
                <img src="images/products/<?=$row['prod_img']?>" />

                <div class="btnbuy">
                <?php $pid = $row['serial']; ?>
                <a href='product_details.php?pid=<?php echo $pid; ?>' class="btnblue">Buy Now</a>
                </div>

                </div>

                <div class="ftrhead">
                <b><?=$row['product_name']?></b>
                </div>

                <div class="descrip">        
                <?=$row['short_descrip']?>
                </div>

                <div class="price">
                <p>Price: $<?=$row['product_price']?></p>
                </div>

            </div>   
            <?php } ?>
        </div>
    </div>

please provide me a small idea so i can do it this is my stage of learning though i am not an expert i hope it;s not bad to learn from seniaors as I also want to become expert in php :)

0

That's OK uk, but this is a pretty vague question, so asking for a bit of code from you will help us refine our suggestions.

Still not sure what you want to search on (filters). Let us assume that you have a list of manufacturers (from your manufacturers table):

manufacturers: id | label

Checkbox creator:

function create_checkboxes($db, $table, $idField, $labelField, $filterSuffix)
{
    $output = '';
    if($result = $db->query("SELECT $idField AS ccID, $labelField AS ccLABEL FROM $table ORDER BY $labelField"))
    {
        while ($row = $result->fetch_assoc()) {
            $output .= "<label><input value='{$row['ccID']}' type='checkbox' name='filter[$filterSuffix][]' /> {$row['ccLABEL']}</label>";
        }

        $result->free();
    }
}

Then in the appropriate place:

<form action="search.php" method="get">
    <?=create_checkboxes($mysqli, 'manufacturers', 'id', 'label', 'man')?>
    <input type="submit" name="submitFilter" value="Filter Results" />
</form>

You can then grab the checked values:

//list FK fields from which you allow filters

$allowedFilters = ['man'=>'manufacturer_id','cat'=>'category_id'];
$whereClauseItems = '';
$whereElements = [];
$where = '';

$idOptions = array(
    'options' => array(
        'min_range' => 1
    )
);

if(isset($_GET['filter']))
{

    foreach($allowedFilters as $k=>$filterTable)
    {
        if(isset($_GET['filter'][$k]))
        {
            foreach($_GET['filter'][$k] as $item)
            {
                if(trim($item) && filter_var($item, FILTER_VALIDATE_INT, $idOptions)) $whereElements[$k] = intval($item);
            }
            if(count($whereElements[$k])) $whereClauseItems[] = "`$filterTable` IN (" . implode(',',$whereElements) . ")";
        }
    }
    if(count($whereClauseItems[])) $where = ' ' . implode(' AND ', $whereClauseItems); //or use ' OR ' ??
}

$searchSQL = "SELECT ... FROM products" . $where;

//do filtered list and pass to page

This is not tested and I tried to make it as flexible as possible for searching on multiple columns - and this assumes you're passing integers to the SQL. It could be modified to present an SQL prepared statement instead - which actually would be safer.

Just a rough idea though.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.