mysql - Category and subcategory php

Hello i am trying to fix a thing as a beginner i am still learningphp
untill i consider i can migrate to a framework, what i am trying to do is:
I have a router in php like this :
<?php if(isset($_GET['contact'])){ include('conctact.php'); }elseif(isset($_GET['name_product'])){ include('product.php'); }else{ include('home.php'); } ?>
Links for menu like this:
<?php $select = mysqli_query($conn, "SELECT * FROM products"); while($row = mysqli_fetch_assoc($select)){?> <div><a href="shoes/<?=$row['url_slug'];?>"><?=$row['name']?> - <?=$row['url_slug'];?></a></div> <?php } ?>
(Shoes is the category i want to add later dynamic category, on click i want to load page product.php, i dont want to have id on url so i did with url slug)
There is the.htaccess
RewriteRule ^([a-zA-Z0-9-])/([a-zA-Z0-9-])$ index.php?name_category=$name_product=$1 [L]
My url would like to look like thiswww.example/shoes/product-name
And theproduct.php
looks like this:
<?php
$name_product = mysqli_real_escape_string($conn, $_GET['name_product']);
$sql=mysqli_query($conn,"SELECT * FROM products WHERE url_slug='$name_product '");
$row=mysqli_fetch_array($sql);
?>
My table mysql looks like that:
Product table
|id|name|url_slug|category|
Category table
|id|name
I don't know how to write that.htaccess
when click on link to include pageproduct.php
and show details of product fetch from database.
Answer
Solution:
RewriteRule ^/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)$ /index.php?name_category=$1&name_product=$2 [L]
Each set of brackets in your regex will get an own$
variable.
This will match this exact urlwww.example/shoes/product-name
like this:
$1
=shoes
$2
=product-name
So if yoururl_slug
in your table isproduct-name
you can use this to query your sql
SELECT * FROM products WHERE url_slug='$name_product'
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: mysqli::real_connect(): (hy000/2002): connection refused
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.