Link to home
Start Free TrialLog in
Avatar of gamebits
gamebitsFlag for Canada

asked on

How to split query results in 2 (or 3) columns

Might be simple but I have no idea.

I have a database that hold baseball cards information, I want to make a query to get a printable checklist, basically a table with 3 columns like this

checkbox  card#  title

that is easy I got that already, this is working great if you don't have more than 20 to 25 rows to display but if you have 2 - 300 rows it's not working so good anymore.

it would be better to split the result in 2 or 3 columns like this

checkbox  card#  title          checkbox  card#  title          checkbox  card#  title

How do you do that?

here is the code I'm using


<?
  $resultID2 = mysql_query ("SELECT card_number,card_title
                               FROM Non_Sports, Card_Set, Series
                              WHERE Series.series_id = Card_Set.series_id
                                AND Non_Sports.card_id = Card_Set.card_id
                                AND Card_Set.series_id = '62'
                              ORDER BY sorting_order
                               ");
                           
  $num=mysql_numrows($resultID2);
 
  echo "<b>Displaying <font color=\"red\">$num</font> results.</b><br><br>
 
  <table align=\"center\" border=\"0\" width=\"400\" cellpadding=2 cellspacing=0> ";
 
 while ($datarow = mysql_fetch_assoc($resultID2)) {

 
                 echo '<tr><td width=5><input type="checkbox" name="Record_id[]" value='. $datarow['card_id'] .'></td>';
                       echo '<td align="center">'. $datarow['card_number'] .'</td>';
                       echo '<td align="left">'. $datarow['card_title'] .'</td>';
                       
       echo '</tr>';

 $datarow_count++;  
                          }
                         
     print "</table>";

?>
 
Thanks

Gamebits
Avatar of Raynard7
Raynard7

Make a table with 6, rather than 2 columns,

Currently for each record you are outputting tr for each line.

Create a counter and increment every time that you return a value, starting at 0
before you fetch the assoc array output <tr>

Only output </tr><tr> when counter mod 3 = 0 (every third time) and counter != 0
write your two columns <td> data field 1 </td><td> data field 2 </td>
at the end - if counter mod 3 != 0  also output </tr>

This will produce data for 6 columns in a table
the data will be sorted across the columns.
Avatar of gamebits

ASKER

Thanks for the reply Raynard7.

If I read you correctly this will sort the result like this (assume card numbers)

1                       2                        3
4                       5                        6
7                       8                        9

Would it be possible to sort the result like this

1                       4                       7
2                       5                       8
3                       6                       9

or is this totally impossible?

Thanks

Gamebits
This is possible;

What you would need to do is use the mysql num rows function to return the possible number of rows in the query.

I would then create a table with three columns.

I would then enter the first column create a new table with two columns incide this cell and and loop down using your above code until i had number_of_rows/3 I would then start on the second column until i had reached number_of_rows/3  and then repeat.

This way the order will be preserved - but is more complicated to code.
ASKER CERTIFIED SOLUTION
Avatar of pritaeas
pritaeas
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Forgot: You can change $num_columns to whatever you want: 1, 2, 3, 4, 5, .....

You only have to adjust the display to the way you want it.

Hope this helps, pritaeas
hi mate

here is a function i use on my php site, hopefully it iwll help you get an idea about what you should do:

  $query= mysql_query ("SELECT card_id, card_number,card_title
                               FROM Non_Sports, Card_Set, Series
                              WHERE Series.series_id = Card_Set.series_id
                                AND Non_Sports.card_id = Card_Set.card_id
                                AND Card_Set.series_id = '62'
                              ORDER BY sorting_order
                               ");

$results = mysql_query($query);


while ($row = mysql_fetch_assoc($results)) {
$items[] = $row;
}


     /*
     $sort_direction:
         1 for up to down
         2 for left to right
     */
     $sort_direction = 1;
     $columns = $num;
     $rows = ceil(count($items) / $columns);
     $table_data = array();

     for($x = 0; $x < $rows; $x++) {
         $table_data[$x] = '';
     }
     for($i = 0; $i < count($items); $i++) {
          $item = $items[$i];
          $text = '<td><input type="checkbox" name="Record_id[]" value='. $item['card_id'] .'></td>' .
                     '<td align="center">'. $item['card_number'] .'</td>' .
                     '<td align="left">'. $datarow['card_title'] .'</td>';
          switch($sort_direction) {
               case 1:
                    $table_data[$i % $rows] .= $text;
                    break;
               case 2:
                    $table_data[(int) $i / $columns] .= $text;
                    break;
          }
          $result = '<table width="' . $width . '" align="center">';
          $result .= "\n  <tr>\n";
          $result .= implode("  </tr>\n  <tr>\n", $table_data);
          $result .= '  </tr>';
          $result .= "</table>\n";
     }
echo $result;
Have these suggestions worked? they should.
Perfect!!!!

Thank you so much!!!!
I really wana say Thank you so much!. this discussions really help me a lot. !!!