0

I'm using Repeatable field Type.

The data from database are:

{"social_icon":["fa fa-facebook","fa fa-twitter","fa fa-linkedin"],"social_link":["https://www.facebook.com/user","https://www.twitter.com/user","https://www.linkedin.com/user"]}

array made wit $array_links = json_decode($social_links, true);

Array ( [social_icon] => Array ( [0] => fa fa-facebook [1] => fa fa-twitter [2] => fa fa-linkedin ) [social_link] => Array ( [0] => https://www.facebook.com/user [1] => https://www.twitter.com/user [2] => https://www.linkedin.com/user ) ) 1

if I get information through loop:

<?php foreach ($array_links as $key => $value) {
                foreach ($value as $content) {
                    echo $content.'<br>';
                }
            } ?>

It shows me the following

fa fa-facebook
fa fa-twitter
fa fa-linkedin
https://www.facebook.com/user
https://www.twitter.com/user
https://www.linkedin.com/user

I want to build it:

<a href="https://www.facebook/user"><i class="fa fa-facebook"></i></a>

get data with $array_links[0][1];not serve me, because they use some 6 links, and other links will not use.

Thanks for helping me.

1 Answer 1

1

You could group each set of field by the key.

Call the parameter and convert to array:

$social_links = $params->get('social_links');
$array_links = json_decode($social_links, true);

Create a function in your helper.php that will group them by key

public function group_by_key($array) 
{
    $result = array();

    foreach ($array as $sub) 
    {
        foreach ($sub as $k => $v) 
        {
            $result[$k][] = $v;
        }
    }
    return $result;
}

Then push $array_links through the function and called it in mod_mymodule.php

$filtered_array = group_by_key($array_links);

Then loop through the result and display however you liek:

foreach ($filtered_array as $key => $val) 
{   
    echo '<a href="' . $val[0] . '"><i class="' . $val[1] . '"></i></a>';
}
4
  • Use this code in modules. in this case are extra fields for com_content; and using this function, when there is nothing in the database, shows me error. @Lodder
    – Leoalv
    Commented Dec 12, 2015 at 18:02
  • @Leoalv - What error are you being show?
    – Lodder
    Commented Dec 12, 2015 at 18:34
  • The error is invalid argument supplied for foreach() . And is detected within the function. The error occurs when a user does not fill your social links
    – Leoalv
    Commented Dec 12, 2015 at 23:36
  • Then write a simple if clause to test against the user input, if the social data from the input is empty, skip the processing. Commented Dec 13, 2015 at 16:49

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.