【问题标题】:How to create clean url fetching data from mysql in php?如何在 php 中创建干净的 url 从 mysql 获取数据?
【发布时间】:2019-04-18 04:31:31
【问题描述】:

我想知道如何在php中直接动态从mysql中获取数据创建url?

假设我有一个名为“Products”的数据库表:

1) id
2) seller_id
3) Product Code
4) Product Image
5) Product Description
  • 假设其中添加了 5000 个或更多产品。
  • 假设我的域名是:www.xyz.com

现在我想要的是从产品表的卖家 ID 创建网址,例如 www.xyz.com/sellerid1

如果添加了新的卖家 ID,那么我将自动动态获取 url,并在该页面上从“产品”表中获取与卖家 ID 相关的所有相关产品

我想要这样的网址

www.xyz.com/sellerid1
www.xyz.com/sellerid2
www.xyz.com/sellerid3

我的文件名是我想从“产品”表中获取数据的地方:seller.php

我不想要这样的网址:

www.xyz.com/seller.php?id=sellerid1
www.xyz.com/seller.php?id=sellerid2
www.xyz.com/seller.php?id=sellerid3

我想如果有人直接在地址栏上点击网址,例如:www.xyz.com/sellerid1,那么与sellerid相关的所有产品都会显示。它可以通过 www.xyz.com/seller.php?id=sellerid1 实现这一点,但没有用户会记住那种类型的 url。我要 www.xyz.com/sellerid1

抱歉,两个问题合二为一,但我不知道如何动态实现。

欢迎任何想法或建议。

【问题讨论】:

  • 你可以使用 .htaccess 吗?检查此链接:stackoverflow.com/questions/16388959/url-rewriting-with-php
  • @Van Tho,感谢您的回复。是的,我可以使用 . htaccess 但它会从所有网址中删除 .php 吗?实际上这是我的第一个项目,所以我从不使用 .htaccess。以及如何从数据库创建 url 并显示所有相关产品这是我必须完成的第一个任务。
  • @Thomas Jeriko,就像您提出的问题一样,我也很困惑-问题是,我不明白如何将 $sellerid 从 url 动态传递给 php 本身。喜欢 www.xyz.com/sellerid1
  • @Sarah 为什么不用像 laravel 或 codeignitor 这样的框架?

标签: javascript php jquery mysql mysqli


【解决方案1】:

你们有卖家表吗? 如果是,那么您可以从表中获取卖家并动态制作链接。

查询:

SELECT id, name FROM tbl_sellers WHERE enabled=1 LIMIT 10;

html (index.php):

$result = []; // execute $query

// loop on result
foreach($result as $item)
    echo "<a href=\"www.xyz.com/seller.php?id=sellerid{$id}\">{$name}</a>";

seller.php:

// Getting request id from url parameters and cast to integer
    $sellerId = (int)str_replace('sellerid', '', isset($_REQUEST['id']) ? $_REQUEST['id'] : '');

    if(!$sellerId || $sellerId < 1){
        exit('Seller not found.');
    }


    $query = "SELECT * FROM tbl_products WHERE seller_id='{$sellerId}'";
    $result = []; // execute $query

    // fetch query
    exit(var_dump($result));

【讨论】:

  • @Ata amini,感谢您的宝贵回复。我知道并在上面的问题中提到过这个概念,我不想创建像 www.xyz.com/seller.php?id=sellerid1 这样的网址。我想如果有人点击这样的网址:www.xyz.com/sellerid1 那么所有与sellerid相关的产品都会显示。问题是,我不明白如何将 $sellerid 从 url 动态传递到 php 本身。
  • 使用这一行:$sellerId = (int)str_replace('sellerid', '', isset($_REQUEST['id']) ? $_REQUEST['id'] : '');
  • 从 url 获取参数可以使用:$_GET、$_REQUEST 和 $_POST,如果你的请求方法是 post。
【解决方案2】:

在这里使用核心 php。在您的 .htaccess 文件中添加以下行。假设您的默认文件是 index.php 这里。

RewriteEngine on

RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteRule ^seller/([^/]+)$ seller.php?sellerid=$1

现在您将尝试打开 URL http://www.yoururl.com/seller/{your_dynamic_seller_id_comes_here}。假设您正在打开 URL http://www.yoururl.com/seller/1,那么您将从以下代码 (seller.php) 中获取动态卖家 ID。

<?php
$sellerid = $_GET['sellerid'];
//get the result from database query 

$dbquery = "SELECT * FROM Products WHERE seller_id='$sellerid' ";
//execute the above query and use the result and do your stuff.

?>

希望对你有所帮助。

【讨论】:

    【解决方案3】:

    也许你可以使用 Laravel、Codeigniter 等 MVC 框架,或者其他使用 MVC 的框架。 MVC 框架,可以做你想要的,就像

    www.xyz.com/sellerid1
    

    或 你可以这样做,但链接不像

    www.xyz.com/sellerid1
    

    但喜欢

    www.xyz.com/sellerid/1
    

    index.php:

    <?php
    //get request url
    $request_uri = explode('/', $_SERVER['REQUEST_URI']);
    // print_r($request_uri);
    if($request_uri[2]=='sellerid'){
        include 'test.php';
    }
    ?>
    

    test.php:

    <?php  
        echo $request_uri[2].'/'.$request_uri[3];
    ?>
    

    .htaccess:

    RewriteEngine on
    RewriteCond $1 !^(index\.php|resources|robots\.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L,QSA]
    

    以及如何创建链接是这样的:

    <?php  
        $url="http://www.example.com/";
        $theproduct=array(...);//Fill with your sellerid
        foreach ($theproduct as $key) {
            ?>
                <a href="<?=$url.'sellerid/'.$key['seller_id']?>"></a>
            <?php
        }
    ?>
    

    【讨论】:

    • @Thomas Jeriko,感谢您的回复。不幸的是我没有使用它。没有框架。没有框架,这是不可能的吗?
    • 啊,我明白了。是的,您可以在没有框架的情况下做到这一点,请参阅此答案stackoverflow.com/questions/27570522/…
    • 我在 localhost 上测试过
    • @Thomas Jeriko,感谢 Thomas 的大力帮助。我想知道如果sellerid 是:best-book-seller-in-newyork 那么网址将是 www.xyz.com/best-book-seller-in-newyork
    • @Sarah 你可以做,比如 $request_uri =explode('/',str_replace('-', ' ', $_SERVER['REQUEST_URI']));
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多