Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 2 FirstFirst 12
Results 16 to 17 of 17
  1. #16
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,378
    Thanks
    10
    Thanked 1,190 Times in 1,180 Posts
    OK, you could use one PHP script that handles all image requests ...

    if(!empty($_POST["radio"])){
    $manuel = $_POST["radio"];
    echo '<img src="circle.php?i='.$manuel.'" alt="">'."<br>";
    }

    Then, in circle.php, you GET the i variable and use it for any color. Just a suggestion.

  2. #17
    New Coder
    Join Date
    Feb 2017
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hey, I have finished storing data in the db.

    What my code does more : Store the data (name, checkboxes, radio) in a db, display the data on the first page. On the second page, using the data in the db to run the condition, and then display the image.
    I have also one button to delete the data.

    PHP Code:
    <?php
    try{
        
    //connect nagios+my db     
        
    $nagios = new PDO('mysql:host=192.168.120.87;dbname=nagios''root''');
        
    $php= new PDO('mysql:host=localhost;dbname=test''root''');

        
    $php->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
        
    $nagios->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION); 
        
    //radioarray        
        
    $radio_choices = ['Green','Yellow','Red'];
        
    //select host from nagios        
        
    $queryhost "SELECT display_name FROM nagios_hosts";
        
    $host $db->prepare($queryhost);
        
    $host->execute();
        
    $resulthost $host->fetchAll();
        
    //select service from nagios
        
    $queryservice 
        
    "SELECT display_name, service_object_id FROM nagios_services WHERE host_object_id = 
        SELECT host_object_id FROM nagios_hosts WHERE display_name = ? LIMIT 1)"
    ;
        
    $service $nagios->prepare($queryservice);
        
    ?>
            
        <form method="POST" action="#">
                 
        <input type="text" name="something" value="<?= isset($_POST['something']) ? htmlspecialchars($_POST['something']) : '' ?>"/> <br>
        <?php
        
    //radio
        
    foreach($radio_choices as $choice)
        {
            
    $chk = isset($_POST["radio"]) && $_POST["radio"] == $choice ' checked' '';
            echo 
    "<input type='radio' name='radio' value='$choice'$chk/>$choice";
        }
        
    //submit add + delete           
        
    echo' <input type="submit" name="submit" value="Add"> <br>'
        echo
    ' <input type="submit" name="delete" value="Delete"> <br>'
        
    //select + display result from my db            
        
    $sql1 "SELECT name,checkbox,radio FROM artists";
        
    $test $conn->prepare($sql1);
        
    $test->execute();
        
    $result $test->fetchAll();
                    
        foreach(
    $result as $row1){
            echo 
    "<b>".$row1['name']."</b>"."<br>";
            if (
    $row1['radio'] != NULL){
                echo 
    $row1['radio']."<br>";
            }
            if ((
    $row1['radio'] == NULL) && $row1['checkbox'] != NULL){
                echo 
    $row1['checkbox']."<br>";
            }
        }
        
    //storing the data into my db.            
        
    if(isset($_POST['submit'])){                                           
            
    $sql2 "CREATE TABLE IF NOT EXISTS ARTISTS (
                    ID int(11) AUTO_INCREMENT PRIMARY KEY,
                    name varchar(30) NOT NULL,
                    checkbox varchar(100),
                    radio varchar(30))"
    ;

            
    $conn->exec($sql2);                                  
            
    //checkboxes
            
    $checkbox $_POST['list'];
            
    $checkboxes '';
            for (
    $i=0$i<count($checkbox); $i++)
            {
                
    $checkboxes.= $checkbox[$i];
                
    $checkboxes .= ', ';                                                                     
            }
            
    //name(input text)
            
    if(isset($_POST['something'])){
                
    $name $_POST['something'];}
            
    //radio        
            
    if(isset($_POST['radio'])){
                
    $radio $_POST['radio'];}                                                                       
                
    //insert
            
    $sql3 "INSERT INTO `artists` (`name`,`checkbox`,`radio`) VALUES (:name,:checkbox,:radio)";
            
    $insert $conn->prepare($sql3);
            
    $insert->execute([":name"=>$name":checkbox"=>$checkboxes":radio"=>$radio]);
        } 
        
    //delete
        
    if(isset($_POST['delete'], $_POST['something'])){             
            
    $sql4 $conn->prepare("DELETE FROM artists WHERE name = (:name)");
            
    $sql4->bindParam(':name'$_POST['something']);
            
    $name $_POST['something'];
            
    $sql4 -> execute();                        
        }
        
    //display all service with a checkboxes
        
    foreach ($resulthost as $row2 )
        {                            
            
    $service->execute([$row2['display_name']]);
            
    $resultservice $service->fetchAll();
                        
            echo 
    $row2['display_name']."<br>";

            foreach (
    $resultservice as $row)
            { 
                
    //checkboxes                        
                
    echo '<input type="checkbox" name="list[]" value='.$row2['service_object_id'].'>';
                echo 
    $row2['display_name']."<br>";   
            }
          
        }
    ?>                
        </form>
        <?php
    }
    catch(
    Exception $e)
    {
        echo 
    "Échec : " $e->getMessage();
    }

    //SECOND PAGE ?>

    <?php
    try{
        
    //connect
        
    $nagios = new PDO('mysql:host=192.168.120.87;dbname=nagios''root''');
        
    $php= new PDO('mysql:host=localhost;dbname=test''root''');

        
    $php->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
        
    $nagios->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION); 
                
        
    $sql "SELECT name,checkbox,radio FROM artists";
        
    $test $php->prepare($sql);
        
    $test->execute();
        
    $result $test->fetchAll();

         
    //condition using the data stored in my db instead.                           
        
    foreach ($result as $row1 ){                                 
            echo 
    $row1['name']."<br>";
            if (
    $row1['radio'] != NULL){
                if(
    $row1['radio'] == 'Vert') echo '<img src="Green.php" alt="">'."<br>";
                if(
    $row1['radio'] == 'Orange') echo '<img src="Yellow.php" alt="">'."<br>";
                if(
    $row1['radio'] == 'Rouge') echo '<img src="Red.php" alt="">'."<br>";
            }
                    
            if ((
    $row1['radio'] == NULL) && $row1['checkbox'] != NULL){
                
    $ids $row1['checkbox'];
                
    $idQue implode(", ",array_fill(0,count($ids),"?"));

                
    $query "SELECT service_object_id,current_state FROM nagios_servicestatus WHERE service_object_id IN (".$idQue.")";
                                 
                
    $select $nagios->prepare($query);
                
    $select->execute([$ids]);
                
    $results $select->fetchall();
                                
                
    $containsOne false;
                
    $containsTwo false;
                
    // first loop over all results to check the content of the array
                
    foreach($results as $row2){
                    if (
    $row2['current_state'] == 1){
                        
    $containsOne true;
                    }              
                    if(
    $row2['current_state'] == 2){
                        
    $containsTwo true;
                    }
                }    
                        
    // once the loop is done, check the results and echo the correct message
                
    if (!$containsOne && ! $containsTwo) echo '<img src="Green.php" alt="l">'.'<br>';
                if (
    $containsOne  && ! $containsTwo) echo '<img src="Yellow.php" alt="">'.'<br>';
                if (
    $containsTwo) echo '<img src="Red.php" alt="">'.'<br>';                    
            }                                 
        }            
    }
    catch(
    Exception $e)
    {
        echo 
    "Échec : " $e->getMessage();
    }
    ?>
    I still need to add more feature but this is the most of it.
    I know my code probably sucks, but it works how I want.
    What do you think ?
    Last edited by xarj; Mar 6th, 2017 at 04:59 PM.


 
Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •