Doubts about the $_GET variable

Asked

Viewed 66 times

0

I have this research form

inserir a descrição da imagem aqui And I’m getting her fee for the $_GET on this route

$app->get("/search", function()
{
	$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
	
	$pagination = Product::getPageSearchSite($_GET, $page);

	$pages = [];
	
	for ($x = 0; $x < $pagination['pages']; $x++)
	{
		array_push($pages, [
			'link'=>'/search?'. http_build_query([
			'page'=>$x+1,
			'search'=>$_GET
			]),
			'text'=>$x+1
		]);
	}

		$page = new Page();		
	
		$page->setTpl("search", array(
			"products"=>$pagination['data'],
			"search" =>$_GET,
			"pages"=>$pages

		)); 		
});

That sends the variable to the getPageSearch method, where it performs the pagination and takes the values of the search..

	public static function getPageSearchSite($value, $page = 1, $itemsPerPage = 3)
	{	
		$idproduct = $value["searchbycode"];
		$vlobject = $value["searchbyobject"];
		$vltype = $value["searchbytype"];
		$vlcategory = $value["searchbycategory"];
		$vlbedroom = $value["searchbybedroom"];
		$idcity = $value["searchbycity"];
		$vlprice = $value["searchbyprice"];

		$start = ($page - 1) * $itemsPerPage;
		$sql = new Sql();

		$query = "SELECT SQL_CALC_FOUND_ROWS * FROM tb_products a INNER JOIN tb_photoproducts b USING (idphoto) INNER JOIN tb_cities c USING (idcity) WHERE 1 = 1 ";
			
		if($value["searchbycode"] != ""){		
			$sql->select($query .= " AND idproduct = ".  $idproduct);
		}
		if($value["searchbyobject"] != ""){		
			$sql->select($query .= " AND vlobject = '" . $vlobject . "'");
		}
		if($value["searchbytype"] != ""){		
			$sql->select($query .= " AND vltype = '" . $vltype . "'");
		}
		if($value["searchbycategory"] != ""){		
			$sql->select($query .= " AND vlcategory = '" . $vlcategory . "'");
		}
		if($value["searchbybedroom"] != ""){		
			$sql->select($query .= " AND vlbedroom = '" . $vlbedroom . "'");
		}
		if($value["searchbycity"] != ""){		
			$sql->select($query .= " AND idcity = '" . $idcity . "'");
		}	
		if($value["searchbyprice"] != ""){
			
			if($value["searchbyprice"] == 1){
				$sql->select($query .= " AND vlprice >= 0 AND vlprice <= 100000 ");
			}
			else if($value["searchbyprice"] == 2){
				$sql->select($query .= " AND vlprice >= 100000 AND vlprice <= 200000 ");
			}
			else if($value["searchbyprice"] == 3){
				$sql->select($query .= " AND vlprice >= 200000 AND vlprice <= 400000 ");
			}
			else if($value["searchbyprice"] == 4){
				$sql->select($query .= " AND vlprice >= 400000 AND vlprice <= 600000 ");
			}
			else if($value["searchbyprice"] == 5){
				$sql->select($query .= " AND vlprice >= 600000 AND vlprice <= 800000 ");
			}
			else if($value["searchbyprice"] == 6){
				$sql->select($query .= " AND vlprice >= 800000 AND vlprice <= 1000000 ");
			}
			else if($value["searchbyprice"] == 7){
				$sql->select($query .= " AND vlprice >= 1000000 AND vlprice <= 1400000 ");
			}
			else if($value["searchbyprice"] == 8){
				$sql->select($query .= " AND vlprice >= 1400000 AND vlprice <= 1600000 ");
			}
			else if($value["searchbyprice"] == 9){
				$sql->select($query .= " AND vlprice >= 1600000 AND vlprice <= 1800000 ");
			}
			else if($value["searchbyprice"] == 10){
				$sql->select($query .= " AND vlprice >= 1800000 AND vlprice <= 2000000 ");
			}
			else if($value["searchbyprice"] == 11){
				$sql->select($query .= " AND vlprice >= 2000000 ");
			}
		}	
		
		$results = $sql->select($query .= " LIMIT $start, $itemsPerPage;");
		$resultTotal = $sql->select("SELECT FOUND_ROWS() AS nrtotal;");
		return [
			'data'=>$results, //todos resultados da consulta
			'total'=>(int)$resultTotal[0]["nrtotal"], //numero totol de registros
			'pages'=>ceil($resultTotal[0]["nrtotal"] / $itemsPerPage) //tot de pag*ceial arredonda pra cima
		];
	}

The problem is that in the first search it works, the $value variable it receives is in array format and it fills in the variables there, but the second time when it clicks to go to some page and it sends $_GET to getPageSearchSite() the value received is in URL format and it can’t catch the variables, I can’t figure out why this is happening.

  • 1

    But why are you passing $_GET as a method parameter ? no need

  • But what alternative do I have? I’m sending because I need to search the bank for the values received from the search field that are in $_Get.

  • 1

    You don’t need to pass it as a method parameter, just access the $_GET from inside, and it will bring the results correctly

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.