-1

I retrieve comments from my database. If the user clicks the "devam" link, I want that s/he can see the full comment.

In order to do that, I wrote the below code. I use template_lite and if the character count of the comment is bigger than 95, it makes it truncate. With the below code, when I click the "devam" link, it shows a longer comment but only the first one. Why? What's wrong?

foreach value=users from=$comment}
    <tr>
        <td>{$users.ADD_DATE}</td>
        <td>{$users.NAME}</td>
        {if $users.COMMENT|count_characters > 95}
            <td><span id="comment12kisa">{$users.COMMENT|truncate:95:"..."}
                </span><a href="" onclick="$('#comment12kisa').html($('#comment12uzun').html());return false;">devami</a></td>
        {else}
            <td><span id="comment12uzun">{$users.COMMENT}</span></td>
        {/if}
        <td><center><input type="checkbox" name="checkComment[]" value={$users.ID} /></center></td>
    </tr>

A unique id is needed,

What's wrong in:

 {foreach value=users from=$comment}
     <tr>
         <td>{$users.ADD_DATE}</td>
         <td>{$users.NAME}</td>
         {if $users.COMMENT|count_characters > 95}
             <td><span id="comment12kisa_{$users.COMMENT.id}">{$users.COMMENT|truncate:95:"..."}
             </span><a href="" onclick="$('#comment12kisa_{$users.COMMENT.id}').html($('#comment12kisaa_{$users.COMMENT.id}').html());return false;">devami</a></td>
         {else}
             <td><span id="comment12kisaa_{$users.COMMENT.id}">{$users.COMMENT}</span></td>
         {/if}
         <td><center><input type="checkbox" name="checkComment[]" value={$users.ID} /></center></td>
     </tr>
 {/foreach}

3 Answers 3

1

You need to put the full value of COMMENT into a hidden element, then show that. Currently you are only generating the truncated version in your HTML.

{if $users.COMMENT|count_characters > 95}
    <td><span id="comment12kisa">{$users.COMMENT|truncate:95:"..."}
    </span>
    <span id="comment12uzun" style="display"none">{$users.COMMENT}</span> <-- you need THIS
    <a href="" onclick="$('#comment12kisa').html($('#comment12uzun').html());return false;">devami</a></td>
{else}
1

Because you have one id for each of your spans. As a solution, you can add comment.id to the span, something like:

<span id="comment12kisa_{$users.COMMENT.id}">
    {$users.COMMENT|truncate:95:"..."}
</span>

Of course, you need to change it in the JavaScript code as well.

0
0

The problem is that you only have a truncated version of the comment or the full version, never both. If the comment is longer than 95 characters then you need to store the full version somewhere, and display it when the user clicks on the link.

    {if $users.COMMENT|count_characters > 95}
        <td>
            <span id="comment12kisa">{$users.COMMENT|truncate:95:"..."}</span>
            <a href="" onclick="$('#comment12kisa').html($('#comment12uzun').html());return false;">devamı</a>
            <span id="comment12uzun" style="display:none">{$users.COMMENT}</span>
        </td>
    {else}
        <td>
            <span id="comment12uzun">{$users.COMMENT}</span>
        </td>
    {/if}

Your Answer

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

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