Welcome Guest, Not a member yet? Register   Sign In
Query Builder "Where" with Array
#1

Hello everyone! I just started learning CodeIgniter 4 and will be very thankful if you help with my problem. In my database I have a table with various products. The table has columns "brand" and "country". I would like to display all the products and add checkboxes for brands and countries so that user can filter the displayed products. Here's my form:
Code:
<form method="post" action="<?=base_url('page/view/'.$currentCatalog);?>">
    <h2>Brands</h2>
        <?php foreach ($filters as $item) : ?>
            <input type="checkbox" name="brand[]" value="<?=$item['brand'];?>"><?=$item['brand'];?><br>
        <?php endforeach ?>
    <h2>Countries</h2>
        <?php foreach ($filters as $item) : ?>
            <input type="checkbox" name="country[]" value="<?=$item['country'];?>"><?=$item['country'];?><br>
        <?php endforeach ?>
    <input type="submit">
</form>

In my controller I get the post data and send it to the model (I am not showing you all the code, this $catalog is not important):
Code:
$postFilters = $this->request->getPost();
$data = [
    'catalog' => $model->displayItems($catalog, $postFilters),
];

After submitting the form $postFilters looks like this:

Array ( [brand] => Array ( [0] => someBrand) [country] => Array ( [0] => someCountry)

In my model I use this query:
Code:
$items = $this->where('catalog', $catalog)
              ->where($postFilters)
              ->findAll();
return $items;

If one brand and/or one country are selected in the form, everything works fine. But if I select more than one country or brand, I get error:
    mysqli_sql_exception #4078
    Illegal parameter data types varchar and row for operation '='

Could you please help me solve this? "Brand" and "Country" will not be the only parameters in the form, I will add more, so my query has to be dynamic.
Reply


Messages In This Thread
Query Builder "Where" with Array - by IvanBell - 05-18-2020, 02:52 AM
RE: Query Builder "Where" with Array - by akinuri - 05-19-2020, 09:20 AM



Theme © iAndrew 2016 - Forum software by © MyBB