跳至内容

欢迎!

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.

此问题已终结
作为版主,您可以批准或拒绝该答案。
接受 拒绝
97 查看

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.

形象
丢弃

您的回复

请大家尽量给个实质性的回答。如果您想回答这个问题或发表评论,只需 使用评论工具。请记住,您可以随时修改您的答案。 - 不用重复回答同样的问题。另外,请不要忘记投票 - 它确实有助于选择最好的问题和答案!