回声<;svg>;在php函数中满足条件时类中的元素
我有一段简单的代码,结合了一些 我想做的事情如下: 我有一个php类在代码中定义了一些属性。 类-状态 “状态”变量-圆圈、大小、颜色 我想链接一个svg元素,它给出圆的大小和半径,然后是颜色 我有一个函数,当它被满足时,一个语句将需要根据满足的条件显示具有大小和颜色的类圆 下面是课堂:回声<;svg>;在php函数中满足条件时类中的元素,php,html,if,svg,Php,Html,If,Svg,我有一段简单的代码,结合了一些 我想做的事情如下: 我有一个php类在代码中定义了一些属性。 类-状态 “状态”变量-圆圈、大小、颜色 我想链接一个svg元素,它给出圆的大小和半径,然后是颜色 我有一个函数,当它被满足时,一个语句将需要根据满足的条件显示具有大小和颜色的类圆 下面是课堂: <?php class Statuses { var $circle; var $size; var $color; public function print_condition() {
<?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();
我会这样做:(我假设您动态获取颜色和大小变量的值。)
$size=300;
$color=“橙色”;
回声'
';
此外,您可以将svg保存在一个变量中,并在需要的地方使用它,而不是进行回显
$size=300;
$color=“橙色”;
$theSVG=
';
然后:
<?php
function funcName()
{
if (condition) {
echo $theSVG;
} else {
echo "";
}
}
您的命名惯例使您很难理解您的意图,尽管我相信以下是您需要的。基本上,你是在努力走捷径
每个“状态”都有一组变量,这些变量构成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;
}
}
或者你的三个圈子:
<?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>
非常感谢,这真的很有帮助。请你对我的问题的第二部分也提供一些意见。我询问如何在函数中使用变量$daily。我遇到的问题是,当我尝试在函数中使用变量时,它会说“Undefined variable$daily”,很抱歉问了一些比较新的问题,但我还是新手,并且尽我最大的努力掌握一切。我已经更新了我的答案。我希望这就是你要问的。你刚刚在我脸上挂了一个大大的微笑,非常感谢@cmprogram!很抱歉,我的命名约定让我很难理解这个问题,正如我提到的,我在编码方面还是新手,但我会在以后的文章和代码中努力纠正命名和正确的语法使用。“你帮了大忙,先生。”阿列克斯库切绝对没问题。如果你是新来的,给你的变量和类起一个很大的提示:给你的变量和类起一个你想要的名字,最好是一些值得纪念的或者对你有意义的名字。忽略那些说你应该简洁的建议,这些建议可能会在以后出现。更重要的是,你理解这个概念,以及实际发生的事情。祝你好运我在使用代码时遇到了两个问题,他们不断给出错误代码未定义的变量Sir@cmprogram。我纠正了他们,如果你想编辑你的帖子,包括修复?什么是未定义的变量?太棒了,但是非常感谢你的帮助,先生。
<?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;
}
}
<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>