Link to home
Start Free TrialLog in
Avatar of jaxbrian
jaxbrian

asked on

MySQL results in three columns

I am trying to display mysql results in three columns. I have been able to do it with one column but I have a hard time with loops and that is what I think I need. I would like to display 50 results per column so that would be a total of 150 per page. The code below is what I have so far to fetch the results and print them in one column. the second code would give me three columns but it only prints the same results.

thanks in advance for any help with this

JaxBrian
<?php 
include "connect.php";
$data = mysql_query("SELECT * FROM test ORDER BY 1") 
or die(mysql_error()); 
Print "<table border cellpadding=1>"; 
while($info = mysql_fetch_array( $data )) 
{ 
Print "<tr>";
Print "<ul>";
Print "<td>".$info['1'] . "&nbsp;&nbsp;&nbsp;" . $info['2'] . "&nbsp;&nbsp;&nbsp;" . "</td><td>" . "&nbsp;&nbsp;&nbsp;" . $info['3'] . "&nbsp;&nbsp;&nbsp;" . "</td>  "; Print "<td>".$info['1'] . "&nbsp;&nbsp;&nbsp;" . $info['2'] . "&nbsp;&nbsp;&nbsp;" . "</td><td>" . "&nbsp;&nbsp;&nbsp;" . $info['3'] . "&nbsp;&nbsp;&nbsp;" . "</td>  "; 
Print "<td>".$info['1'] . "&nbsp;&nbsp;&nbsp;" . $info['2'] . "&nbsp;&nbsp;&nbsp;" . "</td><td>" . "&nbsp;&nbsp;&nbsp;" . $info['3'] . "&nbsp;&nbsp;&nbsp;" . "</td>  "; 
} 
Print "</ul>";
Print "</tr></table>";
?>



<?php 
include "connect.php";
$data = mysql_query("SELECT * FROM test222 ORDER BY 1") 
or die(mysql_error()); 
Print "<table border cellpadding=1>"; 
while($info = mysql_fetch_array( $data )) 
{ 
Print "<tr>";
Print "<ul>";
Print "<td>".$info['1'] . "&nbsp;&nbsp;&nbsp;" . $info['2'] . "&nbsp;&nbsp;&nbsp;" . "</td><td>" . "&nbsp;&nbsp;&nbsp;" . $info['3'] . "&nbsp;&nbsp;&nbsp;" . "</td>  "; Print "<td>".$info['1'] . "&nbsp;&nbsp;&nbsp;" . $info['2'] . "&nbsp;&nbsp;&nbsp;" . "</td><td>" . "&nbsp;&nbsp;&nbsp;" . $info['3'] . "&nbsp;&nbsp;&nbsp;" . "</td>  "; 
Print "<td>".$info['1'] . "&nbsp;&nbsp;&nbsp;" . $info['2'] . "&nbsp;&nbsp;&nbsp;" . "</td><td>" . "&nbsp;&nbsp;&nbsp;" . $info['3'] . "&nbsp;&nbsp;&nbsp;" . "</td>  "; 
} 
Print "</ul>";
Print "</tr></table>";
?>

Open in new window

Avatar of Aaron Tomosky
Aaron Tomosky
Flag of United States of America image

How about filling up a div set to float left, then fixture two more times?
Avatar of jaxbrian
jaxbrian

ASKER

I am not sure exactly what you are talking about but, lets say i have over 600 entries in a table and I need it to print three columns per page first column would be 1-50, second would be 51-100, third would be 101-150 an the on page two it would start with 4 being 151-x and so on. does that make my question a little more clear?


thank you,
jaxBrian
Yeah. So instead of making a table, make a div. Stop after 50 rows. End the div. Start another. Make sure the divs are set to float left with a width of 32% or less for thicker margins.
so, set a LIMIT 0,50 and echo that it the first div container that floats left then LIMIT 50,50 and echo that in a container that is in the middle and so on?

I would have to do that for some undetermined amount of times because there is no telling how many entries there will be. is there a way to put that into a loop so I wouldn't have to type that 50 times in case there was 3000 results in the array?
SOLUTION
Avatar of Aaron Tomosky
Aaron Tomosky
Flag of United States of America 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
ASKER CERTIFIED SOLUTION
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
Thanks requeue. I'm limited to non working code answers when I'm on my phone.
hey guys, thanks for all the help with this and I will award the points. I was just wondering if you knew of a way to insert a page break after the third column is printed before it drops down and prints a new column.
I want to do this so I can have three columns of data per page when I print it out.

thank you very much for your help with this

JaxBrian
You can do a seperate CSS file for printing, but you may want to look into generating a PDF in php. It's the only way to insure it comes out correct on different computers and printers.
when I create a pdf or print preview it only has one page, that is really tall. I tried to say something like page-break-after:800px
would the stylesheet for print be something like <style ...   media="print"> and then set up the margins and what not but how do I force a line break after each group of three divs?
http://www.velocityreviews.com/forums/t164455-how-to-add-a-page-break-to-a-html-page.html

But I still think you should use php to make a PDF. Not the browser.  Here is one way
http://www.fpdf.org/

There are many others, search experts exchange for questions about php PDF generation.
so i have looked into the other stylesheet for print and with the code above i can not figure out how to insert some kind of identifier for the CSS to recognize so i can tell it to page-break-after:  If i make it so that it uses the same div element as the columns are in it creates one column with the page breaks after every fifty rows.
You have to add another counter variable and look for >=3
I don't know how to do that, I will try and will look around for something.

thank you
Just like the counter variable for the 50 per column.
so, I have gotten it to print an element that I put a page-break-after:always; in the stylesheet and for some reason it still will only print just one page. it will not recognize that there is more than one page and I do not know why.

any ideas?
I suggest you start a new question for this new question. However I bet you will mostly get responses to use something like fpdf.
thank you for your time