douzhuang6321 2018-05-20 10:53
浏览 31
已采纳

特殊字符在任何地方都有效,但在控制器codeigniter中没有

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:

  1. Get the id of the category using category name(from url)
  2. Get the products with the specific category id
  3. 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:

  1. In the head i have <meta charset="utf-8">
  2. In config file i have this $config['charset'] = 'utf-8';
  3. In database file i have this 'char_set' => 'utf8' and 'dbcollat' => 'utf8_general_ci'

展开全部

  • 写回答

1条回答 默认 最新

  • dongwei4096 2018-05-20 14:01
    关注

    Sounds like you just need:

    urldecode()

    Decodes any %## encoding in the given string. Plus symbols ('+') are decoded to a space character.

    $name_to_pass = implode(' ',explode('_', urldecode($name)));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部