• Hello Fabrik Community

    Fabrik is now in the hands of the development team that brought you Fabrik for Joomla 4. We have recently transitioned the Fabrik site over to a new server and are busy trying to clean it up. We have upgraded the site to Joomla 4 and are running the latest version of Fabrik 4. We have also upgraded the Xenforo forum software to the latest version. Many of the widgets you might have been used to on the forum are no longer operational, many abandoned by the developers. We hope to bring back some of the important ones as we have time.

    Exciting times to be sure.

    The Fabrik 4.0 Official release is now available. In addition, the Fabrik codebase is now available in a public repository. See the notices about these in the announcements section

    We wish to shout out a very big Thank You to all of you who have made donations. They have really helped. But we can always use more...wink..wink..

    Also a big Thank You to those of you who have been assisting others in the forum. This takes a very big burden off of us as we work on bugs, the website and the future of Fabrik.

Help for json_decode

rodeokid

Member
Hi, i have a checkbox element returning over 50 checkboxes for the user to check (it's a list for employees who received a training). So everything is fine, the element retrieve the employees list from database and display all names with checkbox in front of them.
The person who gives the training just check which employees had follow the training, and can come back later to add another employee just by checking his name.

My problem is that i would like to display on a page the name of every employees who had follow 1 particular training, so i need to retrieve the names from the database which are stored like this :

Code:
["John Test","Jack Test","Henry Test","Dennis Test"]

So i use a calc element with the following code:

Code:
$checkbox = '{liste_des_formations___employes}';
$checkboxvalue = json_decode($checkbox);
foreach(array_keys($checkboxvalue) as $key) {
    echo $checkboxvalue[$key]."<br />";
}

But my problem is that i have this as a result:

John Test
Jack Test
Henry Test
Dennis Test
John%20Test
Jack%20Test
Henry%20Test
Dennis%20Test

So can someone point me in the right direction cause i would like to learn the right syntax to only have the first 4 result... not the last four with the %20 that replace space... why do i get those ?
Thanks for your help !

rodeokid
 
I suspect it may be because the json_decode decodes by default into standard objects rather than an array. I am not sure why that would create this symptom, but it may be because array_key does something when it converts it to an array - or possibly something else.

However you could try:
PHP:
$checkboxvalue = json_decode($checkbox, true);
and see if it helps.

Alternatively, it may be that json_decode does not support this form of json which does not have labels.

You might want to install JDump and use it to dump the variables to see what they hold.
 
Last edited:
Ok did some test, no luck at all... still the same if i change to this:
Code:
$checkboxvalue=json_decode($checkbox,true);
jdump refuse to install, so i used vardump that gave me this:
Code:
string(123) "["John Test","Jack Test","Henry Test","Dennis Test"]"

So those are the result i want, but no matter what i try, it keeps adding four same result with %20 in the place of the space... don't know how to get rid of those double unwanted results...
Still looking for help if someone have an idea.
rodeokid
 
Hmmm - it could also be that you are echoing the output and then returning it somehow as well.

I haven't tested it but this is the code I would write:
PHP:
$checkbox = '{liste_des_formations___employes}';
$checkboxvalues = json_decode($checkbox, true);
$result = implode("<br />\r\n", $checkboxvalues);
return $result;
 
Alternatively, it may be that json_decode does not support this form of json which does not have labels.

Arrays without keys are perfectly legal in JSON, and supported by PHP's json_decode(). Arrays are surrounded with []. Decoding a JSON array without keys automatically creates sequential numeric indexes, starting at 0. Objects in JSON, surrounded by {}, must have keys.

(One little wrinkle, not relevant here but something to file away for future reference ... if you have a JSON array with non-sequential indexes, like ["0":"foo","2":"bar"], it will actually decode into an object, not an array)

When dealing with JSON in Fabrik, it's usually best to use our JSONtoData helper, as that avoids errors if the input is empty. And also handles that non-sequential "feature". And also (optionally) handles the special case of a single empty value in a array, [""], which it will return as an empty array, not an array with a single empty value - this works round a legacy issue with Fabrik dropdown/checkbox/radiobutton data.

Code:
$checkbox = FabrikWorker::JSONtoData('{liste_des_formations___employes}', true);

The second arg forces the result to be an array.

Then implode() the result, as per Sophist's example.

-- hugh
 
Ok so i tried with this:
Code:
$checkbox = FabrikWorker::JSONtoData('{liste_des_formations___employes}', true);
$result = implode("<br />\r\n", $checkbox);
return $result;
But i have the same exact result... 4 correct answer, and the 4 same answers with %20 between first and last name...
Any idea ?

rodeokid
 
Not really.

Although you could try accessing the $data array directly, rather than using a placeholder.

Code:
return implode("<br />\r\n", $data['liste_des_formations___employes']);

I'm relatively sure it should already be an array in $data.

-- hugh
 
Ok thanks Hugh... now this single line
Code:
return implode("<br />\r\n", $data['liste_des_formations___employes']);
works to have only one instance of every name... but i face a new problem, cause now accent are not take in charge... so the name Luc Bélisle display like this: Luc Bu00e9lisle
I believe it is a decode problem... but really don't know how to do it.
 
Ok i finaly found a solution that works, but it's a little bit weird...

This is what i use for my listcheck calc element:
Code:
$checkbox = json_decode($data['liste_des_formations___employes'], true);
$result = implode("<br />\r\n", $checkbox);
return $result;
The result of this code in the database was perfect, all the names of the employees were good with french accent... but when display on page, all french accent were replaced like this example: Luc Bu00e9lisle
So i just hide this calc element, and simply use a display element with this code:
Code:
return '{liste_des_formations___listcheck}'

So is the calc element unable to show french accent from json data ? I don't know, but the use of the display element was my only option.
This might help other french users looking for a solution to display data from a json encode database element.

rodeokid
 
When dealing with accented characters for display on a page, you have to convert them to something HTML can render, with htmlentities() or htmlspecialchars(). See the PHP manual pages for the differences between the two (google 'php htmlentities' should get you the manual page as the first result).

Code:
$checkbox = json_decode($data['liste_des_formations___employes'], true);
$result = implode("<br />\r\n", $checkbox);
return htmlentities($result, ENT_QUOTES, "UTF-8");

This assumes you are using UTF-8 encoding, which is most likely.

In the display element, we handle doing that for you. In the calc, we make less assumptions about the result, and just render it as-is. The reason for that is, if you are returning a string which is already encoded into HTML entities, it would get broken if we ran it through htmlentities() again (so for example, &amp; would get turned into &amp;amp;).

-- hugh
 
Ok i was pretty sure there was a reason for this cause the display element solved my problem magically... will try your proposition..lolll
Thanks !
rodeokid
 
Ok now... i know it's impossible to get a list of unchecked checkbox, because it only return the checked one in the list to display...but is it possible to use array_diff to compare the list of employees i generate for my checkbox element, to this new list of checked one...so it would give me the list of unchecked one ? Yes ? No ? Someone can point me in the right direction ? Please... cause now that i have a fine list of employees who did receive the training for a particular subject...it would be useful for the trainer to have access easily to a list of those who did not receive this training..so unchecked box... If i could have the original employees list in a array, and the checked box list in a array... i think there should be a way to compare those 2 list... but never did this before... Any code genius available ?
Thanks
rodeokid
 
When dealing with accented characters for display on a page, you have to convert them to something HTML can render, with htmlentities() or htmlspecialchars(). See the PHP manual pages for the differences between the two (google 'php htmlentities' should get you the manual page as the first result).

Code:
$checkbox = json_decode($data['liste_des_formations___employes'], true);
$result = implode("<br />\r\n", $checkbox);
return htmlentities($result, ENT_QUOTES, "UTF-8");

This assumes you are using UTF-8 encoding, which is most likely.

In the display element, we handle doing that for you. In the calc, we make less assumptions about the result, and just render it as-is. The reason for that is, if you are returning a string which is already encoded into HTML entities, it would get broken if we ran it through htmlentities() again (so for example, &amp; would get turned into &amp;amp;).

-- hugh

Ok tried your code, but no luck, with it i am back at this result:

John Test
Jack Test
Henry Test
Dennis Test
John%20Test
Jack%20Test
Henry%20Test
Dennis%20Test

So it gives me the right list but then add all names with %20 instead of space between first and last name. Anyway, the display element solution is fine for me...if i could just find a way to use array_diff on my original list and the checked list it now gives me... it would gives me the unchecked list i am looking for.
The whole project is to be able for trainers to have a fast access to the list of employees who got trained for 1 subject, and be able to print of PDf list of trained and untrained employees... so you choose the subject in one list, and it gives you the whole employees list with checkbox in front of each name (working), and you have a tab with all the employees who got trained (working), and another tab with the employees that are not yet trained for this subject (this is what i am looking to achieve)... so if we can't have a list of unchecked box, i thought it is maybe possible to compare 2 array -> the original list of all employees and the checked box list -> this should give me the difference, so unchecked box... but i am stuck
Thanks
rodeokid
 
Just adding the htmlentities() on the return wouldn't have added those extra entries, so you must have changed something else.

I'd be happy to help further if you'd like to take out a sub.

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top