2013-05-07 2 views
1

나는 PHP 파일 $cs_map_data라는 이름의 다음 다차원 배열을 가지고와 나는 유식 템플릿에 할당했습니다 모든 요소의다차원 배열에서 smarty로 키에 액세스하는 방법은 무엇입니까?

Array 
(
    [2] => Array 
     (
      [class_id] => 2 
      [class_name] => II 
      [class_checked] => 0 
      [class_subjects] => Array 
       (
        [0] => Array 
         (
          [cs_map_id] => 81 
          [subject_name] => 11 Engllish 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

       ) 

     ) 

    [3] => Array 
     (
      [class_id] => 3 
      [class_name] => III 
      [class_checked] => 0 
      [class_subjects] => Array 
       (
        [0] => Array 
         (
          [cs_map_id] => 155 
          [subject_name] => Hidi 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

        [1] => Array 
         (
          [cs_map_id] => 156 
          [subject_name] => 11 Maths 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

        [2] => Array 
         (
          [cs_map_id] => 157 
          [subject_name] => 11 Science 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

        [3] => Array 
         (
          [cs_map_id] => 158 
          [subject_name] => 11 Engllish 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

       ) 

     ) 

    [4] => Array 
     (
      [class_id] => 4 
      [class_name] => IV 
      [class_checked] => 0 
      [class_subjects] => Array 
       (
        [0] => Array 
         (
          [cs_map_id] => 86 
          [subject_name] => Physics 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

       ) 

     ) 

    [6] => Array 
     (
      [class_id] => 6 
      [class_name] => VI 
      [class_checked] => 0 
      [class_subjects] => Array 
       (
        [0] => Array 
         (
          [cs_map_id] => 144 
          [subject_name] => Mathematics 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

        [1] => Array 
         (
          [cs_map_id] => 145 
          [subject_name] => dfadadadsagfasrsarasrarBiology 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

       ) 

     ) 

    [7] => Array 
     (
      [class_id] => 7 
      [class_name] => VII 
      [class_checked] => 0 
      [class_subjects] => Array 
       (
        [0] => Array 
         (
          [cs_map_id] => 129 
          [subject_name] => Physics 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

        [1] => Array 
         (
          [cs_map_id] => 130 
          [subject_name] => Chemistry11 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

        [2] => Array 
         (
          [cs_map_id] => 131 
          [subject_name] => 11 Science 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

       ) 

     ) 

    [8] => Array 
     (
      [class_id] => 8 
      [class_name] => VIII 
      [class_checked] => 0 
      [class_subjects] => Array 
       (
        [0] => Array 
         (
          [cs_map_id] => 67 
          [subject_name] => Hidi 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

        [1] => Array 
         (
          [cs_map_id] => 68 
          [subject_name] => 11 Engllish 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

       ) 

     ) 

    [9] => Array 
     (
      [class_id] => 9 
      [class_name] => IX 
      [class_checked] => 0 
      [class_subjects] => Array 
       (
        [0] => Array 
         (
          [cs_map_id] => 87 
          [subject_name] => Mathematics 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

        [1] => Array 
         (
          [cs_map_id] => 88 
          [subject_name] => Hidi 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

        [2] => Array 
         (
          [cs_map_id] => 89 
          [subject_name] => 11 Science 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

       ) 

     ) 

) 

가 지금은 키 [cs_map_id]의 값에 액세스 할 및 [subject_name] 똑똑한 템플릿. foreach를 사용해 보았지만 키 [cs_map_id][subject_name]에서 값을 가져올 수 없습니다. foreach 루프에서 smarty로 키 [cs_map_id][subject_name]의 값을 어떻게 액세스해야하는지 누구든지 도와 줄 수 있습니까?

<table>       
       <tr> 
       {assign var='i' value=0} 
       {if $cs_map_data} 
       {foreach from=$cs_map_data item="map_data"} 
       {if $i%4 == 0}</tr><tr>{/if} 
       <td align="left" valign="top" width="150"> 

        <input type="checkbox" name="cs_map_id[]" id="{$map_data.cs_map_id}" value="{$map_data.cs_map_id}" onChange="get_test_by_category('{$map_data.cs_map_id}'); return false;" /> 
       {$map_data.subject_name} 
       </td> 
       {assign var='i' value=$i+1} 
       {/foreach} 
       {else} 
         <td> 
          <i>No Subject is assigned to the class.</i> 
        </td> 
       {/if} 
       </tr> 
       </table> 

중 하나가 멋지 템플릿에 forech에서 키의 값을 모두 사용하는 방법에 대한 좀 도와 줄래 다음과 같이 유식 템플릿 날에 쓴 코드는? 미리 감사드립니다.

+0

Smarty 2 또는 3을 사용하고 있습니까? – eidsonator

+0

@eidsonator Smarty 2 – PHPLover

+0

아, 그건 내가 foreach 구문에서 생각한 것입니다. 나는 3에 훨씬 더 익숙해 ... 2에서 3 사이에서이 영역에 큰 변화가있었습니다.하지만 foreach 대신 {SECTION}을 사용하려고합니다. "내부"배열을 반복합니다. – eidsonator

답변

3

Smarty 2 및 3에서 작동합니다. {foreach} 버전간에 많은 변경이 있었지만 {section} 동일하게 작동합니다.

이것은 당신의 배열을 통해 루프를 작동해야
{section name=map loop=$cs_map_data} 
    <tr> 
     <td>{$cs_map_data[map].class_name}</td> //this is the format to access your data from the array 
    {section name=subject loop=$cs_map_data[map].class_subjects} 
      <td> 
       {$cs_map_data[map].class_subjects[subject].subject_name} //this is how to access the entries of your 'inner' array 
      </td> 
    {/section} //end your nested section 
    </tr> 
{/section} //end your outer section 

...

섹션의 유식 설명서 : http://www.smarty.net/docsv2/en/language.function.section.tpl

행운을 빕니다!

+0

정말 고마워요. 귀하의 코드는 필자에게 필요한 마술을 만들었습니다. 내 생명을 구 해주셔서 다시 한번 감사드립니다. – PHPLover

+1

내가 처음에는 영리하게 배우기 시작했을 때 2 일 동안 저것을 해결했습니다! 뒤죽박죽이지만 머리를 감싸고 나면 꽤 간단합니다. 프로젝트에 행운을 빌어 요! – eidsonator

관련 문제