SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict
    Join Date
    May 2002
    Location
    Louisville, KY
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $i inside 3 for loops... not showing what $i equals. help?

    I can't figure this out. I've got a for() inside a for() inside a for(). I can't figure why I can't access the $i, $j, or $k. Here's my code. Any help is greatly appreciated!

    PHP Code:
    <?php
    // Get campuses
    $sql "SELECT * FROM campus ORDER BY c_order ASC";
    doResult($sql,1);

    $numcampuses mysql_num_rows($result);
    $i=0;
    while(
    $r=mysql_fetch_array($result)) {
        
    $c_id[$i] = $r[id];
        
    $c_name[$i] = $r[c_name];
        
    $i=$i+1;
    }


    // Get PAGES for use below
    $i=0;
    $sql "SELECT id, p_campus, p_link, p_linkTitle, p_order FROM pages ORDER BY p_order ASC";
    doResult($sql,1);
    $numpages mysql_num_rows($result);
    while(
    $r=mysql_fetch_array($result)) {
        
    $p_id[$i] = $r[id];
        
    $p_campus[$i] = $r[p_campus]; 
        
    $p_link[$i] = $r[p_link];
        
    $p_linkTitle[$i] = $r[p_linkTitle];
        
    $p_order[$i] = $r[p_order];

        
    $i=$i+1;
    }

    // Get main_links for use below
    $sql "SELECT * FROM main_links ORDER BY id ASC";
    doResult($sql,1);

    $numlinks mysql_num_rows($result); 
    $trPercent round(100 $numlinks);

    $i=0;
    while(
    $r=mysql_fetch_array($result)) {
        
    $main_linkID[$i] = $r[id];
        
    $main_link[$i] = $r[name];
        
    $i=$i+1;
    }

    // begin displaying links of pages
    $i=0;
    $j=0;
    $k=0;
    while(
    $i $numcampuses) {
        echo 
    $i;
        echo 
    "<tr><td colspan=$numrows><h2>$c_name[$i]</h2></td><tr>\n";
        
        echo 
    "<tr>\n";
        
    $menuID 1;
        
        while(
    $j $numlinks) {
        echo 
    "-$j---$i";
            echo 
    "<td width=\"$trPercent%\" style=\"border-left: 1px #CCCCCC solid; border-bottom: 1px #CCCCCC solid;\">&nbsp;\n";
            echo 
    "<a href=# style=\"text-decoration:none;\" onClick=\"return clickreturnvalue()\" onMouseover=\"dropdownmenu(this, event, 'anylinkmenu$menuID')\">$main_link[$j]</td>\n";
            echo 
    "<div id=\"anylinkmenu$menuID\" class=\"anylinkcss\">\n";
                
            while(
    $k $numpages) {
            
    #echo "-------$k--$j---$i";
                
    if ($p_campus[$k] == $c_id[$i] && $p_link[$k] == $main_linkID[$j]) {
                    echo 
    "<a href=#>$p_linkTitle[$k]</a>";
                    
    #echo "$p_campus[$k] == $c_id[$i] && $p_link[$k] == $main_linkID[$j]";
                
    }
            
    $k=$k+1;
            } 
    //end while3
            
            
            
    echo "</div>\n";
        
            
    $menuID $menuID+1;
        
    $j=$j+1;
        } 
    // end while2
        
    echo "</tr>\n";
    $i=$i+1;
    // end while1
    ?>

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what do you mean by cant access?

  3. #3
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jpfour23
    I've got a for() inside a for() inside a for().
    I think you mean while().

  4. #4
    Are You There? KDesigns's Avatar
    Join Date
    Oct 2003
    Location
    Your Monitor
    Posts
    1,146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are having troubles where your final while() starts it's because you reset all the values for $i, $j, and $k to 0. At a value of 0 your last while loop shouldn't even trigger.
    PHP Code:
    // begin displaying links of pages
    $i=0;
    $j=0;
    $k=0;
    //at 0 they are all going to be < than whatever you compare them against. 
    ChooseDaily.com - Follow on Twitter
    Top Resources for Web Designers and Developers Every Day!

  5. #5
    SitePoint Addict
    Join Date
    May 2002
    Location
    Louisville, KY
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry I did mean while()... I had it as a for() earlier.

    The $i, $j, $k are declared 0 above the code... so when they are incremented in the while() loop, inside the while() they should be $k=$k+1 right?

    Perhaps I don't understand how loops work...

    Thanks for the help.
    JP

  6. #6
    SitePoint Evangelist chiphunt1's Avatar
    Join Date
    Oct 2003
    Location
    louisville, ky
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure what you mean by "can't access" either. What do you get for these values:

    echo "campuses = " . $numcampuses ;
    echo "pages = " . $numpages;
    echo "links = " . $numlinks;
    Calm down -- it's only ones and zeroes

  7. #7
    SitePoint Addict
    Join Date
    May 2002
    Location
    Louisville, KY
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    chip..

    the output is 4, 7, 50

    by can't access... i mean the values $i, $j, $k...

    it appears that my 2nd while loop gets executed once... but the 3rd not at all... and i can't access $i or $j inside the 3rd while().

  8. #8
    SitePoint Evangelist chiphunt1's Avatar
    Join Date
    Oct 2003
    Location
    louisville, ky
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For debugging purposes can you replace this code:


    PHP Code:
    // begin displaying links of pages
    $i=0;
    $j=0;
    $k=0;
    while(
    $i $numcampuses) {
        echo 
    $i;
        echo 
    "<tr><td colspan=$numrows><h2>$c_name[$i]</h2></td><tr>\n";
        
        echo 
    "<tr>\n";
        
    $menuID 1;
        
        while(
    $j $numlinks) {
        echo 
    "-$j---$i";
            echo 
    "<td width=\"$trPercent%\" style=\"border-left: 1px #CCCCCC solid; border-bottom: 1px #CCCCCC solid;\">&nbsp;\n";
            echo 
    "<a href=# style=\"text-decoration:none;\" onClick=\"return clickreturnvalue()\" onMouseover=\"dropdownmenu(this, event, 'anylinkmenu$menuID')\">$main_link[$j]</td>\n";
            echo 
    "<div id=\"anylinkmenu$menuID\" class=\"anylinkcss\">\n";
                
            while(
    $k $numpages) {
            
    #echo "-------$k--$j---$i";
                
    if ($p_campus[$k] == $c_id[$i] && $p_link[$k] == $main_linkID[$j]) {
                    echo 
    "<a href=#>$p_linkTitle[$k]</a>";
                    
    #echo "$p_campus[$k] == $c_id[$i] && $p_link[$k] == $main_linkID[$j]";
                
    }
            
    $k=$k+1;
            } 
    //end while3
            
            
            
    echo "</div>\n";
        
            
    $menuID $menuID+1;
        
    $j=$j+1;
        } 
    // end while2
        
    echo "</tr>\n";
    $i=$i+1;
    // end while1
    ?> 
    with this code and post the output:

    PHP Code:
    // begin displaying links of pages
    $i=0;
    $j=0;
    $k=0;
    while(
    $i $numcampuses) {
        echo 
    "i = " $i "<br>";
        
        while(
    $j $numlinks) {
          echo 
    "inside j loop <br>";
          echo 
    "i = " $i "<br>";
          echo 
    "j = " $j "<br>";

          while(
    $k $numpages) {
            
                 echo 
    "inside k loop <br>";
                 echo 
    "i = " $i "<br>";
                 echo 
    "j = " $j "<br>";
                 echo 
    "k = " $k "<br>";
                
    $k=$k+1;
            } 
    //end while3
            
            
           
    $j=$j+1;
        } 
    // end while2
        
      
    $i=$i+1;
    // end while1
    ?> 
    Calm down -- it's only ones and zeroes

  9. #9
    SitePoint Evangelist chiphunt1's Avatar
    Join Date
    Oct 2003
    Location
    louisville, ky
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also it really seems to me like you need for loops instead of while loops. Don't you need to set $k back to zero upon the second execution of the J loop? k loop is only going to run for one cycle because you never set it back to zero.

    I would suggest this:

    PHP Code:
    for($i 0$i$numcampuses;$i++) {
        echo 
    "i = " $i "<br>";
        
        for(
    $j 0;$j $numlinks;$j++) {
          echo 
    "inside j loop <br>";
          echo 
    "i = " $i "<br>";
          echo 
    "j = " $j "<br>";

          for(
    $k 0;$k $numpages;$k++) {
            
                 echo 
    "inside k loop <br>";
                 echo 
    "i = " $i "<br>";
                 echo 
    "j = " $j "<br>";
                 echo 
    "k = " $k "<br>";
                
            } 
    //end while3
            
            
         
        
    // end while2
        
      
    // end while1 
    Calm down -- it's only ones and zeroes


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
  •