[PHP] nested for loops

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default Re: [PHP] nested for loops

    your syntax is correct, just need to change for ($j=0: $j < 5; $j++) to
    for ($j=0; $j < 5; $j++)
    >>> "Micah Montoy" <montmica@isu.edu> 07/09/03 12:56PM >>>
    Anyone ever do a nested for loop?

    $i =0;
    $j =0;
    for ($x=0; $x < 50; $x++){
    echo ("1 to 50");

    for ($j=0: $j < 5; $j++) {
    echo ("less than 5");
    }
    }



    Matt Matijevich Guest

  2. Similar Questions and Discussions

    1. Nested loops?
      Hi, I've been adapting a script which parses HTML and creates an RSS feed (http://www.perl.com/lpt/a/2001/11/15/creatingrss.html). Now I want...
    2. HTML::Template arbitraryily nested recursive loops
      Can anyone suggest a way to display an arbitrarily deep nested loop structure using HTML::Template? Below is what I've tried. I have a data...
    3. Possible Bug w/ nested loops and queries
      May have found a possible bug... can someone please verify? In a database I have two tables, temp1 and temp2 These tables can contain anything,...
    4. Using 'my' within nested loops
      I am curious about the amount of overhead created with the first example below as compared to the second example below: Example 1: my $this;...
    5. nested for loops
      Anyone ever do a nested for loop? $i =0; $j =0; for ($x=0; $x < 50; $x++){ echo ("1 to 50"); for ($j=0: $j < 5; $j++) { echo ("less than...
  3. #2

    Default Re: [PHP] nested for loops

    I did it similar but calling some variables and it would never work. I'll
    give it a shot again and see what happens and if same thing, I'll post it.

    thanks


    "Matt Matijevich" <matijevich@alliancetechnologies.net> wrote in message
    news:sf0c1185.079@nw0102dsm.alliancetechnologies.n et...
    > your syntax is correct, just need to change for ($j=0: $j < 5; $j++) to
    > for ($j=0; $j < 5; $j++)
    >
    > >>> "Micah Montoy" <montmica@isu.edu> 07/09/03 12:56PM >>>
    > Anyone ever do a nested for loop?
    >
    > $i =0;
    > $j =0;
    > for ($x=0; $x < 50; $x++){
    > echo ("1 to 50");
    >
    > for ($j=0: $j < 5; $j++) {
    > echo ("less than 5");
    > }
    > }
    >
    >
    >

    Micah Montoy Guest

  4. #3

    Default RE: [PHP] nested for loops

    Hi,

    I did one once. It was similar to what you typed out here. Are you having
    troubles with this?

    -Dan Joseph
    > -----Original Message-----
    > From: Micah Montoy [mailto:montmica@isu.edu]
    > Sent: Wednesday, July 09, 2003 1:56 PM
    > To: [email]php-general@lists.php.net[/email]
    > Subject: [PHP] nested for loops
    >
    >
    > Anyone ever do a nested for loop?
    >
    > $i =0;
    > $j =0;
    > for ($x=0; $x < 50; $x++){
    > echo ("1 to 50");
    >
    > for ($j=0: $j < 5; $j++) {
    > echo ("less than 5");
    > }
    > }
    >
    > I haven't seen a nested loop in PHP, so I'm not sure of the exact syntax.
    >
    > thanks
    >
    >
    >
    > --
    > PHP General Mailing List (http://www.php.net/)
    > To unsubscribe, visit: http://www.php.net/unsub.php
    >
    Dan Joseph Guest

  5. #4

    Default Re: [PHP] nested for loops

    Well, the for loop is now working but its not displaying what I want. I am
    pulling data from a DB and for every new <td> I would like to get the next
    result and if it reaches more then 2 columns, to start a new row. This I am
    not initally having a problem with. That is the looping. The problem I am
    running into, is getting the the DB stuff to show. Right now, I only one
    record and it shows for every single one. Here is a snippet of the code.

    $result = mssql_query("SELECT * FROM files WHERE file_id = '$cat_name' ORDER
    BY file_name ASC");

    $i = $col_num;
    $j = 0;

    $filename= mssql_result($result,0,"file_name");
    $fileID = mssql_result($result,0,"file_id");

    for ($k = 0; $k < mssql_num_rows($result); $k++) {
    //changes row color
    if (fmod($k, 2)){$rowColor = "$oddcolor";}else{$rowColor =
    "$evencolor";}

    echo ("<TR bgcolor=$rowColor>");

    for ($j = 0; $j < $i; $j++) {
    echo ("<td><a href=index.php&ID=$fileID_id>$filename</a>".
    "Name: <strong>$getimage_name</strong>".
    "<a href='index.php&ID=$fileID">Delete</a></td>");
    }
    echo ("</TR>");
    }


    Anyone see what I need to do to make it loop through and pull out the next
    file name. I thought about a foreach but it never wanted to work with the
    above.

    thanks


    "Matt Matijevich" <matijevich@alliancetechnologies.net> wrote in message
    news:sf0c1185.079@nw0102dsm.alliancetechnologies.n et...
    > your syntax is correct, just need to change for ($j=0: $j < 5; $j++) to
    > for ($j=0; $j < 5; $j++)
    >
    > >>> "Micah Montoy" <montmica@isu.edu> 07/09/03 12:56PM >>>
    > Anyone ever do a nested for loop?
    >
    > $i =0;
    > $j =0;
    > for ($x=0; $x < 50; $x++){
    > echo ("1 to 50");
    >
    > for ($j=0: $j < 5; $j++) {
    > echo ("less than 5");
    > }
    > }
    >
    >
    >

    Micah Montoy Guest

  6. #5

    Default Re: [PHP] nested for loops

    <snip>
    $result = mssql_query("SELECT * FROM files WHERE file_id = '$cat_name'
    ORDER
    BY file_name ASC");

    $i = $col_num;
    $j = 0;

    $filename= mssql_result($result,0,"file_name");
    $fileID = mssql_result($result,0,"file_id");

    for ($k = 0; $k < mssql_num_rows($result); $k++) {
    //changes row color
    if (fmod($k, 2)){$rowColor = "$oddcolor";}else{$rowColor =
    "$evencolor";}

    echo ("<TR bgcolor=$rowColor>");

    for ($j = 0; $j < $i; $j++) {
    echo ("<td><a href=index.php&ID=$fileID_id>$filename</a>".
    "Name: <strong>$getimage_name</strong>".
    "<a href='index.php&ID=$fileID">Delete</a></td>");
    }
    echo ("</TR>");
    }

    </snip>


    Unless I am misuderstainding what you want to do, you need to reassign
    $filename and $fileID_id variables in your loop somewhere using the
    mssql_result function. The way the loop is working now they will be the
    same throught the entire loop.
    Matt Matijevich Guest

  7. #6

    Default Re: [PHP] nested for loops

    You're getting the data from your query outside of your loop. That needs to
    be part of your loop, otherwise, as you've seen, it just displays the first
    record. You could try something like this:

    $cnt = 0;
    while ($row = mysql_fetch_object($result))
    {
    echo "<td>$row->file_name</td><td>$row->file_id</td>";
    $cnt++;
    if ($cnt % 2)
    echo "</tr><tr>";
    }

    HTH.

    -- Rob



    "Micah Montoy" <montmica@isu.edu> wrote in message
    news:20030709190208.75714.qmail@pb1.pair.com...
    > Well, the for loop is now working but its not displaying what I want. I
    am
    > pulling data from a DB and for every new <td> I would like to get the next
    > result and if it reaches more then 2 columns, to start a new row. This I
    am
    > not initally having a problem with. That is the looping. The problem I
    am
    > running into, is getting the the DB stuff to show. Right now, I only one
    > record and it shows for every single one. Here is a snippet of the code.
    >
    > $result = mssql_query("SELECT * FROM files WHERE file_id = '$cat_name'
    ORDER
    > BY file_name ASC");
    >
    > $i = $col_num;
    > $j = 0;
    >
    > $filename= mssql_result($result,0,"file_name");
    > $fileID = mssql_result($result,0,"file_id");
    >
    > for ($k = 0; $k < mssql_num_rows($result); $k++) {
    > //changes row color
    > if (fmod($k, 2)){$rowColor = "$oddcolor";}else{$rowColor =
    > "$evencolor";}
    >
    > echo ("<TR bgcolor=$rowColor>");
    >
    > for ($j = 0; $j < $i; $j++) {
    > echo ("<td><a href=index.php&ID=$fileID_id>$filename</a>".
    > "Name: <strong>$getimage_name</strong>".
    > "<a href='index.php&ID=$fileID">Delete</a></td>");
    > }
    > echo ("</TR>");
    > }
    >
    >
    > Anyone see what I need to do to make it loop through and pull out the next
    > file name. I thought about a foreach but it never wanted to work with the
    > above.
    >
    > thanks
    >
    >
    > "Matt Matijevich" <matijevich@alliancetechnologies.net> wrote in message
    > news:sf0c1185.079@nw0102dsm.alliancetechnologies.n et...
    > > your syntax is correct, just need to change for ($j=0: $j < 5; $j++) to
    > > for ($j=0; $j < 5; $j++)
    > >
    > > >>> "Micah Montoy" <montmica@isu.edu> 07/09/03 12:56PM >>>
    > > Anyone ever do a nested for loop?
    > >
    > > $i =0;
    > > $j =0;
    > > for ($x=0; $x < 50; $x++){
    > > echo ("1 to 50");
    > >
    > > for ($j=0: $j < 5; $j++) {
    > > echo ("less than 5");
    > > }
    > > }
    > >
    > >
    > >
    >
    >

    Rob Adams Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139