2014-12-17 2 views
-6

echo 문에서 while 루프를 실행하고 싶습니다.Loop In Echo Statement - PHP

$sql="select distinct(attribute) from categorywithfilter where subcategory='$cat'"; 
$query=mysql_query($sql); 
while($res=mysql_fetch_assoc($query)){ 
$sub=$res['attribute']; 
$sqlfilter="select * from categorywithfilter where attribute='$sub' "; 
$queryfilter=mysql_query($sqlfilter); 

echo " <div class='form-group'> 
      <label class='col-sm-2 control-label'>$sub</label> 
      <div class='col-sm-6'> 

       <select name='category' class='form-control' id='filter'> 
       while($resf=mysql_fetch_assoc($queryfilter)){ 
        echo'ok'; 
       } 

       </select> 
      </div> 
      </div>"; 
} 

그러나 그것은 작동하지 않습니다 :

내 코드는 여기에있다.

Plz은 단지 이런 식으로 그것을 할 그런

+2

표시되는 오류 메시지는 무엇입니까? –

+1

'echo "Stuff"; 반면 (x) {echo "More"; } echo "End";'- 한 번에 모든 것을하려고하는 사람들은 무엇입니까? –

+0

에코에서 잠시 벗어나십시오. – Matheno

답변

0

을 좀 도와 :

echo "Anything you want"; 
while(....) { 
    echo " ... "; 
} 
echo "Text"; 
0

으로 빠른 해결책이 하나

$sql="select distinct(attribute) from categorywithfilter where subcategory='$cat'"; 
$query=mysql_query($sql); 
while($res=mysql_fetch_assoc($query)){ 
$sub=$res['attribute']; 
$sqlfilter="select * from categorywithfilter where attribute='$sub' "; 
$queryfilter=mysql_query($sqlfilter); 

echo " <div class='form-group'> 
      <label class='col-sm-2 control-label'>$sub</label> 
      <div class='col-sm-6'> 

       <select name='category' class='form-control' id='filter'>"; 
       while($resf=mysql_fetch_assoc($queryfilter)){ 
        echo 'ok'; 
       } 

      echo "</select> 
      </div> 
      </div>"; 
} 
0

그냥 변수를 만들 시도하고 필요한 출력을 추가 유지 .

다음과 같이 결과를 인쇄하십시오.

$output = "" 

while($resf=mysql_fetch_assoc($queryfilter)){ 

$output .= " div and other html tags "; 

     /*Append whatever you want*/ 


$output .= " /div"; 

} 

echo $output; 
0
$sql="select distinct(attribute) from categorywithfilter where subcategory='$cat'"; 
    $query=mysql_query($sql); 
    while($res=mysql_fetch_assoc($query)){ 
    $sub=$res['attribute']; 
    $sqlfilter="select * from categorywithfilter where attribute='$sub' "; 
    $queryfilter=mysql_query($sqlfilter); 
    ?> 


     <div class='form-group'> 
     <label class='col-sm-2 control-label'><?php echo $sub; ?></label> 
     <div class='col-sm-6'> 
     <select name='category' class='form-control' id='filter'> 
     <?php 
     while($resf=mysql_fetch_assoc($queryfilter)){ 
     echo'ok'; 
     } 
     ?> 
    </select> 
    </div> 
    </div> 
    <?php } ?> 
0

당신은 사용할 수 없습니다 동안 wcho 루프하지만 이런 식으로 뭔가를 할 수 있습니다.

<?php 

    $sql="select distinct(attribute) from categorywithfilter where subcategory='$cat'"; 
    $query=mysql_query($sql); 

    while($res=mysql_fetch_assoc($query)){ 

     $sub=$res['attribute']; 
     $sqlfilter="select * from categorywithfilter where attribute='$sub' "; 
     $queryfilter=mysql_query($sqlfilter); 

?> 
      <div class='form-group'> 
       <label class='col-sm-2 control-label'>$sub</label> 
       <div class='col-sm-6'> 
        <select name='category' class='form-control' id='filter'> 

     <?php 
        while($resf=mysql_fetch_assoc($queryfilter)){ 
         echo'ok'; # wrap ok with option tag 
        } 
     ?> 

        </select> 
       </div> 
       </div> 
<?php 

    } 

?> 

이 예제는 while 루프에있는 문제를 해결하기위한 용도로만 제공됩니다.