从函数中回显PHP变量宽度:样式

I need to get the variable $percent_format outside of the loop for usage in my style to get the dynamical width für the div. But the Problem is i parameterized the funcion Ofen_Auslastung with a sql result from the Loop. Any idea how to fix this?

foreach ($connection->query($sql) as $row) {

                        $j=0;
                        echo "<tr>";
                            echo "<td> <a href='Kapauebersicht.php?OfenName=".$row['Name']."'><button onclick='myFunction()'><img src='http://xxx/xxx/Bilder/".$row[$j].".png' height='80px'></button></a></td>";       //Bezeichnung1
                            echo "<td>".$row[$j]."</td>";       //Bezeichnung1
                            list($total, $percent_format, $Anzahl) = Ofen_Auslastung($row[$j]);
                            $j++;
                            echo "<td>".$row[$j]."</td>";       //Bezeichnung2
                            $j++;
                            echo "<td>".$row[$j]."</td>";       //Bezeichnung3
                            $j++;
                            echo "<td><div class='outter'><div class='inner' >$percent_format%</div> $Anzahl belegte Plätze sind $percent_format% Auslastung von $total Plätzen. <p /></td>";
                        echo "</tr>";
                    }

                $connection = null; //reset connection


            }
            catch(PDOException $e)
                {
                    echo $e->getMessage();
                }           
    echo "</table>";

            //echo "<pre>";
            //print_r($sort);
            //echo " ****************************************************************** <br>";
            //print_r($sorted);

            //echo "</pre>";

            ?>

</table>
<button  value="Zurück" class="Button3" onclick="location.href='ma_QualiOverview.php'">Zurück</button>
<style type="text/css">
.outter{
    height:25px;
    width: 200px;
    border:solid 1px #000;
}
.inner{
    height:25px;
    width:<?php echo $percent_format ?>%;
    border-right:solid 1px #000;
    background: rgb(30,87,153); /* Old browsers */
background: -moz-linear-gradient(top, rgba(30,87,153,1) 0%, rgba(41,137,216,1) 50%, rgba(32,124,202,1) 51%, rgba(125,185,232,1) 100%); /* FF3.6-15 */
background: -webkit-linear-gradient(top, rgba(30,87,153,1) 0%,rgba(41,137,216,1) 50%,rgba(32,124,202,1) 51%,rgba(125,185,232,1) 100%); /* Chrome10-25,Safari5.1-6 */
background: linear-gradient(to bottom, rgba(30,87,153,1) 0%,rgba(41,137,216,1) 50%,rgba(32,124,202,1) 51%,rgba(125,185,232,1) 100%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#1e5799', endColorstr='#7db9e8',GradientType=0 );
}
</style>

thx for your help.

I think you are trying to add dynamic width in the element div with class named inner. According to me you doesn't need variable $percent_format value outside the loop if you want to change the style css of the element div dynamically, which is already inside the loop. You just need to add inline css in div with class named inner like the code line in bold written below:

foreach ($connection->query($sql) as $row) {
  $j=0;
  echo "<tr>";
  echo "<td> <a href='Kapauebersicht.php?OfenName=".$row['Name']."'><button onclick='myFunction()'><img src='http://xxx/xxx/Bilder/".$row[$j].".png' height='80px'></button></a></td>";       //Bezeichnung1
  echo "<td>".$row[$j]."</td>";       //Bezeichnung1
  list($total, $percent_format, $Anzahl) = Ofen_Auslastung($row[$j]);
  $j++;
  echo "<td>".$row[$j]."</td>";       //Bezeichnung2
  $j++;
  echo "<td>".$row[$j]."</td>";       //Bezeichnung3
  $j++;
  echo "<td><div class='outter'><div class='inner' style='width: $percent_format%;'>$percent_format%</div> $Anzahl belegte Plätze sind $percent_format% Auslastung von $total Plätzen. <p /></td>";
  echo "</tr>";
}

You doesn't need to call the variable $percent_format outside the foreach for this. And if you still want to call the variable outside the loop, you can use it. But doing so will only display the last item's value from the loop. I hope this might solve your problem.

This line you are echo-ing a string:

 echo "<td><div class='outter'><div class='inner' >$percent_format%</div> $Anzahl belegte Plätze sind $percent_format% Auslastung von $total Plätzen. <p /></td>";

Would help to close and reopen php around the variable...

 echo "<td><div class='outter'><div class='inner' >" . $percent_format . "%</div> $Anzahl belegte Plätze sind " . $percent_format . "% Auslastung von $total Plätzen. <p /></td>";

You can always declare a variable outside of the loop then set it within the loop:

$percent_format = "";

foreach ($connection->query($sql) as $row) {

                        $j=0;
                        echo "<tr>";
                            echo "<td> <a href='Kapauebersicht.php?OfenName=".$row['Name']."'><button onclick='myFunction()'><img src='http://xxx/xxx/Bilder/".$row[$j].".png' height='80px'></button></a></td>";       //Bezeichnung1
                            echo "<td>".$row[$j]."</td>";       //Bezeichnung1
                            list($total, $pcnt_format, $Anzahl) = Ofen_Auslastung($row[$j]);
$percent_format = $pcnt_format;
                            $j++;
                            echo "<td>".$row[$j]."</td>";       //Bezeichnung2
                            $j++;
                            echo "<td>".$row[$j]."</td>";       //Bezeichnung3
                            $j++;
                            echo "<td><div class='outter'><div class='inner' >{$percent_format}%</div> $Anzahl belegte Plätze sind $percent_format% Auslastung von $total Plätzen. <p /></td>";
                        echo "</tr>";
                    }

                $connection = null; //reset connection


            }
            catch(PDOException $e)
                {
                    echo $e->getMessage();
                }           
    echo "</table>";

            //echo "<pre>";
            //print_r($sort);
            //echo " ****************************************************************** <br>";
            //print_r($sorted);

            //echo "</pre>";

            ?>