Reputation: 2701
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?
Upvotes: 0
Views: 59
Reputation: 899
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.
Upvotes: 1
Reputation: 5202
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;">';
}
}
Upvotes: 0
Reputation: 12508
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;">';
}
}
?>
Upvotes: 3