我需要在循环外获取变量$ percent_format,以便在我的样式中使用,以获取div的动态宽度。但是问题是我用Loop的sql结果参数化了功能Ofen_Auslastung。知道如何解决这个问题吗?
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>
谢谢您的帮助。
答案 0 :(得分:1)
您正在回显字符串的这一行:
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>";
将帮助您关闭并重新打开变量附近的php ...
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>";
答案 1 :(得分:1)
您始终可以在循环外声明一个变量,然后在循环内进行设置:
$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>";
?>
答案 2 :(得分:1)
我认为您正在尝试使用名为inner的类在div元素中添加动态宽度。根据我的说法,如果要动态更改元素 div 的样式css,则不需要在循环外使用变量 $ percent_format ,该变量已经在循环内。您只需要在div中添加名为 inner 的 class 的内嵌CSS ,就像下面粗体显示的代码行一样:
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>";
}
您无需为此在foreach之外调用变量 $ percent_format 。而且,如果您仍想在循环外调用变量,则可以使用它。但是这样做只会显示循环中最后一项的值。 我希望这可以解决您的问题。