SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Looping issue

  1. #1
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Looping issue

    I having some trouble tour with an outer while loop.
    The outer loop only runs once when it should run twice since the table (history) has 2 entries in it. However, when run by itself, w/out the two inner while loops. Any help here would be greatly appreciated.

    PHP Code:
    <?php
    set_time_limit
    (1000);
    //!.  Open Database Connection.   //!.
    //!.   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .!//                                                                      
        
    $db mysql_connect("localhost""root""xxxxxx") or die("Could not connect.");
        if(!
    $db
            die(
    "no db");
        if(!
    mysql_select_db("ICMPmon_TEST",$db))
             die(
    "No database selected.");
    //!. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .!//
    //!. Get node_id from history table   .!//
    $result mysql_query("SELECT DISTINCT node_id FROM history");
    while(
    $row mysql_fetch_array($result))
        {
            
    $node_id $row['node_id'];
            echo 
    $node_id "<br>";

    //!. Get sysname and ip_address from node table where node_id = $node_id   .!//
            
    $result mysql_query("SELECT * FROM node WHERE id = $node_id");
            while(
    $row mysql_fetch_array($result))
            {
                
    $sysname $row['sysname'];
                
    $ip_address $row['ip_address'];
                echo 
    "<b>" $sysname "&nbsp;&nbsp;&nbsp;&nbsp;" $ip_address "</b><br>";
            }        
    //!. Get up and down times from the history table where node_id = $node_id   .!//    
                    
    echo "<table class=borders  width=600px cellspacing =2 pading = 2>";
                    echo 
    "<tr bgcolor = #000000 font-color=#ffffff>";
                    echo 
    "<th><font color=#ffffff>Down</font></th><th><font color=#ffffff>Up</font></th>";
    //!. Set row colors & set row count.   .!//
                       
    $color1 "#52CBF5"
                       
    $color2 "#CCCCCC";
                       
    $row_count 0
                    
    $result mysql_query("SELECT * FROM history WHERE node_id = $node_id");
                    while(
    $row mysql_fetch_array($result))
                    {
                        
    $down_at    $row['down_at'];
                            
    $down_atdate("M-d-y | H:i:s ",$down_at);
                        
    $up_at $row['up_at'];
                            
    $up_at date("M-d-y | H:i:s ",$up_at);
                        
    $row_color = ($row_count 2) ? $color1 $color2;
                        echo 
    "<tr bgcolor=" $row_color .">";
                        echo 
    "<td>" $down_at "</td><td>" $up_at "</td>";
                        echo 
    "</tr>";
                    }
                    echo 
    "</table><br>";
        }
    //!. Close Database connecton   .!//
    mysql_close($db);
    ?>

  2. #2
    SitePoint Member
    Join Date
    Jul 2009
    Location
    Devon, UK
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Depends on the structure of your table "history" and what node_id represents.

    You're using a WHERE clause based on this value and if only one of those records matches then only one will be given.

  3. #3
    SitePoint Member
    Join Date
    Jul 2009
    Location
    Devon, UK
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just noticed you're using depreciated HTML...
    PHP Code:
    echo "<tr bgcolor=" $row_color .">"
    Might be better off using this:
    PHP Code:
    echo '<tr style="background-color:'.$row_color.'">'

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2009
    Location
    Maidenhead, UK
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you reuse the $result variable.... use $result1 and $result2 for the inner loops...
    -------------------------------------------------------
    UK postcode->address lookup : www.CraftyClicks.co.uk
    a piece of my brain : adam.stylo.name (at your own risk)
    -------------------------------------------------------


Bookmarks

Posting Permissions

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