簡體   English   中英

如何在php中創建干凈的url從mysql中獲取數據?

[英]How to create clean url fetching data from mysql in php?

我想知道如何在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

抱歉,兩個問題合二為一,但我不知道如何動態實現這一目標。

歡迎任何想法或建議。

你有賣家桌嗎? 如果是,那么您可以從表中獲取賣家並動態鏈接。

詢問:

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>";

賣家.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));

也許你可以使用像 Laravel、Codeigniter 這樣的 MVC 框架,或者其他使用 MVC 的框架。 MVC 框架,可以做,正是你想要的,比如

www.xyz.com/sellerid1

或者你可以這樣做,但鏈接不是這樣的

www.xyz.com/sellerid1

但喜歡

www.xyz.com/sellerid/1

索引.php :

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

測試.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
    }
?>

在這里使用核心 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.

?>

希望這對你有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM