Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 01-16-2005, 08:04 AM   PM User | #1
keith1995
Regular Coder

 
Join Date: Jul 2004
Posts: 130
Thanks: 0
Thanked 0 Times in 0 Posts
keith1995 is an unknown quantity at this point
Split Results to Three Columns

I've never actually figured out how to do this. What I'm looking to do is query our database, get the results (which are simple names) and then output the results to a three column table with the results evenly divided up into each column. So, the table would look similar to:

result 1 | result 4 | result 7
result 2 | result 5 | result 8
result 3 | result 6 | result 9

The database is a very simple one. Just two rows:

ID
Name

How do I accomplish this?
keith1995 is offline   Reply With Quote
Old 01-16-2005, 07:12 PM   PM User | #2
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf is on a distinguished road
what server side language will you use to turn the recordset into a table?

regardles of the language, you best ue an incrementing variable and then check the modulus when you devide this incremental by 3.
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Old 01-16-2005, 10:52 PM   PM User | #3
keith1995
Regular Coder

 
Join Date: Jul 2004
Posts: 130
Thanks: 0
Thanked 0 Times in 0 Posts
keith1995 is an unknown quantity at this point
Raf,

You are always so quick to resond to my questions and are always so helpful! Thanks for that.

I will be using PHP. If you could show me some sample code that I could use to edit to fit my needs, that will be very helpful.
keith1995 is offline   Reply With Quote
Old 01-17-2005, 08:35 AM   PM User | #4
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf is on a distinguished road
here you go http://www.codingforums.com/showthre...hlight=modulus
--> contains links to two threads with examplecode
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Old 01-18-2005, 09:09 PM   PM User | #5
keith1995
Regular Coder

 
Join Date: Jul 2004
Posts: 130
Thanks: 0
Thanked 0 Times in 0 Posts
keith1995 is an unknown quantity at this point
Raf,

I've got the following code that doesn't what I need. However, this lists the results as such:

Name 1 | Name 2 | Name 3
Name 4 | Name 5 | Name 6
Name 7 | Name 8 | Name 9

I'd like to get it to list it like this:

Name 1 | Name 4 | Name 7
Name 2 | Name 5 | Name 8
Name 3 | Name 6 | Name 9

The code is:
PHP Code:
echo "<table width='100%' border='0' cellpadding='2' cellspacing='2' class='twelve'>";
                
$column=3

while(
$value=mysql_fetch_assoc($result)) 
    { 
    
$id $value['id'];
    
$first_name $value['first_name'];
    
$last_name $value['last_name'];

if (
$column==3
    { 
    echo 
"<tr>"
    } 

echo 
"<td>$first_name $last_name</td>"

$column++; 

if (
$column==6
    { 
    echo 
"</tr>"
    
$column=3
    } 


echo 
"</table>"
Any ideas how I would change it to get it to list the results like above?
keith1995 is offline   Reply With Quote
Old 01-18-2005, 09:56 PM   PM User | #6
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf is on a distinguished road
so kinda like a three column article or so ...

i would first build a 2D array where the first dimension is the row, and the second dimension is the column. Like
$arr_cells[1][1] = Name1
$arr_cells[2][1] = Name2
$arr_cells[1][2] = Name4

and then turn that array into a table like
PHP Code:
$table '<table>';
foreach (
$arr_cells as $row){
   
$table .= '<tr>';
   foreach (
$row as $cell){
       
$table .= '<td>'$cell .'</td>';
   }
   
$table .= '</tr>';
}
$table '</table>'
the harder bit is to build the array. You need to have a count and devide it by the number of columns, and then ceil it to know the number of rows. And then you need a variable that is incremented till you reach the number of rows, and then reset + increment another variable that contains the columnvalue. Like
PHP Code:
$number_of_collumns '3';
$row_num '1';
$coll_num '1';
$number_of_rows ceil(mysql_num_rows($result)/$number_of_collumns);
$arr_cells = array();
while(
$value=mysql_fetch_assoc($result)) { 
    
$arr_cells[$row_num][$coll_num] = $value['first_name'] . ' ' $value['last_name'];
    
$row_num ++;
    if (
$row_num $number_of_rows){
        
$row_num '1';
        
$coll_num ++;
    } 
}
// and then turning the array into a table
$table '<table>';
foreach (
$arr_cells as $row){
   
$table .= '<tr>';
   foreach (
$row as $cell){
       
$table .= '<td>'$cell .'</td>';
   }
   
$table .= '</tr>';
}
$table '</table>'
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:40 AM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.