+ Reply to Thread
Results 1 to 7 of 7

Thread: Splitting result into multiple colunms

  1. #1
    playminigames is offline x10 Sophmore playminigames is an unknown quantity at this point
    Join Date
    Jul 2009
    Location
    earth
    Posts
    216

    Splitting result into multiple colunms

    Ok i have been searching on google for this, but i have not been able to find what i need, so i came here. I have this mysql query and i would like it to diplay in 5 rows, like this
    1. 4. 7. 10. 13.
    2. 5. 8. 11. 14.
    3. 6. 9. 12. 15.

    Thanks in advanced
    Last edited by playminigames; 09-28-2009 at 09:26 PM.

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Splitting result into multiple colunms

    Exactly what is the result of the query?

    One row of 15 columns?
    15 rows of 1 column? Like 15 names?
    3 rows of 5 columns? Like 3 people, each with first_name, last_name, age, ssn, city?

    Is the result always the same number of items?
    Nothing is always absolutely so.

  3. #3
    s42kyks is offline x10Hosting Member s42kyks is an unknown quantity at this point
    Join Date
    Sep 2009
    Posts
    1

    Re: Splitting result into multiple colunms

    This was kinda interesting because I was needing to write a script to do the exact same thing.
    The script I wrote will change the formatting from a horizantal reading sort in a grid to a vertical sort in a grid, which isn't straightforward when the data is in a linear array.

    A B C D
    E F G H
    I J

    becomes

    A D G J
    B E H
    C F I

    The way I thought of to do it is to increment the counter by the number of rows. If it goes above the number of elements (n), that means a new row if reached and subtract by (n-1) to get one more. The wording is a little off, but anyways, here's the PHP script I went ahead and wrote. It's in PHP since that's what I'm using (and I needed this anyways).

    Arguments:
    $array --- input linear array of sorted values
    $col --- number of columns to output (rows will be calculated)
    $pad --- value to put in empty cells. To switch, it is needed for the array to be able to fit a rectangle, thus some empty values may be needed. (See reordering above and look at blank spaces and notice the blank spaces.) The script forces the output array to be a rectangle by appending blank values.

    Output:
    linear array of values. Using the letters for example:
    {A, D, G, J, B, E, H, ' ', C, F, I, ' '}

    Script:

    (Note: NOT debugged yet and written in notepad.)
    Code:
    function $out = reorder_array( $array, $col, $pad ) {
      $num = count($array_in);
      $row = ceil($num/$col);
    
      $in = array_pad( $array, $row*col, $pad );
      $num = $row*col-1; //Highest index
      
      $out = array();
      for($i=0; $i!=$num; $i+=$row) {
        if($i>=$num) $i-=$num;
        
        $out[] = $in[$i];
      }
      
      return $out
    }
    This will reformat the array and let you print in the same manner you would for a horizontal sort.

    This may not be the most efficient way
    Last edited by s42kyks; 10-02-2009 at 02:06 AM. Reason: bugfix

  4. #4
    playminigames is offline x10 Sophmore playminigames is an unknown quantity at this point
    Join Date
    Jul 2009
    Location
    earth
    Posts
    216

    Re: Splitting result into multiple colunms

    ok so basically i have a list of games, and i need them to display in 6 columns and i want them to order down first, so i know there is a way to do it, but my google skills have failed me and i havent been able to get the result i wanted.

    Thanks

  5. #5
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Splitting result into multiple colunms

    First it was five columns, now it is six.
    First it was a mysql query, now it is a 'list'.
    Nothing is always absolutely so.

  6. #6
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: Splitting result into multiple colunms

    Quote Originally Posted by descalzo View Post
    First it was five columns, now it is six.
    First it was a mysql query, now it is a 'list'.
    lol
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  7. #7
    playminigames is offline x10 Sophmore playminigames is an unknown quantity at this point
    Join Date
    Jul 2009
    Location
    earth
    Posts
    216

    Re: Splitting result into multiple colunms

    Never mind, i found my solution, thanks for trying to help anyways

+ Reply to Thread

Similar Threads

  1. multiple queries
    By garrensilverwing in forum Programming Help
    Replies: 12
    Last Post: 05-12-2009, 08:24 PM
  2. Multiple Forum / Hosting Account Links
    By Bryon in forum News and Announcements
    Replies: 10
    Last Post: 04-02-2009, 02:02 AM
  3. Replies: 14
    Last Post: 09-29-2008, 07:07 PM
  4. New Site-Suggestions?
    By mnoutside in forum Review My Site
    Replies: 9
    Last Post: 08-27-2008, 07:01 AM
  5. Have a problem with my forum
    By tikloos in forum Scripts & 3rd Party Apps
    Replies: 43
    Last Post: 01-19-2006, 01:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers