调用php函数()在每个图标前显示名称
Call php function () to display name before each icons
我想在图像on.png或off.png之前显示每个继电器的名称。
我有 2 页:
switch.php and functions.php
switch.php,显示每个继电器打开或关闭的图标,读取 gpio 状态。 functions.php 显示每个继电器的名称。
我不知道如何在 switch.php 中调用 r$i() 之类的函数来显示类似 :
的内容
relay0<img id='button_0' src='images/off.png' alt='off'/><br>
relay1<img id='button_1' src='images/off.png' alt='off'/>
有我的脚本:
// switch.php
<?php
$status = array(0, 0, 0, 0, 0, 0, 0);
for ($i = 0; $i < count($status); $i++) {
//set the pin's mode to output and read them
system("gpio mode ".$i." out");
exec ("gpio read ".$i, $status[$i], $return );
if ($status[$i][0] == 0 ) {
echo ("<img id='relay_".$i."' src='images/off.png' alt='off'/><br>");
}
if ($status[$i][0] == 1 ) {
echo ("<img id='relay_".$i."' src='images/on.png' alt='on'/><br>");
}
}
和
// function.php
<?php
function r0(){
echo "relay0";
}
function r1(){
echo "relay1";
}
function r2(){
echo "relay2";
}
function r3(){
echo "relay3";
}
?>
感谢您的帮助。
如果继电器的 ID 与您不需要 function.php 脚本的按钮的 ID 匹配,请尝试如下操作:
switch.php
<?php
$status = array(0, 0, 0, 0, 0, 0, 0);
for ($i = 0; $i < count($status); $i++) {
//set the pin's mode to output and read them
system("gpio mode ".$i." out");
exec ("gpio read ".$i, $status[$i], $return );
if ($status[$i][0] == 0 ) {
echo ("relay$i<img id='relay_".$i."' src='images/off.png' alt='off'/><br>");
}
if ($status[$i][0] == 1 ) {
echo ("relay$i<img id='relay_".$i."' src='images/on.png' alt='on'/><br>");
}
}
注意:当您使用双引号时,不需要用点连接变量。
希望对您有所帮助。
更新:另一种方法是使用数组,看...
<?php
$relays = array();
$relays[] = "A";
$relays[] = "B";
$relays[] = "C";
$relays[] = "D";
$status = array(0, 0, 0, 0, 0, 0, 0);
for ($i = 0; $i < count($status); $i++) {
//set the pin's mode to output and read them
system("gpio mode ".$i." out");
exec ("gpio read ".$i, $status[$i], $return );
if ($status[$i][0] == 0 ) {
echo ("{$relays[$i]}<img id='relay_".$i."' src='images/off.png' alt='off'/><br>");
}
if ($status[$i][0] == 1 ) {
echo ("{$relays[$i]}<img id='relay_".$i."' src='images/on.png' alt='on'/><br>");
}
}
假设您的 function.php
文件将来应该被替换为更多功能,您可以这样做:
function r0() {
echo 'relay0';
}
function r1() {
echo 'relay1';
}
$i = 1;
$fn = "r$i"; // string containing function name
$fn(); // this will call function 'r1'
call_user_func($fn); // this will also call 'r1'
希望对您有所帮助:)
可以使用变量函数,
$function = 'r'.$i; //set function name
$function(); //call the function
更多信息:http://php.net/manual/en/functions.variable-functions.php
我想在图像on.png或off.png之前显示每个继电器的名称。 我有 2 页:
switch.php and functions.php
switch.php,显示每个继电器打开或关闭的图标,读取 gpio 状态。 functions.php 显示每个继电器的名称。
我不知道如何在 switch.php 中调用 r$i() 之类的函数来显示类似 :
的内容relay0<img id='button_0' src='images/off.png' alt='off'/><br>
relay1<img id='button_1' src='images/off.png' alt='off'/>
有我的脚本:
// switch.php
<?php
$status = array(0, 0, 0, 0, 0, 0, 0);
for ($i = 0; $i < count($status); $i++) {
//set the pin's mode to output and read them
system("gpio mode ".$i." out");
exec ("gpio read ".$i, $status[$i], $return );
if ($status[$i][0] == 0 ) {
echo ("<img id='relay_".$i."' src='images/off.png' alt='off'/><br>");
}
if ($status[$i][0] == 1 ) {
echo ("<img id='relay_".$i."' src='images/on.png' alt='on'/><br>");
}
}
和
// function.php
<?php
function r0(){
echo "relay0";
}
function r1(){
echo "relay1";
}
function r2(){
echo "relay2";
}
function r3(){
echo "relay3";
}
?>
感谢您的帮助。
如果继电器的 ID 与您不需要 function.php 脚本的按钮的 ID 匹配,请尝试如下操作:
switch.php
<?php
$status = array(0, 0, 0, 0, 0, 0, 0);
for ($i = 0; $i < count($status); $i++) {
//set the pin's mode to output and read them
system("gpio mode ".$i." out");
exec ("gpio read ".$i, $status[$i], $return );
if ($status[$i][0] == 0 ) {
echo ("relay$i<img id='relay_".$i."' src='images/off.png' alt='off'/><br>");
}
if ($status[$i][0] == 1 ) {
echo ("relay$i<img id='relay_".$i."' src='images/on.png' alt='on'/><br>");
}
}
注意:当您使用双引号时,不需要用点连接变量。
希望对您有所帮助。
更新:另一种方法是使用数组,看...
<?php
$relays = array();
$relays[] = "A";
$relays[] = "B";
$relays[] = "C";
$relays[] = "D";
$status = array(0, 0, 0, 0, 0, 0, 0);
for ($i = 0; $i < count($status); $i++) {
//set the pin's mode to output and read them
system("gpio mode ".$i." out");
exec ("gpio read ".$i, $status[$i], $return );
if ($status[$i][0] == 0 ) {
echo ("{$relays[$i]}<img id='relay_".$i."' src='images/off.png' alt='off'/><br>");
}
if ($status[$i][0] == 1 ) {
echo ("{$relays[$i]}<img id='relay_".$i."' src='images/on.png' alt='on'/><br>");
}
}
假设您的 function.php
文件将来应该被替换为更多功能,您可以这样做:
function r0() {
echo 'relay0';
}
function r1() {
echo 'relay1';
}
$i = 1;
$fn = "r$i"; // string containing function name
$fn(); // this will call function 'r1'
call_user_func($fn); // this will also call 'r1'
希望对您有所帮助:)
可以使用变量函数,
$function = 'r'.$i; //set function name
$function(); //call the function
更多信息:http://php.net/manual/en/functions.variable-functions.php