I have a database for a restaurant.The owner will add categories such Pizza,Desert or Risotto(no accents or special characters) or categories with special characters/accents such as Salată, Pește or Vodkă(romanian characters).
The table for categories is set to accept special characters.I used ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8
for this.
In the navbar i have a dropdown with all the products(food and bar).
To access one category i'm using GET
like this:
<?php foreach ($categories_bar as $category): ?>
<a class="dropdown-item" href="<?php echo site_url('/bar/'.implode('_',explode(" ",$category['category_name']))); ?>"><?php echo $category['category_name']; ?></a>
<?php endforeach; ?>
Using this, the URL
will be http://localhost/quartiere/bar/name_of_the_category if the category name will have 2 or more words or http://localhost/quartiere/bar/nameofthecategory if the category name will contain only one word.
When the user click's on a category, the server will make this actions:
- Get the id of the category using category name(from url)
- Get the products with the specific category id
- Display them on a page.
The problem is that when a category with special characters is clicked, to the database will not go the normal name...it will go something like this(for the word Pălincă)-> P%C4%83linc%C4%83.
It's strange beacuse in the dropdown the names are displayed correctly.
The function that should display the products for every category is this.At the beginning i'm reversing the explode-implde process that i'm doing in navbar.Doing this i will have a string just like the string from database:
public function bar_view($name = NULL){
$name_to_pass = implode(' ',explode('_',$name));
$category = $this->products_model->get_category_id($name_to_pass);
$products = $this->products_model->get_bar_prods_by_category($category);
$data['title'] = $name_to_pass;
$data['products'] = $products;
if(empty($data['products'])){
show_404();
}
$this->load->view('templates/header');
$this->load->view('menu/bar_view',$data);
$this->load->view('templates/footer');
}
To specify:
- In the
head
i have<meta charset="utf-8">
- In config file i have this
$config['charset'] = 'utf-8';
- In database file i have this
'char_set' => 'utf8'
and'dbcollat' => 'utf8_general_ci'