循环回声声明 - PHP

时间:2014-12-17 11:53:10

标签: php mysql

我想在echo语句中执行while循环。

我的代码段在这里:

$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帮帮我

5 个答案:

答案 0 :(得分:0)

然后就这样做:

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

答案 1 :(得分: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>";
}

答案 2 :(得分:0)

只需创建一个变量并继续追加所需的输出。

按如下方式打印结果。

$output = ""

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

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

        /*Append whatever you want*/


$output .= " /div";

}

echo $output;

答案 3 :(得分: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 } ?>

答案 4 :(得分:0)

你不能在wcho中使用while循环,但是你可以做这样的事情。

<?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

    }

?>

此示例仅用于解决echo中while循环的问题。