1

I created a simple component with a subform that stores data in a database. Now I need to display this data on the front page. The data is displayed as is from db

{"comment0":{"comment":"one"},"comment1":{"comment":"two"}}

How do I fix it and get values one, two on front-end? Please tell me step by step.

What I have:

views/myview/tmpl/default.php

<?php defined('_JEXEC') or die; ?>
<?php foreach ($this->items as $item) : ?>

            <?php echo $item->comment; ?>

<?php endforeach; ?>

views/myview/view.html.php

<?php defined('_JEXEC') or die;
class MycomponentViewMyView extends JViewLegacy {
    protected $items;
    public function display($tpl = null)
    {
        $this->items        = $this->get('Items');

        $app        = JFactory::getApplication();
        $params     = $app->getParams();
        $this->assignRef( 'params', $params );

        if (count($errors = $this->get('Errors')))
        {
            JError::raiseError(500, implode("\n", $errors));
            return false;
        }
        parent::display($tpl);
    }
}

also standard model

<?php defined('_JEXEC') or die;
class MyComponentModelMyModel extends JModelList {
    public function __construct($config = array())
    {
        if (empty($config['filter_fields']))
        {
            $config['filter_fields'] = array(
                'id', 'a.id'
            );
        }

        parent::__construct($config);
    }

    protected function populateState($ordering = null, $direction = null)
    {
        $id = JRequest::getInt('id');
        $this->setState('id', $id);
    }

    protected function getListQuery()
    {
        $db     = $this->getDbo();
        $query  = $db->getQuery(true);

        $query->select(
            $this->getState(
                'list.select',
                'a.id, a.comment,'
            )
        );
        $query->from($db->quoteName('#__mycomponent').' AS a');
        $query->where('(a.state IN (0, 1))');

        if ($id = $this->getState('id'))
        {
            $query->where('a.id = '.(int) $id);
        }
        return $query;
    }
}
  • Are you saying that echo $item->comment; is only looping one time and is displaying {"comment0":{"comment":"one"},"comment1":{"comment":"two"}}? – mickmackusa Jul 6 '18 at 0:21
0

I don't know if there is a Joomla-specific fix for this, but it seems that you will simply need to decode your json string. I don't know how your actual data may vary or if you will want to including any conditional handling, but you can just loop through the generated multi-dimensional data.

Assuming echo $item->comment; inside of your loop is displaying {"comment0":{"comment":"one"},"comment1":{"comment":"two"}}, then you can

Here is a demonstration:

$comment_json = '{"comment0":{"comment":"one"},"comment1":{"comment":"two"}}';
↑↑↑↑↑↑↑↑↑↑↑↑↑
------------------------------------- this is effectively your $item->comment
                        ↓↓↓↓↓↓↓↓↓↓↓↓↓
$comments = json_decode($comment_json);

foreach ($comments as $key => $set) {
    echo "$key contains:\n";
    foreach ($set as $label => $value) {
        echo "\t$label : $value\n";
    }
    echo "---\n";
}

Output:

comment0 contains:
    comment : one
---
comment1 contains:
    comment : two
---

*I've added \n and \t for better readability.

There will be different ways to handle the decoded data, but this is probably the easiest to read and manage.

Generally speaking, storing data in a database as a json string can indicate a suboptimal database design in some cases ...but I'll avoid that debate because too much is unknown about your data and its intended usage.

0

It works. thanks for the solution mickmackusa.

example

views/myview/tmpl/default.php

<?php defined('_JEXEC') or die; ?>
<?php foreach ($this->items as $item) : ?>
<?php
$comment_json = $item->comment;
$comments = json_decode($comment_json);
foreach ($comments as $key => $set) {
    echo "$key contains:\n";
    foreach ($set as $label => $value) {
        echo nl2br ("\t$label : $value\n");
    }
    echo "---\n";
}
?>


<?php endforeach; ?>
  • I am happy to hear that my solution was useful to you. However, I don't feel that there is any benefit to adding your redundant answer to this page. – mickmackusa Jul 7 '18 at 22:28
  • I've published my answer for beginners, it fully explains exactly where to place the code. My experience shows that the lack of a complete instruction can create additional questions and take a lot of time. – Nik Jul 9 '18 at 7:09
  • I would have been happy to edit my answer if you felt it wasn't clear enough. I went out of my way to explain what the variable represents via coded comment. – mickmackusa Jul 9 '18 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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