douzhuang6321 2018-05-20 18: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 22: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)));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化