Question

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.

Was it helpful?

Solution

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 -->

OTHER TIPS

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}

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top