Splitting result into multiple colunms

playminigames

New Member
Messages
216
Reaction score
6
Points
0
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:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
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?
 

s42kyks

New Member
Messages
1
Reaction score
0
Points
0
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:

playminigames

New Member
Messages
216
Reaction score
6
Points
0
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
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
First it was five columns, now it is six.
First it was a mysql query, now it is a 'list'.
 
Top