Greetings.

I have minimal (but growing) skills in programming in any language, and apologize in advance if this has been answered recently (several pages of "array" searches on Google and this forum haven't answered the question).

As an applied test, I decided to populate an HTML table with data from a PHP associative array, and have successfully done so using the FOREACH structures.

The data are a customer name, an ID number, and a ranking number. The array is sorted on any of the three fields, and fed to an HTML table, with the appropriate variables in each location.

While this seems to work fine for a table consisting of single instances of the record (with the three data elements per record) per loop, I decided I wanted to populate the table with side-by-side instances of the array records.

As an example:

Current Table:
Record 1: (1) (2) (3)
Record 2: (1) (2) (3)
Record 3: (1) (2) (3)

Proposed Table:
Record 1: (1) (2) (3) Record 2: (1) (2) (3)
Record 3: (1) (2) (3) Record 4: (1) (2) (3)

My difficulties are in seeing how I would manage to increment the pointer to allow two records to be accessed per cycle of FOREACH.

I've attempted to use NEXT, which does what I desire, until the next loop where I get a doubled record. (Record 1, Next to 2, Record 2, Next to 3...) I am assuming that NEXT does not actually move the array pointer.

I've looked at ARRAY_POP, which doesn't seem to provide the function I require, and reacts differently than I expected in practice.

At present, I'm studying the IMPLODE function, but that seems to be a rather cumbersome solution.

If one of you kind folk would care to point me to an example, or the proper PHP commands, I would be much obliged.

    To truly understand the machanism for this you have to understand the modulous (%) operator. This operator gives you the remainder should you divide a by b so 10%10 would give you 0 11%10 would give you 1 etc. (I can't give a further explination because I'm a little drunk at the moment)
    therefore for your situation you need somthin like the following.

    echo("<table>");
    $i=0;
    foreach($array as $key => $value)
      if($i%2==0)
        echo("<tr>");
      echo("<td>");
      //echo data
      echo("</td>");
      if($i%2==1)
        echo("</tr>");
    }
    

    HTH
    Bubble

      foreach will only work on one array at a time, BUT here are some ways you can address this issue.

      array_merge() will join two arrays together and then use foreach on the new array.

      or you could do a nested foreach inside of a foreach i.e.

      $test_A=array();
      $test_B=array();

      foreach( $test_A as $value_1)
      {
      foreach( $test_B as $value_2)
      {
      //do whatever using $value_1 and $value_2
      //to limit some of the returned values down to what you
      //want use something here like

             if($value_A > 10)
             {
                    $blah.=$value_A;
             }
      
      }

      }

      Best of luck to you

      AJ

        Thank you both, I've been successfull in translating this from concept to code 🙂

        Bubblenut:

        I sat stunned for a moment, and then proceeded to soundly kick myself for the narrow thinking. Of course that will work, thank you.

        Kenderized:

        Thank you as well. I had run across the concept of re-working the array in one of my books, and was planning on using it if nothing else came to mind.

        The only objection I had to it was I assume the additional time required to rework the original array into another would be noticable with large record sets, although this was just a concept (I have a habit of being a minimalist when it comes to coding), and I was certain that I was overlooking a simple function.

        Imagine my shock when I discovered I was wrong :p

        I hadn't quite envisioned your code; Thanks again for the object lesson, I'll be sure to print it out for further reference.

          I too try to keep my code simple and small, but I generally keep to the practice of , First make it work, then make it neat and trim, anyway your quite welcome for the help. I would suggest browsing through www.php.net site and look at the array functions listed there for some good reading.

          good luck take care
          AJ

            Write a Reply...