簡體   English   中英

回聲 <svg> 在php函數中滿足條件時類中的元素

[英]Echo <svg> element in a class when condition is met in php function

我有一個簡單的代碼與一些相結合。

我想做的是如下:

我有一個php類在我的代碼中定義了一些屬性。 類 - 狀態

“狀態”的變量 - 圓,大小,顏色。

我想鏈接一個svg元素,它給出圓的大小和半徑,然后是顏色。

我有一個函數,當它滿足時, 語句將需要顯示具有大小和顏色的類圓,具體取決於滿足的條件。

這是班級:

<?php

class Statuses
{

var $circle;
var $size;
var $color;

public function print_condition()
{
    echo $this->circle;
    echo $this->size;
    echo $this->color;
}

public function condition($circle, $size, $color)
{
    $this->circle = $circle;
    $this->size = $size;
    $this->color = $color;
}

}

$daily = new Statuses;

$daily->condition("height=\"300\" width=\"300\"", "cx=\"150\" cy=\"150\" r=\"100\"", "stroke=\"black\" stroke-width=\"3\" fill=\"green\"");

$daily->print_condition();

這是功能:

<?php
function funcName()
{
if (condition) {
    echo "I want to echo the 'daily' variable here";

} else {
    echo "I want to echo the 'none' variable here";

}
}

這是svg元素:

  <div id="circles">

<svg id="green" height="300" width="300">
    <circle cx="150" cy="150" r="100" stroke="black" stroke-width="3" fill="green" />
</svg>
<br>
<svg id="orange" height="300" width="300">
    <circle cx="150" cy="150" r="100" stroke="black" stroke-width="3" fill="orange" />
</svg>
<br>
<svg id="red" height="300" width="300">
    <circle cx="150" cy="150" r="100" stroke="black" stroke-width="3" fill="red" />
</svg>

</div>

我不知道如何讓svg元素在變量中正確顯示,我不知道如何在我的函數中調用變量。

非常感謝您的建議。

您的命名慣例使您很難理解您的意圖,盡管我相信您需要以下內容。 基本上你是在努力偷工減料。

每個“狀態”都有一組變量,這些變量是svg中定義的圓。 只需編輯您的類以包含這些並重命名您的變量。

<?php

class myCircle
{

$name;
$height;
$width;
$cx;
$cy;
$r;
$stroke;
$stroke-width;
$fill;

private function _print()
{
    echo "<svg id='$this->name' height='$this->height' width='$this->width'>
    <circle cx='$this->cx' cy='$this->cy' r='$this->r' stroke='$this->stroke' stroke-width='$this->stroke-width' fill='$this->fill' />
    </svg>";
}

private function _set($name, $height, $width, $cx, $cy, $r, $stroke, $stroke-width, $fill)
{
    $this->name = $name;
    $this->height = $height;
    $this->width = $width;
    $this->cx = $cx;
    $this->cy = $cy;
    $this->r = $r;
    $this->stroke = $stroke;
    $this->stroke-width = $stroke-width;
    $this->fill = $fill;
    }

}

課程結束。

你已經省略了你的$條件究竟是什么,所以我沒有將它包含在我的答案中。 但要實現這一目標,然后使用您的課程,您只需執行以下操作:

(要么將myCircle類放在同一個php頁面上,要么包含它,然后)

<div id="circles">
<?php if($condition){
    $daily = new myCircle;
    $daily->_set("testCircle", "300", "300", "150", "150", "100", "black", 3, "green");
    $daily->_print();
  }else{
// Nothing?
} ?>
<div>

或者你的三個圈子:

<?php     
$circles = array("green", "orange" "red");    
?>

<div id="circles">

<?php 
if($condition){
for($i = 0; $i < count($circles); $i++){
    $daily = new myCircle;
    $daily->_set($circles[$i], "300", "300", "150", "150", "100", "black", 3, $circles[$i]);
    $daily->_print();
  }
} else{
// Do nothing 
}
?> 
</div>

我會做這樣的事情:(我假設您動態獲取顏色和大小變量的值。)

$size = 300;
$color = "orange";

echo '<svg id="'.$color.'" height="'.$size.'" width="'.$size.'">
    <circle cx="150" cy="150" r="100" stroke="black" stroke-width="3" fill="'.$color.'" />
</svg>';

此外,您可以將svg保存在變量中,而不是回顯,並在需要的地方使用它。

$size = 300;
$color = "orange";

$theSVG = '<svg id="'.$color.'" height="'.$size.'" width="'.$size.'">
    <circle cx="150" cy="150" r="100" stroke="black" stroke-width="3" fill="'.$color.'" />
</svg>';

然后:

<?php
function funcName()
{
if (condition) {
   echo $theSVG;

} else {
    echo "";

}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM