简体   繁体   中英

How to pass array parameters in url - PHP

I'm tryng to pass parameters in the url, but I receive this error: *implode(): Invalid arguments passed in *

I know that the cause is that I pass a string as parameter instead of an array, but how can I do to pass the array directly?

My code:

$all_prod_cat="SELECT * FROM products GROUP BY product_category";
$run_all_prod_cat = mysqli_query($con,$all_prod_cat);

$sql= "SELECT * FROM products"; 

if(isset($_GET['product_category']) && $_GET['product_category']!="") 
    {
        $risperpag = 16;            
        $limit = $risperpag * $_GET['p'] - $risperpag;

        $prod_cat = $_GET['product_category'];
        $sql.=",categories WHERE product_category IN ('".implode("','",$prod_cat)."') AND product_category = cat_id LIMIT $limit,$risperpag";

        $get_cat_pro_total = "SELECT * FROM products,categories WHERE product_category IN ('".implode("','",$prod_cat)."') AND product_category = cat_id";      


$run_cat_pro_total = mysqli_query($con,$get_cat_pro_total); 
$num_cat_pro_total = mysqli_num_rows($run_cat_pro_total);

$npag = ceil($num_cat_pro_total / $risperpag);

$p = $_GET['p'];    

echo "<ul>";

    if($p!=1)
    {           
        echo '<li class="numPagine"><a href="lista_prodotti.php?sort_price=&product_category='.urlencode($stringa).'&p='.($p-1).'">← Indietro</a></li>';
    }

     for($i=1;$i<=$npag;$i++){

          if($p==$i)
          {

            echo '<li class="numPagine pagina_attuale">'.$i.'</li>';
          }
          else
          {               
            echo '<li class="numPagine"><a href="lista_prodotti.php?sort_price=&product_category='.urlencode($stringa).'&p='.$i.'">'.$i.'</a></li>';
          }

    }               

    if($p!=$npag)
    {           
        echo '<li class="numPagine"><a href="lista_prodotti.php?sort_price=&product_category='.urlencode($stringa).'&p='.($p+1).'">Avanti →</a></li>';                      

    }           

echo "</ul>";           

    }       

The problem is when I set the urlencode parameter, I tried to serialize, but I get an other error. I think I should unserialize, but I don't know the exact point.

Thanks in advance.

It is possible to generate url with get params using http_build_query function.

$urlparams = [
    'product_category' => [
         'cat1',
         'cat2',
         'cat3',
         'cat4'
    ]
];

echo http_build_query($urlparams);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM