ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

The do loop

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • The do loop

    I'm just testing the waters here...If anyone interested in more let me know
    please.




    simple do/for loop



    The Do
    PHP Code:
      dow count 11;

      
    // in this do loop we will be writing 10 records in 
      // a subfile

       
    mysubfilefield1 'test data';
       
    RRN+=1;   
       
    write SUB01;

       
    count +=1;
      
    enddo
    The For
    PHP Code:
      for count 1 to 10;

      
    // in this do loop we will be writing 10 records in 
      // a subfile

       
    mysubfilefield1 'test data';
       
    RRN+=1;   
       
    write SUB01;
       
      
    enddo
    PHP
    PHP Code:
     <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <html>
    <head>
        <title>Untitled</title>
    </head>

    <body>
     <?

      // same comment id "//" as free RPG
      // this will write a header line 6 times
      // because there are only 6 header sizes <h1>,<h2>,<h3>,<h4>,<h5>,<h6>
      // notice the "." after the "This is header" this is for concatenating of 
      // the test and the variable counter..(Variables are identified by the "$") 
      // see attached image for output

      // there are three "expressions in the for loop
      // 1= initialization expression
      // 2= conditional expression (in our case do while less than 7)
      // 3= re-initialize expression we used $counter++ 
      // same as ($counter = $counter + 1) 
      // to decrement we would have said:
      //  for ($counter = 6; $counter > 0  ; $counter--)
      // you can leave out the conditional and the PHP loop would just run & run & run & run
      // you get the point.... ;) 
         
       for ($counter = 1; $counter < 7; $counter++)
        {
           $number++;
           print "<h$counter>";
           print "This is header" .$number . "<br>" ;
           print "</h$counter>";    
        } 

      ?>


    </body>
    </html>
    Attached Files
    Last edited by jamief; March 10, 2007, 11:08 PM. Reason: adding image
    All my answers were extracted from the "Big Dummy's Guide to the As400"
    and I take no responsibility for any of them.

    www.code400.com

  • #2
    Re: The do loop

    Originally posted by jamief View Post
    PHP
    for ($counter = 1; $counter < 7; $counter++)
    {
    $number++;
    print "<h$counter>";
    print "This is header" .$number . "<br>" ;
    print "</h$counter>";
    }
    These three statements all function the same:

    PHP Code:
    print "This is header$number<br>" 
    PHP Code:
    print "This is header" .$number "<br>" 
    PHP Code:
    print 'This is header' .$number '<br>' 
    When a string is enclosed in double quotes then you can directly add variables (eg $number) and it will interpret their values into the text string without having to concatenate them with the period "."

    To include double quotes in a string you can enclose it in single quotes
    eg
    PHP Code:
    print 'This is my "Example"'
    GC
    Greg Craill: "Life's hard - Get a helmet !!"

    Comment


    • #3
      Re: The do loop - extended ...

      Assuming we have established a connection to our database (in this example I use MySql) then we can use the Do loop to read through multiple selects from the DB.

      PHP Code:

      <?
      $cntr = 0;
      $SQL_String1 = "SELECT Fld1, Fld2, Fld3 FROM Table1 WHERE Fld1 < 10";
      $Do_SQL1 = mysql_query($SQL_String1);
      if ($Get_Array1 = mysql_fetch_array($Do_SQL1)) {
        do {
          $Fld1_Val = $Get_Array1["Fld1"];
          $Fld2_Val = $Get_Array1["Fld2"];
          $Fld3_Val = $Get_Array1["Fld3"];
          $cntr ++;
          Print "Row $cntr values are:  Fld1=[$Fld1_Val] Fld2=[$Fld2_Val] Fld3=[$Fld3_Val]<br>"; 
          ...
            Or do other processing actions here
          ...
        } while ($Get_Array1= mysql_fetch_array($Do_SQL1);
      }
      ?>
      GC
      Last edited by gcraill; March 11, 2007, 03:01 AM. Reason: format
      Greg Craill: "Life's hard - Get a helmet !!"

      Comment


      • #4
        using the PHP while statement (Dow-RPG)

        Thanks greg......I will post additional info on database processing later..including connection strings and building/using MYSql table.

        PHP Code:
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

        <html>
        <head>
            <title>dow(RPG) - while PHP</title>
        </head>

        <body>
        <table width="30%" border="1">
        <?
         //create a variable for the sum initilize 
         $sum = 0;
         $multiplicand = 2;
         $multiplier = 2;
         
         // the loop will create table rows in our table
         // with the sum of out multiplicand * multiplier
         // see attached image for output
         
          while ($sum <= 1024)
         {
          print "<tr><Td>" .  $multiplicand ." multiplied by " . $multiplier . "</td>"; 
          $sum = $multiplicand * $multiplier;
          $multiplicand = $sum;
          print "<td>$sum</td></tr>";
          }
        ?>
        </table>


        </body>
        </html>
        Attached Files
        All my answers were extracted from the "Big Dummy's Guide to the As400"
        and I take no responsibility for any of them.

        www.code400.com

        Comment


        • #5
          Re: The do loop (alternating table colors)

          Okay - I know its ugly its just an example
          just for fun key this in and look at the page source......
          this is still small enough to get a good idea without going blind.

          Once we start loading large tables on the screen all written
          by php, the page source will become very hard to read.



          PHP Code:
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

          <html>
          <head>
              <title>dow(RPG) - while PHP</title>
          </head>

          <body>
          <table width="30%" border="1">
          <?
           //create a variable for the sum initilize 
           $sum = 0;
           $multiplicand = 2;
           $multiplier = 2;
           $myswitch= "0";
           $mystring = "";
           
            // the loop will create table rows in our table
           // with the sum of out multiplicand * multiplier
           // see attached image for output
           
            while ($sum <= 1024)
           {
             print "<tr>";
           
            // ive added a section to make the table row colors alternate
            // normally I would use CSS for this and define everything there
            // that way you just define a class variable and us it
            // but for this example you get what you paid for ;)
           
            // the double "==" is used to check value of variable myswitch
            
             if ($myswitch == "0") 
            {
              $myswitch = "1";
              $mystring =  " <td bgcolor=\"#FFFFFF\" > ";  
            }
             else
            {
              $myswitch = "0";
              $mystring =  " <td bgcolor=\"#A0A0A0\" > ";  
            }
           
            print $mystring . $multiplicand ." multiplied by " . $multiplier . "</td>"; 
            
            $sum = $multiplicand * $multiplier;
            $multiplicand = $sum;
            
            print   $mystring . $sum . "</td></tr>";
            
            }
          ?>
          </table>
          </body>
          </html>
          Attached Files
          All my answers were extracted from the "Big Dummy's Guide to the As400"
          and I take no responsibility for any of them.

          www.code400.com

          Comment

          Working...
          X