1. NOTICE: If you are using Fabrik and update to Joomla 3.10, you will need to update to Fabrik 3.10. And, if you are using Fabrik, do not upgrade to Joomla 4, we do not have a supported version ready for release. More information on a release date coming soon. Also, please note that Fabrik 3.10 will not install on any Joomla sites less than 3.8.
    Dismiss Notice

Help for json_decode

Discussion in 'Community' started by rodeokid, Jul 4, 2018.

  1. rodeokid

    rodeokid Member

    Level: Community
    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 (Text):
    ["John Test","Jack Test","Henry Test","Dennis Test"]
    So i use a calc element with the following code:

    Code (Text):
    $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
     
  2. Sophist

    Sophist Well-Known Member

    Level: Community
    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: Jul 4, 2018
  3. rodeokid

    rodeokid Member

    Level: Community
    Ok did some test, no luck at all... still the same if i change to this:
    Code (Text):
    $checkboxvalue=json_decode($checkbox,true);
    jdump refuse to install, so i used vardump that gave me this:
    Code (Text):
    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
     
  4. Sophist

    Sophist Well-Known Member

    Level: Community
    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;
     
  5. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    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 (Text):
    $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
     
  6. Sophist

    Sophist Well-Known Member

    Level: Community
    Thanks Hugh - wasn't aware of the worker method.
     
  7. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    There's also a FabrikWorker::isJSON($foo), which can be useful.

    -- hugh
     
  8. rodeokid

    rodeokid Member

    Level: Community
    Ok so i tried with this:
    Code (Text):
    $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
     
  9. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Not really.

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

    Code (Text):

    return implode("<br />\r\n", $data['liste_des_formations___employes']);
     
    I'm relatively sure it should already be an array in $data.

    -- hugh
     
  10. rodeokid

    rodeokid Member

    Level: Community
    Ok thanks Hugh... now this single line
    Code (Text):
    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.
     
  11. rodeokid

    rodeokid Member

    Level: Community
    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 (Text):
    $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 (Text):
    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
     
  12. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    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 (Text):

    $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
     
  13. rodeokid

    rodeokid Member

    Level: Community
    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
     
  14. rodeokid

    rodeokid Member

    Level: Community
    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
     
  15. rodeokid

    rodeokid Member

    Level: Community
    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
     
  16. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    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
     

Share This Page