1

I'm trying to close and open a new tag between results, using the tags as separator in implode.

Here's the code:

$return = array();
if ($result && is_array($result) && sizeof($result)) {
    foreach ($result as $row) {
        $return[$row['id_feature']]['values'][] = $row['value'];
        $return[$row['id_feature']]['name'] = $row['name'];
    }
    $tag = '</a><a>';
    foreach ($return as $key=>$row) $return[$key]['value'] = implode($tag, $row['values']);
}

And here's my result:

<td><a>Moderna</a><a>Classica</a></td>

It looks great (on the code), the problem is the real result on the browser.

It appears like this (tried with Chrome, Safari and Firefox):

Moderna</a><a>Classica

I wanted to post a screenshot but I can't cause I'm a noob on stackoverflow...

I tried everything, but I haven't found a reason yet.

What's wrong? Is the function or am I wrong on thinking that I can use html as separator on implode?


I'll post the complete code, as you request:

public function getFrontFeatures($id_product, $separator = null, $id_feature = null) {
    if (version_compare(_PS_VERSION_, '1.5.0.0', '>=')) {
        $id_lang = (int)Context::getContext()->cookie->id_lang;
    } else {
        global $cookie;
        $id_lang = $cookie->id_lang;
    }
    if ($separator == null) {
        $config = $this->_getModuleConfiguration();
        $separator = $config['featureSeparator'];
    }
    $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
    SELECT fp.id_feature, vl.value, fl.name
    FROM `'._DB_PREFIX_.'feature_product` fp
    LEFT JOIN `'._DB_PREFIX_.'feature_value` v ON (fp.`id_feature_value` = v.`id_feature_value`)
    LEFT JOIN `'._DB_PREFIX_.'feature_value_lang` vl ON (v.`id_feature_value` = vl.`id_feature_value` AND vl.`id_lang` = '.(int)$id_lang.')
    LEFT JOIN `'._DB_PREFIX_.'feature` f ON (f.`id_feature` = v.`id_feature`)
    '.(version_compare(_PS_VERSION_, '1.5.0.0', '>=') && Shop::isFeatureActive() && Shop::getContext() == Shop::CONTEXT_SHOP ? Shop::addSqlAssociation('feature', 'f') : '').'
    LEFT JOIN `'._DB_PREFIX_.'feature_lang` fl ON (fl.`id_feature` = f.`id_feature` AND fl.`id_lang` = '.(int)$id_lang.')
    WHERE fp.`id_product` = '.(int)$id_product
    . ($id_feature != null && $id_feature ? ' AND f.`id_feature` = '.(int)$id_feature : '')
    . ' ORDER BY ' 
    . (version_compare(_PS_VERSION_, '1.5.0.0', '>=') ? 'f.`position` ASC, ' : '')
    . 'fp.`position` ASC');
    $return = array();
    if ($result && is_array($result) && sizeof($result)) {
        foreach ($result as $row) {
            $return[$row['id_feature']]['values'][] = $row['value'];
            $return[$row['id_feature']]['name'] = $row['name'];
        }
        $tag = '</a><a>';
        foreach ($return as $key=>$row) $return[$key]['value'] = implode($tag, $row['values']);
    }
    if ($id_feature != null && $id_feature && isset($return[$id_feature])) {
        return $return[$id_feature]['value'];
    } else {
        return $return;
    }
}

This is the code on the .tpl:

{if isset($features) && $features}
        <!-- Data sheet -->
        <section class="page-product-box visible-xs">
            <h3 class="page-product-heading">{l s='Data sheet'}</h3>
            <table class="table-data-sheet">
                {foreach from=$features item=feature}
                <tr class="{cycle values="odd,even"}">
                    {if isset($feature.value)}
                    <td>{$feature.name|escape:'html':'UTF-8'}</td>
                    <td><a>{$feature.value|escape:'html':'UTF-8'}</a></td>
                    {/if}
                </tr>
                {/foreach}
            </table>
        </section>
        <!--end Data sheet -->
    {/if}

And...now I'm not a noob anymore :D I can post screenshots:

Result:

enter image description here

On Inspector:

enter image description here

Hope this helps...

5
  • This doesn't address your implode problem, but just fyi sizeof($result) is redundant after already checking $result, as an empty array will evaluate as false. Commented May 27, 2015 at 16:31
  • The PHP code you included does not show where you output the <td><a> and </a></td> tags around the imploded array. Maybe you should edit to include this as it may be part of your problem. Commented May 27, 2015 at 16:33
  • try getting the length of $return[$key]['value'] and check with the number of characters on screen. There could be characters that don't display. Commented May 27, 2015 at 16:34
  • What's the point of <a> tags without href or name to begin with? Commented May 28, 2015 at 8:09
  • I was testing the function, obviously will have a href, title and so on... Commented May 28, 2015 at 8:11

2 Answers 2

1

implode just add "</a><a>" between each element on your array (so, you don't add anything before and after) if you put an <a> before and a </a> after it will work fine.

return[$key]['value'] = "<a>".implode($tag, $row['values'])."</a>"
Sign up to request clarification or add additional context in comments.

4 Comments

but you look the code?, for me that result doesn't make sense. The error could be another thing, but for that whe need the complete code.
maybe he started with items like <td><a>Modern and Classica</a></td>
It would be possible, in that case my answer doesn't help on anything, but if that is the case, why he didn't just started with <td><a>Modern</a> and <a>Classica</a></td> ? at least for me the code show's it wasn't the intention.
I confirm that I take care of opening the tag first, but I did it on the .tpl file.
0

Here in your template you are using escape.

<td><a>{$feature.value|escape:'html':'UTF-8'}</a></td>

but the string you are escaping contains HTML. So your string

Moderna</a><a>Classica

will be converted to

Moderna&lt;/a&gt;&lt;a&gt;Classica

which is why the tags are displaying as text in your browser rather than being rendered as HTML. (You can see in the image you included from Inspector that the </a><a> is black text rather than the color of the outer tags.)

It is good that you are escaping your output. But since what you are returning from your function will contain HTML, you will have to escape in the function rather than in the template.

$return[$row['id_feature']]['values'][] = htmlspecialchars($row['value']);

Then you should be able to use the value in the template without escaping.

<td><a>{$feature.value}</a></td>

This will become obvious, but I guess I should add that when you get to the point where you are ready to add href to your <a> tags, using implode to join the values like that will no longer work (unless the link will always be the same.)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.