douxianglu4370
2019-04-01 17:36
浏览 57
已采纳

从函数中回显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.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doushou7169 2019-04-01 18:14
    已采纳

    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.

    评论
    解决 无用
    打赏 举报
查看更多回答(2条)

相关推荐 更多相似问题