tags:

views:

113

answers:

1

Hello all,

I am in need to display all the Countries List in ASC order. But with a line inbetween every end of alphabets. This is by using PHP and SMARTY

Example:

America

Australia


Belgium


India


Like the above i need to display the O/P.

My query is

$str = 'A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,Y,Z';
$let=explode(',', $str, 24);

for($i=0; $i<25; $i++)

{

  $letter = $let[$i][0];

  $select="select *from tbl_country where Country_name like '".$letter."%' ORDER BY Country_name ASC"; 

  $country =$this->ExecuteQuery($select, "select"); 

  $objSmarty->assign("letter",$letter); 

}

$objSmarty->assign("Exe_Wcat",$country);

By executing the above query i am getting all the countires in ORDER. But how should i insert the line break ?

Thanks in Advance...

Fero

+2  A: 

When you're looping through the list of countries in your template code, check to see whether the first letter of the country has changed compared to the last loop iteration. If so, output a horizontal line.

To get the first letter, you can use the substring plugin mentioned here:

{$country|substr:0:1}

You can capture the output of that variable:

{capture name=letter}
    {$country|substr:0:1}
{/capture}

... and then check it at the top of your loop against the last capture:

{if $smarty.capture.letter ne ''}
    {if $smarty.capture.letter ne $country|substr:0:1}
        <hr />
    {/if}
{/if}

p.s. I'm not quite sure about the syntax of the inner if statement above, you might have to capture the country letter twice, so you can do something like:

{if $smarty.capture.letter ne $smarty.capture.letter2}

Edit per comment about finding out if it's the last iteration of the loop - from the docs:

{foreach from=$items key=part_id item=prod name=products}
  <a href="#{$part_id}">{$prod}</a>{if $smarty.foreach.products.last}<hr>{else},{/if}
{foreachelse}
Dominic Rodger
+1 Using this approach you can also modify your SQL so that you only carry out one select instead of the 25 you're currently doing
Glen
will u please tell me how to get the last loop iteration in smarty
Fero
@Fero - I'd love to, but the documentation (http://www.smarty.net) is down for me. Why don't you take a look?
Dominic Rodger
@Fero - edited in for you
Dominic Rodger