0

I am having the below array structure created based on some operations in PHP.

array
(
  [7] => array(
    [id] => 7
    [page] => dashboard
    [text] => Text 3
  )
  [1] => array(
    [id] => 1
    [page] => index
    [text] => Text 2
  )
  [3] => array(
    [id] => 3
    [page] => index
    [text] => Text 3
  )
}

I am using the below Smarty template to create table based on this array structure and it works fine.

<table>
    <tr>
        <th>{text key='sitetour+page'}</th>
        <th>{text key='sitetour+text'}</th>
    </tr>

    {foreach from=$allSteps key='id' item='step'}
      <tr>
          <td>{$step.page}</td>
          <td>{$step.text}</td>
      </tr>
    {/foreach}

</table>

What I am looking for is that, based on the value of "page", I need to create tables for each unique value of "page" instead of single table with all values.

Examples are provided below.

Current output: ( single table irrespective of the page value)

page      | text
---------------------
dashboard | text 3
index     | text 2
index     | text 3

Expected Output: ( 2 tables which are broken based on the page value)

dashboard 
---------------------
text 3

index
---------------------
text 2
text 3

I could not find any sample when I searched over here. Thanks in advance.

3 Answers 3

1

Can`t check.However,try:

{foreach from=$allSteps key='id' item='step' name="loop" }

  {if $smarty.foreach.loop.first } 
    {assign var="current" value=$step.page }
    <table>
     <thead>
      <tr> <td>{$step.page}</td> </tr> 
     </thead>
      <tr> <td>{$step.text}</td> </tr>
  {else}
     {if $current == $step.page }
       <tr>
         <td>{$step.text}</td>
       </tr>           
     {else} 
      {assign var="current" value=$step.page }
      </table> <!-- Close prev. table -->
      <table>  <!-- Start next table -->
       <thead>
        <tr> <td>{$step.page}</td> </tr> 
       </thead>
        <tr> <td>{$step.text}</td> </tr>
     {/if}
  {/if}
{/foreach}
</table> <!-- close last table -->
Sign up to request clarification or add additional context in comments.

11 Comments

In this case, how would be possible to add heading rows for each page. I see there is no way to track the break of the value to new one.
@Purus u can add only one time tag <thead> to table
No.. actually, I need multiple tables with unique Id for each unique page value so that I can further process some jquery functions.
On viewing this code, I understand that it will be no different than my version of code.. The final output will be same as per my understanding as your version does not track on the change of page to create a new table
This works only if there are more than 1 element in a category.. for example if the first category has only 1 row, its not displayed.. I tried your updated code.
|
0

You are expecting two tables, yet the html you have is only outputting one table. What you should do is format your data like so:

array
(
  ['dashboard'] => array(
        [7] => array(
        [id] => 7
        [text] => Text 3)
  )
  ['index'] => array(
    [1] => array(
    [id] => 1
    [text] => Text 2)

    [3] => array(
    [id] => 3
    [text] => Text 3)
  )
}

Where the page names are the keys in your php array, call this array $pages or something, then do something like:

{foreach $pages as $page}
<table>
    <tr>
        <th>{$page@key}</th>
    </tr>

    {foreach $page as $text}
      <tr>
          <td>{$text['text']}</td>
      </tr>
    {/foreach}

</table>
{/foreach}

4 Comments

Actually I don't have much control on how this array is created. So I am trying to figure out the solution based on the current array structure.
Loop through the array in php and create a new array like the one I mention
As said, I don't have much control on the PHP side. should go through a process with a client for changing those. :(
You can do the same array manipulation / recreation inside the Smarty template, too. It comes with a few extra curly brackets but both array iteration and array construction work.
0

because the output is linear, you can't really go back and forth between tables. what you'll need to do is have more than one foreach loop, and inside the loops put a condition

<table>
    <tr>
        <th>{text key='sitetour+page'}</th>
        <th>{text key='sitetour+text'}</th>
    </tr>

    {foreach $allsteps as $step}
      <tr>
          {if $step.page == "dashboard"}
              <td>{$step.text}</td>
          {/if}
      </tr>
    {/foreach}

</table>

<table>
    <tr>
        <th>{text key='sitetour+page'}</th>
        <th>{text key='sitetour+text'}</th>
    </tr>

    {foreach $allsteps as $step}
      <tr>
          {if $step.page == "index"}
              <td>{$step.text}</td>
          {/if}
      </tr>
    {/foreach}

</table>

edit: another solution would be to parse your results on the php side before sending it to the page

2 Comments

Sorry. This would not be possible as I don't know how many pages will be available to check as the data is dynamic.
then i would suggest parsing that out in the PHP file, and putting the entire table inside the foreach loop

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.