ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

PHP TO RPG CONVERSION - What is a subfile in PHP?

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

  • PHP TO RPG CONVERSION - What is a subfile in PHP?

    The most difficult thing I find about venturing into PHP (being in RPG for 15 years) is what is the translation.

    For example my Assembly file has 3 Jobs and percents (or of course, any assembly could have any number of jobs):

    Code:
     
    [FONT=Courier New][SIZE=2]Assembly Job   Percent[/SIZE][/FONT]
    [FONT=Courier New][SIZE=2]A1020    11020 20[/SIZE][/FONT]
    [FONT=Courier New][SIZE=2]A1020    11030 40[/SIZE][/FONT]
    [FONT=Courier New][SIZE=2]A1020    11060 40[/SIZE][/FONT]
    I connect and show in php like this:
    Code:
    while(db2_fetch_row($queryexe)) 
    {
    if($haverecs=='false'){print ("<b>The $assembly assembly is assigned to the following-must equal 100%:</b>\n");
    print("<br></br>");}
    $haverecs='true';
    $jobasy = db2_result($queryexe, 'TMASSY');
    $jobnum = db2_result($queryexe, 'TMJOBNO');
    $jobper = db2_result($queryexe, 'TMPCT');
    $total = $total + $jobper;
    $n++;
    $alljobs[$n] = $jobnum;
    $allpcts[$n] = $jobper;
    //Put the results in an HTML table.
    print("<tr bgcolor=#ffffff>\n");
    print("Job: ");
    print("<td>$jobnum</td>\n");
    print("<td>&nbsp&nbsp&nbsp</td>\n");
    print("<input type='text' value=$jobper name='jobper' id='jobper' size=3>\n");
    print("%");
    print("<br></br>");
    print("</tr>\n");
    }
    As you can see the percent is in an input field and the user can change them (subfile to me).

    How do I get the new values back to process them and update the file? I am trying to store the originals in an array, but now what?

    Tammy Bond

  • #2
    Re: PHP TO RPG CONVERSION - What is a subfile in PHP?

    I just posted the links to all of the PHP articles I have published with System i News here: http://forums.systeminetwork.com/isn...061#post534061

    Part 2 covers browser input, and parts 4 and 5 database access and "Subfile" type output.

    Comment


    • #3
      Re: PHP TO RPG CONVERSION - What is a subfile in PHP?

      Originally posted by JonBoy View Post
      I just posted the links to all of the PHP articles I have published with System i News here: http://forums.systeminetwork.com/isn...061#post534061

      Part 2 covers browser input, and parts 4 and 5 database access and "Subfile" type output.
      Thanks so much for the post. I did review Part 2, 4 and 5. I do no see that people are writing variable lines of data to input fields, and reading them back for update. All of the examples are single input fields with a specific field name.

      I am stuck because in order to search the world over for a solution, you need to know what you are looking for. That is why I came to this site.

      If this were a RPG subfile, in each subfile record I would write hidden fields to store my original answers, populate an i/o field with the default value from the file, and finally on enter I would start at the first record in the subfile, read through and compare the hidden to the i/o fields and update the file. What is the equivalent in PHP? Is this a table? Is this a row? Help I'm on a deadline.

      Comment


      • #4
        Re: PHP TO RPG CONVERSION - What is a subfile in PHP?

        I could have sworn I had covered this in the series but maybe my brain is going or it is in Part 6 or ...

        Anyway. There are a couple of ways I would handle it but probably the simplest would be to name the fields like so fieldName[] - that way PHP will treat them as an array when the input is processed. In order to associate the value with the appropriate key value you'd probably want to include a hidden field in each row with the correct value named like so keyField[] then all you have to do is loop through the array and do the processing.

        Comment


        • #5
          Re: PHP TO RPG CONVERSION - What is a subfile in PHP?

          Originally posted by JonBoy View Post
          I could have sworn I had covered this in the series but maybe my brain is going or it is in Part 6 or ...

          Anyway. There are a couple of ways I would handle it but probably the simplest would be to name the fields like so fieldName[] - that way PHP will treat them as an array when the input is processed. In order to associate the value with the appropriate key value you'd probably want to include a hidden field in each row with the correct value named like so keyField[] then all you have to do is loop through the array and do the processing.
          I actually tried to add the [] to the input field and I couldn't get the syntax right. I couldn't find any examples online.

          Could you take my code and illustrate it?

          Then on Submit, I read through? Is it an array? Do you predefine it as an array like I did? I keep the original values in an array, but it doesn't really matter what the originals are if I always update all of them. So I need to prefill them from the file, allow changes, and update the file.

          Comment


          • #6
            Re: PHP TO RPG CONVERSION - What is a subfile in PHP?

            I don't wish to sound rude - but have you really read the articles? I just checked and part 2 contains an example of using the array technique I described. You can find the code here: http://systeminetwork.com/files/64526-Fig4.txt the array in question is languages[] - the article itself describes how it works.

            In your example it would be something like this:
            PHP Code:
            print("Job: ");
            print(
            "<td>$jobnum <input type='hidden' value=$jobnum name='jobnum[]'></td>\n");
            print(
            "<td>&nbsp&nbsp&nbsp</td>\n");
            print(
            "<input type='text' value=$jobper name='jobper[]' id='jobper' size=3>\n"); 
            See the example for getting the array size, of either jobnum or jobper (they will have the same number of entries). Then instead of foreach, loop through the values using a for loop. You might want to consider keeping the original values in a session variable so you could check which ones were actually changed to reduce the number of updates.

            Comment


            • #7
              Re: PHP TO RPG CONVERSION - What is a subfile in PHP?

              Originally posted by JonBoy View Post
              I don't wish to sound rude - but have you really read the articles? I just checked and part 2 contains an example of using the array technique I described. You can find the code here: http://systeminetwork.com/files/64526-Fig4.txt the array in question is languages[] - the article itself describes how it works.

              In your example it would be something like this:
              PHP Code:
              print("Job: ");
              print(
              "<td>$jobnum <input type='hidden' value=$jobnum name='jobnum[]'></td>\n");
              print(
              "<td>&nbsp&nbsp&nbsp</td>\n");
              print(
              "<input type='text' value=$jobper name='jobper[]' id='jobper' size=3>\n"); 
              See the example for getting the array size, of either jobnum or jobper (they will have the same number of entries). Then instead of foreach, loop through the values using a for loop. You might want to consider keeping the original values in a session variable so you could check which ones were actually changed to reduce the number of updates.
              No, you are not rude, I really appreciate your persistence. When I reviewed part 2, it was a 1 box with a list of items. As a novice, I did not think that meant the same thing as writing out 3 different rows with 3 different input fields. That is why I was confused, when I was looking for a sample of input text.

              Comment


              • #8
                Re: PHP TO RPG CONVERSION - What is a subfile in PHP?

                Sorry - I am getting two threads mixed up - I was thinking that you already had web experience with CGIDEV2 and were translating that skill to php. In other words the browser input stuff would be familiar to you. I went back over this thread and see that is not you. Mea culpa. If I had realized that I would have pointed out that the two (multi-selection list and multiple fields with the same name) came to the same thing.

                PHP allows you to use the [ ] naming convention to automagically create an array from the input. In some cases, you might also find the ability to actually code the subscript useful. For example to group all the input fields for an address together you might name the fields address[street1], address[city] and so on. Then you would subsequently be able to access the address data (and perhaps pass it as a parm) as the array $address.

                If you have any more problems please post again.

                Comment


                • #9
                  Re: PHP TO RPG CONVERSION - What is a subfile in PHP?

                  Originally posted by JonBoy View Post
                  Sorry - I am getting two threads mixed up - I was thinking that you already had web experience with CGIDEV2 and were translating that skill to php. In other words the browser input stuff would be familiar to you. I went back over this thread and see that is not you. Mea culpa. If I had realized that I would have pointed out that the two (multi-selection list and multiple fields with the same name) came to the same thing.

                  PHP allows you to use the [ ] naming convention to automagically create an array from the input. In some cases, you might also find the ability to actually code the subscript useful. For example to group all the input fields for an address together you might name the fields address[street1], address[city] and so on. Then you would subsequently be able to access the address data (and perhaps pass it as a parm) as the array $address.

                  If you have any more problems please post again.
                  I have added your changes, now I am getting undefined index. The Major difference in your example and mine, is the data in the file defaulting into the input fields, and allowing the user to change before the submit. Yours loads hard coded values. Please also review the differences in the GET from your example and the POST. I have a mixture and not sure what to do with those.

                  PHP Code:
                   while(db2_fetch_row($queryexe)) 
                  {
                   if($haverecs=='false'){
                  print ("<b>The $assembly assembly is assigned to the following-must equal 100%:</b>\n");
                  print("<br></br>");}
                   $haverecs='true';
                   $jobasy = db2_result($queryexe, 'TMASSY');
                   $jobnum = db2_result($queryexe, 'TMJOBNO');
                   $jobper = db2_result($queryexe, 'TMPCT');
                   $total = $total + $jobper;
                  //Put the results in an HTML table.
                  print("<tr bgcolor=#ffffff>\n");
                  print("Job: ");
                  print("<td>$jobnum <input type='hidden' value=$jobnum name='jobnum[]'></td>\n");
                  print("<td>&nbsp&nbsp&nbsp</td>\n");
                  print("<input type='text' value=$jobper name='jobper[]' id='jobper' size=3>\n");  
                  print("%");
                  print("<br></br>");
                  print("</tr>\n");
                  }
                  // No records are found yet
                  if ($haverecs == 'false'){
                  print("<b>Assign values to job assembly $assembly -must equal 100%:</b>\n");
                  print("<br></br>");
                  print("<b>no records found</b>\n");
                  }
                  print("Assembly total: $total %");
                  }
                  if (!empty($_POST['submit'])) {
                  $jobsUsed = count($_GET['jobper']);
                  $jobsList = $_GET['jobper'];
                  foreach ($jobsList as $jobper) {
                      echo '$jobper';
                  }
                  }
                  ?>
                  <form action="<?php echo $_SERVER['PHP_SELF'];?>" method='post'>
                  <input type="submit" value="submit" name="submit">
                  Last edited by FaStOnE; August 26, 2010, 08:45 AM.

                  Comment


                  • #10
                    Re: PHP TO RPG CONVERSION - What is a subfile in PHP?

                    You are mixing POST and GET. If the form method is post then you should retrieve the data from $_POST. There are circumstances where both get and post data can co-exist - but this isn't one of them. Just be consistent.

                    Comment


                    • #11
                      Re: PHP TO RPG CONVERSION - What is a subfile in PHP?

                      Originally posted by JonBoy View Post
                      You are mixing POST and GET. If the form method is post then you should retrieve the data from $_POST. There are circumstances where both get and post data can co-exist - but this isn't one of them. Just be consistent.
                      What did you think of the undefined index error? When the [] was added to jobper, it has not ever worked. Syntax?

                      Comment


                      • #12
                        Re: PHP TO RPG CONVERSION - What is a subfile in PHP?

                        Originally posted by TBOND View Post
                        What did you think of the undefined index error? When the [] was added to jobper, it has not ever worked. Syntax?
                        Have you corrected the get/post thing and tried? The way it was the array would have always been empty and that could have caused a problem depending on your settings.

                        One thing I noticed was that you are adding rows with input fields to a form that doesn't yet exist. I think some browsers can deal with this but I'm not sure. Any input fields etc. should be within the scope of the FORM. So I would also try moving that to the top of your code.

                        Comment

                        Working...
                        X