Php 动态显示结果

Php 动态显示结果,php,sql,codeigniter,Php,Sql,Codeigniter,我有一张“产品”的桌子。列是'id'和'product_name'等。现在我如何使用codeigniter设置这样的URL localhost/product/details/apple-iphone-7-50064584 现在这里,产品名称是苹果Iphone 7,id是50064584。如何组合它们并动态显示结果?localhost/product/details/apple-iphone-7-50064584 域/appname/controller/method/arguments cla

我有一张“产品”的桌子。列是'id'和'product_name'等。现在我如何使用codeigniter设置这样的URL


localhost/product/details/apple-iphone-7-50064584


现在这里,产品名称是苹果Iphone 7,id是50064584。如何组合它们并动态显示结果?

localhost/product/details/apple-iphone-7-50064584

域/appname/controller/method/arguments

class Details {

  function show($name, $id) {
       $this->load->view('hello);
  }
}

如果删除配置文件的'index.php',将生成类似“localhost/product/details/$product/$id”的url。

。看起来你的问题有点“宽”

为了正确回答,我会写一篇完整的教程


你哪里有问题?查询、活动记录、URI段?..

本指南假设您使用的是Apache web服务器

步骤1:删除index.php

打开/application/config/config.php

改变这个

$config['index_page'] = 'index.php';
对此

$config['index_page'] = '';
保存文件

步骤2:添加.htaccess文件

在与主index.php文件相同的目录中创建一个.htaccess文件,其中包含以下内容

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
保存文件

步骤3:配置Apache

找到Apache配置文件(httpd.conf)。对我来说,它位于/etc/httpd/conf/httpd.conf

确保在加载mod_rewrite指令之前没有注释

例如,如果你看到这个

#LoadModule rewrite_module modules/mod_rewrite.so
换成这个

LoadModule rewrite_module modules/mod_rewrite.so 
保存文件

重新启动apache以使更改生效

步骤4:创建控制器

在/application/controllers/目录中创建一个新的PHP文件,并将其命名为Products.PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Products extends CI_Controller
{

    public function detail($id)
    {

        // Retrieve The Product From The Database
        $this->db->from('products');
        $this->db->where('id', $id);
        $query = $this->db->get();

        // Product Found
        if($query->num_rows() == 1)
        {

            $product = $query->row();

            // Pass The Product Details To The Product Details View Page And Display The Page
            $this->load->view('product-details', $product);

        // Product Not Found
        } else {

            // Display 404 Page
            show_404();

        }

    }

}
保存文件

在浏览器中输入以下URL:


谢谢,你是主人@codeigniter非常欢迎您。谢谢你的夸奖!
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title><?php echo $product_name; ?></title>
</head>
<body>

    <h1><?php echo $product_name; ?></h1>

    <!-- Add the rest of your HTML here -->

</body>
</html>
$route['product/details/(:any)-(:num)']  = 'products/detail/$2';