0

I am trying to create my own custom breadcrumb, to do this I have to get the query string using PHP then pass it to my jQuery. So far I have been able to get the query string keys/values in a foreach loop then place them inside an input, which I can then get the values using jQuery but, my issue is that if I have multiple values for a single key like so;

?filter_style=bevelled-glass&filter_colour=amber,clear 

What I retrieve is both values as one, but I need to be able to separate them. Here is my code and what I am retrieving;

Code;

<?php 
    foreach($_GET as $key => $value){
        echo '<input class="' . $key . '" type="text" value="' . $value . '" style="display: none;">';
    }
?>

output;

<input class="filter_style" type="text" value="bevelled-glass" style="display: none;">
<input class="filter_colour" type="text" value="amber,clear" style="display: none;">

What I want;

<input class="filter_style" type="text" value="bevelled-glass" style="display: none;">
<input class="filter_colour" type="text" value="amber" style="display: none;">
<input class="filter_colour" type="text" value="clear" style="display: none;">

Also is this the best way to pass this data to my jQuery or can I create some sort oJSONon object?

3 Answers 3

3

You can explode each value to get an array of items from them and then loop and print.

<?php 
    foreach($_GET as $key => $value){
        $subarray = explode(",",$value);
        foreach($subarray as $subvalue)
        {
             echo '<input class="' . $key . '" type="text" value="' . $subvalue . '" style="display: none;">';
        }
    }
?>
Sign up to request clarification or add additional context in comments.

Comments

1

I don't know a ton about PHP, but from what I know about GET requests, you should be able to get the amber,clear as an array and break it out from there. In pseudocode,

foreach ($_GET as $key => $value) {
    if ($value is array) { 
        foreach ($val in $value) {
            <input class="' . $key . '" value="' . $val . '">
        }
    else {
        <input class="' . $key . '" value="' . $value . '">
    }
}

I would be very surprised if that code ran as written, but that's what I do to get through this problem when developing in other languages. Also, in my opinion, it's always better to use JSON wherever you can.

3 Comments

if ($value is array) { this condition will never pass since he doesn't pass an array, he pass a string with comma's
yeah, I think the Sujit's answer is the right way in exact code
Yes, mine also tested and work fine, but downvoted, since always there's someone somewhere with the wrong privileges at the hand...
0

Try something like that's

foreach($_GET as $key => $value){

if(count(explode(',',$value))){

foreach(explode(',',$value) as $subvalue){

echo '<input class="' . $key . '" type="text" value="' . $subvalue . '" style="display: none;">';

}

}else{
echo '<input class="' . $key . '" type="text" value="' . $value . '" style="display: none;">';
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.