การใช้ array

10 replies [Last post]
deckit
Joined: 2007-06-07
Points: 0
User offline. Last seen 49 weeks 1 day ago.

เราจะเรียกใช้งาน array ใน index.tpl เวลาที่เราใช้ smarty template ได้ยังไงหรอคะ

roteee
roteee's picture
Joined: 2007-06-06
Points: 49
User offline. Last seen 21 hours 56 min ago.

ทำได้โดย assign ค่า ตัวแปร array เข้าไปใน smarty แล้วฝั่ง template ก็ใช้ foreach วนลูปค่าใน array

ตัวอย่าง

File: testArrSmarty.php
--------------------
<?php
    $myCustomerArr = array(
        array(
             'id' => 1,
             'name' => 'Mister 01'
        ),
        array(
             'id' => 2,
             'name' => 'Mister 02'
        ),
        array(
             'id' => 3,
             'name' => 'Mister 03'
        ),
    );

    include "Smarty/libs/Smarty.class.php";
   
    $smarty = new Smarty;
    $smarty->template_dir   = 'templates';
    $smarty->compile_dir    = 'templates_c';
   
    $smarty->assign('myCustomerArr', $myCustomerArr);
    $smarty->display('testArrSmarty.html');
?>

 

File: templates/testArrSmarty.html
--------------------
<html>
 <head>
  <title>Test Array in Smarty - phpzealots.com</title>
  <meta name="generator" content="editplus" />
 </head>

 <body>
    <h1>My Customers</h1>
    <table border="1">
        <tr>
            <td align="center"><b>ID</b></td>
            <td align="center"><b>Name</b></td>
        </tr>
        <!--{foreach from=$myCustomerArr item=theCustomer}-->
        <tr>
            <td>{$theCustomer.id}</td>
            <td>{$theCustomer.name}</td>
        </tr>
        <!--{/foreach}-->
    </table>
 </body>
</html>

 

หรือดูรายละเอียดได้ที่ :http://smarty.php.net/manual/en/language.function.foreach.php

 

roteee
roteee's picture
Joined: 2007-06-06
Points: 49
User offline. Last seen 21 hours 56 min ago.

นอกจากนี้ สามารถใช้ if-else เพื่อเช็คว่า array นั้นว่างหรือเปล่า ก่อนได้น้ะครับ เช่น

 <!--{if !$myCustomerArr}-->
    <font color="#FF0000">No customer found!</font>
 <!--{else}-->
    <h1>My Customers</h1>
    <table border="1">
        <tr>
            <td align="center"><b>ID</b></td>
            <td align="center"><b>Name</b></td>
        </tr>
        <!--{foreach from=$myCustomerArr item=theCustomer}-->
        <tr>
            <td>{$theCustomer.id}</td>
            <td>{$theCustomer.name}</td>
        </tr>
        <!--{/foreach}-->
    </table>
<!-- {/if}-->

 

roteee
roteee's picture
Joined: 2007-06-06
Points: 49
User offline. Last seen 21 hours 56 min ago.

เพิ่มเติมอีกหน่อย

ใน smarty เวลาอ้างถึง element ใน array จะใช้จุด (.) ตามด้วยชื่อ key เช่น

PHP - $myArray[id]
Smarty - $myArray.id

 

nongsa
Joined: 2008-08-28
Points: 0
User offline. Last seen 1 year 47 weeks ago.
จาก php ต้องการ ให้มีการคำนวณ ใน loop ทำงัยค่ะ ############################################# นี่คือส่วน ของ php เก่า $sql = "SELECT * FROM test "; $sql .= "WHERE ITEMNUM LIKE '". $p ."%' AND CURR='USD' AND STATUS='Y' "; $conn->SetFetchMode(ADODB_FETCH_ASSOC); $recordSet = &$conn->Execute( $sql); if (!$recordSet) print $conn->ErrorMsg(); else { while (!$recordSet->EOF) { $pa1 = $recordSet->fields['a1']; $pa2 = $recordSet->fields['a2']; $Totalan = $recordSet->fields['an']*$TotalYear; $Totalai = $recordSet->fields['ai']+$Totalan; $recordSet->MoveNext(); } } ########################################### {section name=co loop=$contacts} {*math assign="col" equation="x * y" x=$smarty.section.contacts[co].a1 y=$TotalYear*} $contacts[co].ai} $contacts[co].an} {sectionelse} No items found {/section} อยากรู้ว่า ทำงัย ถึงจะคำนวณ ในแต่ละ Array ได้อ่ะ ช่วยหนอ่ยน่ะค่ะ
nongsa
Joined: 2008-08-28
Points: 0
User offline. Last seen 1 year 47 weeks ago.
ได้ แล้วค่ะ {$contacts[co].a1|number_format} {$contacts[co].an|number_format} {$contacts[co].TYear+$contacts[co].ai|number_format} {$contacts[co].pj|number_format} {$contacts[co].IRR} %


©2007-2010 PHPZealots.com. All right reserved.