2014-11-14 9 views
1

제목이 명확하지 않다하지만 내가 필요로하는 것은이 같은 경우 죄송합니다 :저장 배열

나는 dB에서 foreach는 인쇄 데이터를하고 난 다음 배열을 저장해야 여러 변수가 다른 모든 변수와 함께 주 변수를 인쇄하십시오.

foreach ($aItems as $i => $aItemInfo) { 

    $var1 = ''; 
    $var2 = ''; 
    $var3 = ''; 
    $var1 .= $aItemInfo['val1']; 
    $var2 .= $aItemInfo['val2']; 
    $var3 .= $aItemInfo['val3']; 

    // some operations with the db data 
    if ($var1 > 0) { 

     if ($var1 == 1 && $var2 == 0) { 
      $a = 'Some text.'; 
     } 
     else if ($var1 == 1 && $var2 == 1) { 
      $a = 'Some other text.'; 
     } 
     else if ($var1 > 1 && $var2 == 0) { 
      $a = 'Some new text.'; 
     } 
     else if ($var1 > 1 && $var2 == 1) { 
      $a = 'Some other new text.'; 
     } 

     $new_var = ''; 
     // html code 
     $new_var .= '<div id=""> 
         <a id="">' . $othervar . '</a> 
        </div> 
        <div id=""> 
         <span id="">' . $a . '</span> 
        </div>'; 
    } 

    if ($var > 0) { 
     // html code 
     $other_var .= ' . $vars . '; 
    } 

    // html structure 
    $main_var .= ''; 
} 

그리고 주요 변수 인쇄 :

<div class=""> 
    <?php echo $main_var; ?> 
</div> 

사실 일부 변수는 이전에 비해 동일합니다. 나는 "="앞에 점을 넣으려고했지만 실제로 어떻게 작동하는지 또는 무엇을위한 것인지 알지 못합니다 ...

내가 뭘 잘못하고있어? 배열의 각 값을 저장할 수 있도록 각 변수를 만드는 올바른 방법은 무엇입니까?

답변

1

이 경우 정확하게 .= 병합을 사용하고 있지 않습니다. 사용자가 작성한 조건이 맞으면 병합됩니다. 초기

:

$var1 = '1'; 
if($var == 1) { // okay condition 

} 

두 번째 반복 루프 내부에 또 다른 선언.

그냥 이런 식으로 뭔가를 만들 : 그것은 당신이 할 indend 일을하지 않습니다

$main_var = $new_var = ''; // initialize output outtop 
foreach ($aItems as $i => $aItemInfo) { 

    if ($aItemInfo['val1'] == 1 && $aItemInfo['val2'] == 0) { 
     $a = 'Some text.'; 
    } 
    else if ($aItemInfo['val1'] == 1 && $aItemInfo['val2'] == 1) { 
     $a = 'Some other text.'; 
    } 

    // html code 
    $new_var .= ' 
     <div id=""> 
      <a id="">' . $othervar . '</a> 
     </div> 
     <div id=""> 
      <span id="">' . $a . '</span> 
     </div> 
    '; 


    if ($aItemInfo['val1'] > 0) { 
     // html code 
     // another process here whatever it is 
    } 

    // html structure 
    $main_var .= $new_var; 
} 
+0

감사합니다! 이제 나는 내 잘못을 본다. 제발 upvote 나 -1 그래서 없어 : ( –

+0

@ ChazyChaz 다행이 도움이 – Ghost

1

이 시도 :

$var1 = $aItemInfo['val1']; 
$var2 = $aItemInfo['val2']; 
$var3 = $aItemInfo['val3']; 

if ($var1 == 1 && $var2 == 0) { 
    $a = 'Some text.'; 
} 
else if ($var1 == 1 && $var2 == 1) { 
    $a = 'Some other text.'; 
} 
else if ($var1 > 1 && $var2 == 0) { 
    $a = 'Some new text.'; 
} 
else if ($var1 > 1 && $var2 == 1) { 
    $a = 'Some other new text.'; 
} 

if ($var > 0) { 
    $new_var = ''; 
    // html code 
    $new_var .= '<div id=""> 
        <a id="">' . $othervar . '</a> 
       </div> 
       <div id=""> 
        <span id="">' . $a . '</span> 
       </div>'; 
}