Click here to Skip to main content
15,851,701 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this code in fliters.php:
<div class="boxed1 mb-3">
  <h5 class="text-center" style="padding-top: 15px;">Categories</h5>

    <?php if(substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1) == "all-products.php"){ ?>

    <h6 style="margin-left: 10px;">All Products</h6>

  <?php }else{ ?>

    <h6 style="margin-left: 10px;"><a href="all-products">All Products</a></h6>

  <?php }
      $sql1 = $db->prepare("SELECT * FROM categories WHERE id IN (SELECT parent FROM categories) AND parent = ? AND deleted = ?");
      $sql1->bind_param('ii', $var0, $var0);
      $result1 = $sql1->get_result();
      while($row1 = mysqli_fetch_assoc($result1)){

  <h6 style="margin-left: 10px;"><a href="main-category?cat=<?=$row1['id'];?>"><?=$row1['category'];?></a></h6>

  $sql2 = $db->prepare("SELECT * FROM categories WHERE parent IN (SELECT id FROM categories) AND parent = ? AND deleted = ?");
  $sql2->bind_param('ii', $row1['id'], $var0);
  $result2 = $sql2->get_result();
  while($row2 = mysqli_fetch_assoc($result2)){ ?>

    <h6 style="margin-left: 25px;"><a href="sub-category?cat=<?=$row2['id'];?>"><?=$row2['category'];?></a></h6>

  <?php }
      $sql3 = $db->prepare("SELECT categories.*
                            FROM categories
                            INNER JOIN
                            NOT IN (SELECT categories.parent FROM categories)
                            AND categories.parent = ?
                            AND categories.deleted = ?
                            AND products.category =");
      $sql3->bind_param('ii', $var0, $var0);
      $result3 = $sql3->get_result();
      while($row3 = mysqli_fetch_assoc($result3)){

      <h6 style="margin-left: 10px;"><a href="sub-category?cat=<?=$row3['id'];?>"><?=$row3['category'];?></a></h6>

    <?php } ?>


and this code in main-category.php:
  require_once 'init.php';
  include 'includes/head.php';
  include 'includes/navigation.php';

  $arr = array();
  $sql = $db->prepare("SELECT * FROM categories WHERE parent = ? AND deleted = ?");
  $sql->bind_param('ii', $_GET['cat'], $var0);
  $result = $sql->get_result();
  while($row = mysqli_fetch_assoc($result)){
  $arr[] = $row['id'];
  $test = implode(",", $arr);
  $array = array_map('intval', explode(',', $test));
  $array = implode("','", $array);
  $query = $db->query("SELECT * FROM products WHERE category IN ('$array') AND archived = 0 ORDER BY name");

  <!-- Main Content -->
  <div class="container-fluid" style="margin-top:45px;">
    <div class="row">
      <div class="col-lg-2">

        <?php include 'includes/filters.php'; ?>

      <div class="col-lg-1"></div>
      <div class="col-lg-8">

          $catSql = $db->prepare("SELECT * FROM categories WHERE id = ?");
          $catSql->bind_param('i', $_GET['cat']);
          $result7 = $catSql->get_result();
          $childs = mysqli_fetch_assoc($result7);
          $pSql = $db->prepare("SELECT * FROM categories WHERE id = ?");
          $pSql->bind_param('i', $childs['parent']);
          $result20 = $pSql->get_result();
          $parents = mysqli_fetch_assoc($result20);
          <h6 id="backcolor2">
            <ol class="breadcrumb justify-content-center">
              <li class="breadcrumb-item"><a href="index">Home</a></li>
              <li class="breadcrumb-item"><a href="all-products">Products</a></li>
            <li class="breadcrumb-item"><?=$childs['category'];?></li>
        <div class="row row-cols-1 row-cols-md-4 g-4">

          <?php while($row2 = mysqli_fetch_assoc($query)) : ?>

            <div class="col d-flex align-items-stretch">
              <div class="card">
                <img src="<?=explode('/', $_SERVER['PHP_SELF'])[1].'/'.$row2['image'];?>" class="card-img-top">
                <div class="card-body text-center">
                  <h6 class="card-title mb-3"><?=$row2['name'];?></h6>
                  <a href="products?details=<?=$row2['id'];?>" class="btn btn-sm btn-success button_hover stretched-link">Details</a>

          <?php endwhile; ?>


Now the main categories are listed in the filters
<h6 style="margin-left: 10px;"><a href="main-category?cat=<?=$row1['id'];?>"><?=$row1['category'];?></a></h6>

and when I press on one of them the url will appear like this:
I need to change the url to appear like this example:

What I have tried:

How can I do this? What should I edit in the code above?
Updated 17-Jun-23 2:31am

1 solution

What should I edit in the code above?

You have shown no code that might lead to a URL rename so there is nothing to edit. Normally I will not assist with no effort shown but luckily I had a code piece on this from before...

Make use of PHP's 'parse_url()' method to achieve the desired outcome - parse_url[^] as well as the 'http_build_query()' method - http_build_query[^]

The below code allow for any changes in your 'cat' returned values, so any value will be concatenated with during the url rebuild to output the correct page and values -
// Current URL
$currentURL = '';

// Remove query string
$parsedURL = parse_url($currentURL);
$baseUrl = $parsedURL['scheme'] . '://' . $parsedURL['host'] . $parsedURL['path'];

// Rename the URL
$newPath = '/hair-care-categories';
$newQuery = '';

// Get the query string parameters
if (isset($parsedURL['query'])) {
    parse_str($parsedURL['query'], $queryArray);

    // Remove the 'cat' parameter

    // Build the new query string
    $newQuery = http_build_query($queryArray);

// Combine the new path and query string
if ($newQuery) {
    $newURL = $baseUrl . $newPath . '?' . $newQuery;
} else {
    $newURL = $baseUrl . $newPath;

// Output the renamed URL
echo $newURL;

I ran this code in W3Schools PHP Editor[^] and the new URL returned fine.
Share this answer
FRS4002 17-Jun-23 8:04am    
hair-care-categories is just an example... hair-care-categories is dynamic and coming from the database so it could be other than hair-care-categories.
Andre Oosthuizen 17-Jun-23 11:54am    
Well, that's easy enough then -
$newPath = '/hair-care-categories'; //should become 
$newPath = '/' .$My_Value_returned_From_Database;
FRS4002 17-Jun-23 15:02pm    
Thanks for the clarification. But where should I put these codes? Is it in filters.php or in main-category.php? As I said before, this code:
<h6 style="margin-left: 10px;"><a href="main-category?cat=<?=$row1['id'];?>"><?=$row1['category'];?></a></h6>

is in filters.php that leads to main-category.php page... I should add/edit some code in this line above so it leads to what you said above...
Andre Oosthuizen 18-Jun-23 5:12am    
In filters.php. I suggest that you read up on the links that I have supplied, you are now just copying and pasting code without knowing how why we use it or what it's functions are. Once you understand what it does, you will also know where to use the code. By reading the code comments as well, you should be able to work out what each line is for and then where to use it.
FRS4002 18-Jun-23 8:59am    
Yeah, I understand the code and I applied the code in filters.php and it changed the URL. Now how to receive the new URL in main-category.php ? In other words, how main-category.php page will receive the new URL so it opens? Because, when I click on the link it says internal error.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900