Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Echo语句中的While循环-PHP_Php_Mysql - Fatal编程技术网

Echo语句中的While循环-PHP

Echo语句中的While循环-PHP,php,mysql,Php,Mysql,我想在echo语句中执行而循环 我的代码片段如下: $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

我想在echo语句中执行
循环

我的代码片段如下:

$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>";
}
$sql=“从categorywithfilter中选择不同的(属性),其中subcategory='$cat';
$query=mysql\u查询($sql);
而($res=mysql\u fetch\u assoc($query)){
$sub=$res['attribute'];
$sqlfilter=“从categorywithfilter中选择*,其中属性='$sub';
$queryfilter=mysql\u查询($sqlfilter);
回声“
$sub
而($resf=mysql\u fetch\u assoc($queryfilter)){
回声‘ok’;
}
";
}
但它不起作用


请帮帮我,然后就这样做:

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

作为快速解决方案,试试这个

$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>";
}
$sql=“从categorywithfilter中选择不同的(属性),其中subcategory='$cat';
$query=mysql\u查询($sql);
而($res=mysql\u fetch\u assoc($query)){
$sub=$res['attribute'];
$sqlfilter=“从categorywithfilter中选择*,其中属性='$sub';
$queryfilter=mysql\u查询($sqlfilter);
回声“
$sub
";
而($resf=mysql\u fetch\u assoc($queryfilter)){
回声‘ok’;
}
回声“
";
}

只需创建一个变量并不断追加所需的输出

最后打印结果如下

$output = ""

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

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

        /*Append whatever you want*/


$output .= " /div";

}

echo $output;
$sql=“从categorywithfilter中选择不同的(属性),其中subcategory='$cat';
$query=mysql\u查询($sql);
而($res=mysql\u fetch\u assoc($query)){
$sub=$res['attribute'];
$sqlfilter=“从categorywithfilter中选择*,其中属性='$sub';
$queryfilter=mysql\u查询($sqlfilter);
?>

您不能在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循环的问题。

您看到的错误消息是什么?
echo“Stuff”;while(x){echo“More”}echo“End”
-人们试图一次完成所有事情是什么原因?然后从echo中提取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

    }

?>