2017-06-25 1 views
0

Je souhaite afficher le nom de chaque relais avant l'image on.png ou off.png. J'ai 2 pages:Appelez la fonction php() pour afficher le nom avant chaque icône

switch.php and functions.php 

switch.php, affiche des icônes ou désactiver pour chaque relais, lecture de l'état de GPIO. functions.php affiche le nom de chaque relais.

Je ne sais pas comment appeler la fonction comme r $ i(), en switch.php pour afficher quelque chose comme:

relay0<img id='button_0' src='images/off.png' alt='off'/><br> 
relay1<img id='button_1' src='images/off.png' alt='off'/> 

Il y a mes scripts:

// 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>"); 
    }  
} 

et

// function.php 
<?php 
function r0(){ 
    echo "relay0"; 
} 
function r1(){ 
    echo "relay1"; 
} 
function r2(){ 
    echo "relay2"; 
} 
function r3(){ 
    echo "relay3"; 
} 
?> 

Nous vous remercions de votre aide.

Répondre

0

En supposant que votre fichier function.php est censé être remplacé par de nouvelles fonctionnalités à l'avenir, vous pourriez faire quelque chose comme ceci:

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' 

Hope this helps :)

+0

Merci Marios. Je vais essayer ça. – Chris

0

si l'ID des relais correspondant à l'ID des boutons que vous n'avez pas besoin du script function.php, essayez quelque chose comme ceci:

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>"); 
    }  
} 

NOTE : lorsque vous utilisez des guillemets doubles n'ont pas besoin de concaténer les variables avec les points.

J'espère que cela vous aidera.

MISE À JOUR: Un autre aproche est d'utiliser un tableau, regardez ...

<?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>"); 
    } 
} 
+0

Merci Synkronice! Cela pourrait m'aider, mais chaque fonction appellera une icône différente pour chaque relais (relais0 lumière, relais1 chauffage, ...) – Chris

+0

Je vais mettre à jour ma réponse. – Synkronice