How to send multiple parameters in echo based on isset
I have pagination code that is working perfect when filters are not set. However when i use filters on my page it displays every product but unlimited number of pages are shown with nothing on it after products are done. I checked if it would work if i send filter like :
if(isset($brand)){
echo " <a href='{$_SERVER['PHP_SELF'}}?currentpage=$nextpage&brand=$brand'>></a> "
}else{
echo " <a href='{$_SERVER['PHP_SELF'}}?currentpage=$nextpage'>></a>
}
but this is not efficient way to do this because i will have to check for 20 filter. Also I tried with string adding and adding it later to <a> but it is not working like:
$filters;
if(isset($brand)){
$filters .= "&brand=".$brand;
}
So my question is: Is there a way to check in echo if filters are set and send them all to next page(like: brand, color, size..).
php
add a comment |
I have pagination code that is working perfect when filters are not set. However when i use filters on my page it displays every product but unlimited number of pages are shown with nothing on it after products are done. I checked if it would work if i send filter like :
if(isset($brand)){
echo " <a href='{$_SERVER['PHP_SELF'}}?currentpage=$nextpage&brand=$brand'>></a> "
}else{
echo " <a href='{$_SERVER['PHP_SELF'}}?currentpage=$nextpage'>></a>
}
but this is not efficient way to do this because i will have to check for 20 filter. Also I tried with string adding and adding it later to <a> but it is not working like:
$filters;
if(isset($brand)){
$filters .= "&brand=".$brand;
}
So my question is: Is there a way to check in echo if filters are set and send them all to next page(like: brand, color, size..).
php
add a comment |
I have pagination code that is working perfect when filters are not set. However when i use filters on my page it displays every product but unlimited number of pages are shown with nothing on it after products are done. I checked if it would work if i send filter like :
if(isset($brand)){
echo " <a href='{$_SERVER['PHP_SELF'}}?currentpage=$nextpage&brand=$brand'>></a> "
}else{
echo " <a href='{$_SERVER['PHP_SELF'}}?currentpage=$nextpage'>></a>
}
but this is not efficient way to do this because i will have to check for 20 filter. Also I tried with string adding and adding it later to <a> but it is not working like:
$filters;
if(isset($brand)){
$filters .= "&brand=".$brand;
}
So my question is: Is there a way to check in echo if filters are set and send them all to next page(like: brand, color, size..).
php
I have pagination code that is working perfect when filters are not set. However when i use filters on my page it displays every product but unlimited number of pages are shown with nothing on it after products are done. I checked if it would work if i send filter like :
if(isset($brand)){
echo " <a href='{$_SERVER['PHP_SELF'}}?currentpage=$nextpage&brand=$brand'>></a> "
}else{
echo " <a href='{$_SERVER['PHP_SELF'}}?currentpage=$nextpage'>></a>
}
but this is not efficient way to do this because i will have to check for 20 filter. Also I tried with string adding and adding it later to <a> but it is not working like:
$filters;
if(isset($brand)){
$filters .= "&brand=".$brand;
}
So my question is: Is there a way to check in echo if filters are set and send them all to next page(like: brand, color, size..).
php
php
edited Nov 21 '18 at 4:31
miken32
24k94973
24k94973
asked Nov 21 '18 at 4:24
minionminion
866
866
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Use http_build_query to build an HTTP query string from an array of values. When building your array, use the null coalescing operator to avoid notices about using undefined variables.
$filter_array = [
"currentpage" => $nextpage,
"brand" => $brand ?? "",
"color" => $color ?? "",
"size" => $size ?? "",
// and the rest of your variables
];
$query = http_build_query($filter_array);
echo sprintf('<a href="%s?%s">></a>', $_SERVER["PHP_SELF"], $query);
Or, for old unsupported PHP versions:
$filter_array = [
"currentpage" => $nextpage,
"brand" => isset($brand) ? $brand : "",
"color" => isset($color) ? $color : "",
"size" => isset($size) ? $size : "",
// and the rest of your variables
];
$query = http_build_query($filter_array);
echo sprintf('<a href="%s?%s">></a>', $_SERVER["PHP_SELF"], $query);
Oh, I never heard about this what should i put there as it say unexpected ?
– minion
Nov 21 '18 at 4:37
Are you using an old version of PHP? This has been there since 7.0 released three years ago.
– miken32
Nov 21 '18 at 4:37
I am using 7.2.8 version
– minion
Nov 21 '18 at 4:44
Then you would not get an error about an unexpected?.
– miken32
Nov 21 '18 at 4:44
It is working! Thank you man so much, not only did u help me but I learned something great tonight !
– minion
Nov 21 '18 at 4:48
|
show 1 more comment
Here is a way to do it
add all the filters into an array first
$filters = array();
$filters['brand'] = $brand;
$filters['color'] = $color;
$filters['size'] = $size;
loop through the filters array to make params string
$anchor_tag_params = "";
foreach($filters as $key => $value)
{
$anchor_tag_params .= "&".$key."=".$value;
}
and then just append the params string to the anchor tag href
$anchor_tag_html = '<a href="'.$_SERVER['PHP_SELF'].'?currentpage='.$nextpage.$anchor_tag_params.'"></a>';
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405243%2fhow-to-send-multiple-parameters-in-echo-based-on-isset%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use http_build_query to build an HTTP query string from an array of values. When building your array, use the null coalescing operator to avoid notices about using undefined variables.
$filter_array = [
"currentpage" => $nextpage,
"brand" => $brand ?? "",
"color" => $color ?? "",
"size" => $size ?? "",
// and the rest of your variables
];
$query = http_build_query($filter_array);
echo sprintf('<a href="%s?%s">></a>', $_SERVER["PHP_SELF"], $query);
Or, for old unsupported PHP versions:
$filter_array = [
"currentpage" => $nextpage,
"brand" => isset($brand) ? $brand : "",
"color" => isset($color) ? $color : "",
"size" => isset($size) ? $size : "",
// and the rest of your variables
];
$query = http_build_query($filter_array);
echo sprintf('<a href="%s?%s">></a>', $_SERVER["PHP_SELF"], $query);
Oh, I never heard about this what should i put there as it say unexpected ?
– minion
Nov 21 '18 at 4:37
Are you using an old version of PHP? This has been there since 7.0 released three years ago.
– miken32
Nov 21 '18 at 4:37
I am using 7.2.8 version
– minion
Nov 21 '18 at 4:44
Then you would not get an error about an unexpected?.
– miken32
Nov 21 '18 at 4:44
It is working! Thank you man so much, not only did u help me but I learned something great tonight !
– minion
Nov 21 '18 at 4:48
|
show 1 more comment
Use http_build_query to build an HTTP query string from an array of values. When building your array, use the null coalescing operator to avoid notices about using undefined variables.
$filter_array = [
"currentpage" => $nextpage,
"brand" => $brand ?? "",
"color" => $color ?? "",
"size" => $size ?? "",
// and the rest of your variables
];
$query = http_build_query($filter_array);
echo sprintf('<a href="%s?%s">></a>', $_SERVER["PHP_SELF"], $query);
Or, for old unsupported PHP versions:
$filter_array = [
"currentpage" => $nextpage,
"brand" => isset($brand) ? $brand : "",
"color" => isset($color) ? $color : "",
"size" => isset($size) ? $size : "",
// and the rest of your variables
];
$query = http_build_query($filter_array);
echo sprintf('<a href="%s?%s">></a>', $_SERVER["PHP_SELF"], $query);
Oh, I never heard about this what should i put there as it say unexpected ?
– minion
Nov 21 '18 at 4:37
Are you using an old version of PHP? This has been there since 7.0 released three years ago.
– miken32
Nov 21 '18 at 4:37
I am using 7.2.8 version
– minion
Nov 21 '18 at 4:44
Then you would not get an error about an unexpected?.
– miken32
Nov 21 '18 at 4:44
It is working! Thank you man so much, not only did u help me but I learned something great tonight !
– minion
Nov 21 '18 at 4:48
|
show 1 more comment
Use http_build_query to build an HTTP query string from an array of values. When building your array, use the null coalescing operator to avoid notices about using undefined variables.
$filter_array = [
"currentpage" => $nextpage,
"brand" => $brand ?? "",
"color" => $color ?? "",
"size" => $size ?? "",
// and the rest of your variables
];
$query = http_build_query($filter_array);
echo sprintf('<a href="%s?%s">></a>', $_SERVER["PHP_SELF"], $query);
Or, for old unsupported PHP versions:
$filter_array = [
"currentpage" => $nextpage,
"brand" => isset($brand) ? $brand : "",
"color" => isset($color) ? $color : "",
"size" => isset($size) ? $size : "",
// and the rest of your variables
];
$query = http_build_query($filter_array);
echo sprintf('<a href="%s?%s">></a>', $_SERVER["PHP_SELF"], $query);
Use http_build_query to build an HTTP query string from an array of values. When building your array, use the null coalescing operator to avoid notices about using undefined variables.
$filter_array = [
"currentpage" => $nextpage,
"brand" => $brand ?? "",
"color" => $color ?? "",
"size" => $size ?? "",
// and the rest of your variables
];
$query = http_build_query($filter_array);
echo sprintf('<a href="%s?%s">></a>', $_SERVER["PHP_SELF"], $query);
Or, for old unsupported PHP versions:
$filter_array = [
"currentpage" => $nextpage,
"brand" => isset($brand) ? $brand : "",
"color" => isset($color) ? $color : "",
"size" => isset($size) ? $size : "",
// and the rest of your variables
];
$query = http_build_query($filter_array);
echo sprintf('<a href="%s?%s">></a>', $_SERVER["PHP_SELF"], $query);
edited Nov 21 '18 at 4:41
answered Nov 21 '18 at 4:30
miken32miken32
24k94973
24k94973
Oh, I never heard about this what should i put there as it say unexpected ?
– minion
Nov 21 '18 at 4:37
Are you using an old version of PHP? This has been there since 7.0 released three years ago.
– miken32
Nov 21 '18 at 4:37
I am using 7.2.8 version
– minion
Nov 21 '18 at 4:44
Then you would not get an error about an unexpected?.
– miken32
Nov 21 '18 at 4:44
It is working! Thank you man so much, not only did u help me but I learned something great tonight !
– minion
Nov 21 '18 at 4:48
|
show 1 more comment
Oh, I never heard about this what should i put there as it say unexpected ?
– minion
Nov 21 '18 at 4:37
Are you using an old version of PHP? This has been there since 7.0 released three years ago.
– miken32
Nov 21 '18 at 4:37
I am using 7.2.8 version
– minion
Nov 21 '18 at 4:44
Then you would not get an error about an unexpected?.
– miken32
Nov 21 '18 at 4:44
It is working! Thank you man so much, not only did u help me but I learned something great tonight !
– minion
Nov 21 '18 at 4:48
Oh, I never heard about this what should i put there as it say unexpected ?
– minion
Nov 21 '18 at 4:37
Oh, I never heard about this what should i put there as it say unexpected ?
– minion
Nov 21 '18 at 4:37
Are you using an old version of PHP? This has been there since 7.0 released three years ago.
– miken32
Nov 21 '18 at 4:37
Are you using an old version of PHP? This has been there since 7.0 released three years ago.
– miken32
Nov 21 '18 at 4:37
I am using 7.2.8 version
– minion
Nov 21 '18 at 4:44
I am using 7.2.8 version
– minion
Nov 21 '18 at 4:44
Then you would not get an error about an unexpected
?.– miken32
Nov 21 '18 at 4:44
Then you would not get an error about an unexpected
?.– miken32
Nov 21 '18 at 4:44
It is working! Thank you man so much, not only did u help me but I learned something great tonight !
– minion
Nov 21 '18 at 4:48
It is working! Thank you man so much, not only did u help me but I learned something great tonight !
– minion
Nov 21 '18 at 4:48
|
show 1 more comment
Here is a way to do it
add all the filters into an array first
$filters = array();
$filters['brand'] = $brand;
$filters['color'] = $color;
$filters['size'] = $size;
loop through the filters array to make params string
$anchor_tag_params = "";
foreach($filters as $key => $value)
{
$anchor_tag_params .= "&".$key."=".$value;
}
and then just append the params string to the anchor tag href
$anchor_tag_html = '<a href="'.$_SERVER['PHP_SELF'].'?currentpage='.$nextpage.$anchor_tag_params.'"></a>';
add a comment |
Here is a way to do it
add all the filters into an array first
$filters = array();
$filters['brand'] = $brand;
$filters['color'] = $color;
$filters['size'] = $size;
loop through the filters array to make params string
$anchor_tag_params = "";
foreach($filters as $key => $value)
{
$anchor_tag_params .= "&".$key."=".$value;
}
and then just append the params string to the anchor tag href
$anchor_tag_html = '<a href="'.$_SERVER['PHP_SELF'].'?currentpage='.$nextpage.$anchor_tag_params.'"></a>';
add a comment |
Here is a way to do it
add all the filters into an array first
$filters = array();
$filters['brand'] = $brand;
$filters['color'] = $color;
$filters['size'] = $size;
loop through the filters array to make params string
$anchor_tag_params = "";
foreach($filters as $key => $value)
{
$anchor_tag_params .= "&".$key."=".$value;
}
and then just append the params string to the anchor tag href
$anchor_tag_html = '<a href="'.$_SERVER['PHP_SELF'].'?currentpage='.$nextpage.$anchor_tag_params.'"></a>';
Here is a way to do it
add all the filters into an array first
$filters = array();
$filters['brand'] = $brand;
$filters['color'] = $color;
$filters['size'] = $size;
loop through the filters array to make params string
$anchor_tag_params = "";
foreach($filters as $key => $value)
{
$anchor_tag_params .= "&".$key."=".$value;
}
and then just append the params string to the anchor tag href
$anchor_tag_html = '<a href="'.$_SERVER['PHP_SELF'].'?currentpage='.$nextpage.$anchor_tag_params.'"></a>';
answered Nov 21 '18 at 5:25
Kinjal PathakKinjal Pathak
1245
1245
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405243%2fhow-to-send-multiple-parameters-in-echo-based-on-isset%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown