1

I want to know how to create url directly dynamically fetching data from mysql in php?

Let assume I have a database table named "Products":

1) id
2) seller_id
3) Product Code
4) Product Image
5) Product Description
  • Let assume there are 5000 products added in it or more.
  • Let assume my domain name is: www.xyz.com

Now what I want is to create urls from product table's seller ids like www.xyz.com/sellerid1

If new seller id added then I will get url dynamically automatically and on that page get all related products related to seller_id from table "Products"

I want url like

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

My File Name is where I want to fecth data from "Products" table: seller.php

I dont want url like:

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

I want if someone hit the url directly on addressbar like: www.xyz.com/sellerid1 then all the products related to sellerid will show. It can achieve this by www.xyz.com/seller.php?id=sellerid1 but no user will remember that type of url. I want www.xyz.com/sellerid1

Sorry, two questions in one but I have no idea how to achive this dynamically.

Any idea or suggestions would be welcome.

Sarah
  • 405
  • 2
  • 7
  • 23
  • Can you use .htaccess? Check this link: https://stackoverflow.com/questions/16388959/url-rewriting-with-php – Van Tho Nov 15 '18 at 06:00
  • @ Van Tho , Thanks for the reply. Yes, I can use . htaccess but wiil it remove .php from all urls? Actually This is my first project so I never use .htaccess. And how to create url from database and show all related products this is the first task I have to finish. – Sarah Nov 15 '18 at 06:09
  • Possible duplicate of [How can I create a dynamic URL in php?](https://stackoverflow.com/questions/27570522/how-can-i-create-a-dynamic-url-in-php) – Thomas Jeriko Nov 15 '18 at 06:15
  • @Thomas Jeriko, exactly like the question you have suggested I am also confused - The thing is, I don't understand how I can pass the $sellerid from the url to the php itself dynamically. Like www.xyz.com/sellerid1 – Sarah Nov 15 '18 at 06:17
  • 1
    @Sarah Why don't you use a framework like laravel or codeignitor? – localroot Nov 15 '18 at 06:30
  • @ Jestin Sebastian, I am new and I have given semi developed project with no framework. – Sarah Nov 15 '18 at 06:34

3 Answers3

2

Do you have sellers table? if yes, so you can fetch sellers from table and make link dynamically.

query:

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
  • 39
  • 1
  • 8
  • @ Ata amini, Thanks for the valuable reply. This concept I know and have mentioned in my question above, I don't want to create urls like www.xyz.com/seller.php?id=sellerid1. I want if someone hit the url like: www.xyz.com/sellerid1 then all the products related to sellerid will show. The thing is, I don't understand how I can pass the $sellerid from the url to the php itself dynamically. – Sarah Nov 15 '18 at 06:21
  • use this line: `$sellerId = (int)str_replace('sellerid', '', isset($_REQUEST['id']) ? $_REQUEST['id'] : '');` – Ata amini Nov 15 '18 at 06:24
  • Getting parameters from url can done with: $_GET, $_REQUEST and $_POST if your request method is post. – Ata amini Nov 15 '18 at 06:26
0

Maybe you can use MVC framework like Laravel,Codeigniter, or other framework use MVC. MVC Framework , can do, exactly you want, like

www.xyz.com/sellerid1

or you can do like this but the link not form like

www.xyz.com/sellerid1

but like

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]

And how to create link is like :

<?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
  • 213
  • 3
  • 13
0

Using core php here. add the below lines in your .htaccess file. Assuming your default file is index.php here.

RewriteEngine on

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

Now when you will try to open the URL http://www.yoururl.com/seller/{your_dynamic_seller_id_comes_here}. suppose you are opening the URL http://www.yoururl.com/seller/1 then you will get the dynamic seller id from the below code (seller.php).

<?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.

?>

Hope this helps you.