Home > Net >  Is there any more efficient way to code my php $_GET multiple URL parameter?
Is there any more efficient way to code my php $_GET multiple URL parameter?

Time:09-21

Hi i want to make some kind of filtering feature, so in my example there are the brand of the car and the type of the car and if both are exist i want the brand url parameter always in the front of the type url parameter, here is my solution to it :

<div class='brandcontainer'>
<?php
if(!isset($_GET['brand']) && !isset($_GET['type'])){
    echo"
        <a href='market.php?brand=toyota'><div class='brand'>toyota</div>
        <a href='market.php?brand=honda'><div class='brand'>honda</div>
        <a href='market.php?brand=mercedes'><div class='brand'>mercedes</div>";
} if(!isset($_GET['brand']) && isset($_GET['type'])){
    echo"
        <a href='market.php?brand=toyota&type=$_GET[type]'><div class='brand'>toyota</div>
        <a href='market.php?brand=honda&type=$_GET[type]'><div class='brand'>honda</div>
        <a href='market.php?brand=mercedes&type=$_GET[type]'><div class='brand'>mercedes</div>";
} if(isset($_GET['brand']) && !isset($_GET['type'])){
    if($_GET['brand'] == "toyota"){
        echo"
        <a href='market.php?'><div class='brand'>toyota</div>
        <a href='market.php?brand=honda'><div class='brand'>honda</div>
        <a href='market.php?brand=mercedes'><div class='brand'>mercedes</div>";
        }
    else if($_GET['brand'] == "honda"){
        echo"
        <a href='market.php?brand=toyota'><div class='brand'>toyota</div>
        <a href='market.php'><div class='brand'>honda</div>
        <a href='market.php?brand=mercedes'><div class='brand'>mercedes</div>";
            }
   else if($_GET['brand'] == "mercedes"){
        echo"
        <a href='market.php?brand=toyota'><div class='brand'>toyota</div>
        <a href='market.php?brand=honda'><div class='brand'>honda</div>
        <a href='market.php'><div class='brand'>mercedes</div>";
                }
} if(isset($_GET['brand']) && isset($_GET['type'])){
    if($_GET['brand'] == "toyota"){
    echo"
        <a href='market.php?type=$_GET[type]'><div class='brand'>toyota</div>
        <a href='market.php?brand=honda&type=$_GET[type]'><div class='brand'>honda</div>
        <a href='market.php?brand=mercedes&type=$_GET[type]'><div class='brand'>mercedes</div>";
    }
    if($_GET['brand'] == "honda"){
        echo"
        <a href='market.php?brand=toyota&type=$_GET[type]'><div class='brand'>toyota</div>
        <a href='market.php?type=$_GET[type]'><div class='brand'>honda</div>
        <a href='market.php?brand=mercedes&type=$_GET[type]'><div class='brand'>mercedes</div>";
        }
    if($_GET['brand'] == "mercedes"){
        echo"
        <a href='market.php?brand=toyota&type=$_GET[type]'><div class='brand'>toyota</div>
        <a href='market.php?brand=honda&type=$_GET[type]'><div class='brand'>honda</div>
        <a href='market.php?type=$_GET[type]'><div class='brand'>mercedes</div>";
        }
    
}

?>
</div>
<div class='typecontainer'>
<?php
if(!isset($_GET['brand']) && !isset($_GET['type'])){
    echo"
        <a href='market.php?type=sedan'><div class='type'>sedan</div>
        <a href='market.php?type=suv'><div class='type'>suv</div>";
} if(!isset($_GET['brand']) && isset($_GET['type'])){
    if($_GET['type'] == "sedan"){
        echo"
        <a href='market.php?'><div class='type'>sedan</div>
        <a href='market.php?type=suv'><div class='type'>suv</div>";
        }
    else if($_GET['type'] == "suv"){
        echo"
        <a href='market.php?type=sedan'><div class='type'>sedan</div>
        <a href='market.php'><div class='type'>suv</div>";
            }
}if(isset($_GET['brand']) && !isset($_GET['type'])){
    echo"
        <a href='market.php?brand=$_GET[brand]&type=sedan'><div class='type'>sedan</div>
        <a href='market.php?brand=$_GET[brand]&type=suv'><div class='type'>suv</div>";
}if(isset($_GET['brand']) && isset($_GET['type'])){
    if($_GET['type'] == "sedan"){
    echo"
        <a href='market.php?brand=$_GET[brand]'><div class='type'>sedan</div>
        <a href='market.php?brand=$_GET[brand]&type=suv'><div class='type'>suv</div>";
    }
    else if($_GET['type'] = "suv"){
        echo"
        <a href='market.php?brand=$_GET[brand]&type=sedan'><div class='type'>sedan</div>
        <a href='market.php?brand=$_GET[brand]'><div class='type'>suv</div>";
        }
    
}

?>
</div>
  

Is there any other effiecient way to do it ? because if i want to add another filter, it will be a pain. Thank You !!!

CodePudding user response:

I have simplified your code by taking an array of brands. Same you can create a type array and implement it.

<div class='brandcontainer'>
<?php

$arrBrand = array(
    'toyota',
    'honda',
    'mercedes'
);


if(!isset($_GET['brand']) && !isset($_GET['type'])){
    foreach($arrBrand as $brand) {
        echo "<a href='market.php?brand='".$brand."><div class='brand'>".$brand."</div>";
    }
} if(!isset($_GET['brand']) && isset($_GET['type'])){

    foreach($arrBrand as $brand) {
        echo "<a href='market.php?brand='".$brand."&type=".$_GET['type']."><div class='brand'>".$brand."</div>";
    }
    
} if(isset($_GET['brand']) && !isset($_GET['type'])){

    foreach($arrBrand as $brand) {
        if($_GET['brand'] == $brand){
            echo "<a href='market.php?><div class='brand'>".$brand."</div>";
        } else {
            echo "<a href='market.php?brand='".$brand."><div class='brand'>".$brand."</div>";
        }
        
    }
} if(isset($_GET['brand']) && isset($_GET['type'])){


    foreach($arrBrand as $brand) {
         if($_GET['brand'] == $brand){
            echo "<a href='market.php?type=".$_GET['type']."><div class='brand'>".$brand."</div>";
        } else {
            echo "<a href='market.php?brand='".$brand."&type=".$_GET['type']."><div class='brand'>".$brand."</div>";
        }
        
    }

}

?>
</div>

CodePudding user response:

Instead of

if($_GET['brand'] == "toyota"){
        echo"
        <a href='market.php?'><div class='brand'>toyota</div>
        <a href='market.php?brand=honda'><div class='brand'>honda</div>
        <a href='market.php?brand=mercedes'><div class='brand'>mercedes</div>";
        }

repeated for each different brand, I'd probably do something like

$brands = ["toyota", "honda", "mercedes"];
foreach ($brands as $brand) { 
    $suffix = ($_GET['brand'] == $brand ? "" : "?brand=" . $brand);
    if (isset($_GET['type'])) $suffix .= ($suffix == "" ? "?" : "&") . "type=" . $_GET['type'];
    echo "<a href='market.php" . $suffix . "'><div class='brand'>" . $brand . "</div>";
 }

That would tidy up the first part quite nicely.

  • Related