Skip to Content

Welcome!

This community is for professionals and enthusiasts of our products and services.
Share and discuss the best content and new marketing ideas, build your professional profile and become a better marketer together.

This question has been flagged
As a moderator, you can either validate or reject this answer.
Accept Reject
96 Views

1. Adjust ur column xml

key point:

type = sql

query = ur select query

filter = array

key_field = 'what u want to store into database'

value_field = 'what u want to show to user'

layout = joomla.form.field.list-fancy-select

multiple = true (make user can select multiple)


2. Then you need to adjust the table to ensure it can be saved correctly since it is single string containing multiple values

Example file location:

U can direct copy paste below code and adjust 'field_name' to ur own field name

=============================================================

if (isset($array['field_name'])) {

            if (is_array($array['field_name'])) {

                $array['field_name'] = implode(',', $array['field_name']);

            } elseif (strpos($array['field_name'], ',') != false) {

                $array['field_name'] = explode(',', $array['field_name']);

            } elseif (strlen($array['field_name']) == 0) {

                $array['field_name'] = '';

            }

        } else {

            $array['field_name'] = '';

        }

==============================================================


3. Now u need to adjust model to ensure it can be render out when u edit it

Example file location:


U can also direct copy paste below code and change 'field_name' to ur own field name

============================================================

$array = array();


            foreach ((array) $data->field_name as $value)

            {

                if (!is_array($value))

                {

                    $array[] = $value;

                }

            }

            if(!empty($array)){


            $data->field_name = $array;

            }

============================================================


Now the data has already been saved into the database. You can also edit it if you want to.


Without any further action, u will see the data showing out like this:

Which is directly showing out the database value


Now u can utilize below code to filter the id to array then select the name based on the id

=================================================================

$db = JFactory::getDbo();

                                    $query = $db->getQuery(true);


                                    // Convert field_name to an array of product IDs

                                    $productIds = explode(',', $item->field_name);


                                    // Ensure IDs are integers for security

                                    $productIds = array_map('intval', $productIds);


                                    // Prepare the IN clause

                                    $query->select($db->quoteName('product_name'))

                                        ->from($db->quoteName('#__hikashop_product'))

                                        ->where($db->quoteName('product_id') . ' IN (' . implode(',', $productIds) . ')');


                                    $db->setQuery($query);

                                    $productNames = $db->loadColumn(); // Fetch all matching product names

                                    // Convert array to a string format like "product_name1, product_name2"

                                    $product_name = implode(', ', $productNames);


                                    echo $product_name; ?>


Actaul step:

1. Convert the multiple value to array

2. Ensure the id is int

3. Select query to find the name based on id

4. Convert array data to value like (product A, product B)

5. Echo it out


Final Result:


Summary:

When you want to store multiple values in one column, you need to adjust the XML, model, and table (for the backend) to ensure it can be saved successfully. Then, you also need to adjust the default.php file to ensure it echoes out correctly, since the data is an ID and you want to show the item's name.

Avatar
Discard

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!