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

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>';
}
  • 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 Dec 12 '15 at 18:02
  • @Leoalv - What error are you being show? – Lodder Dec 12 '15 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 Dec 12 '15 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. – Dennis Heiden Dec 13 '15 at 16:49

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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