Solved

MySQL results in three columns

Posted on 2011-02-11
18
457 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:jaxbrian
  • 9
  • 8
18 Comments
 
LVL 38

Expert Comment

by:Aaron Tomosky
ID: 34876906
How about filling up a div set to float left, then fixture two more times?
0
 
LVL 2

Author Comment

by:jaxbrian
ID: 34876924
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
0
 
LVL 38

Expert Comment

by:Aaron Tomosky
ID: 34876928
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.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 2

Author Comment

by:jaxbrian
ID: 34876939
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?
0
 
LVL 38

Assisted Solution

by:Aaron Tomosky
Aaron Tomosky earned 250 total points
ID: 34877017
No, in your fetcharray loop, add a counter. Just set a $i=1 up top and start a div.  At the end of the loop $i++. At the top o the loop if $i> 50 reset i and close the div.
0
 
LVL 2

Accepted Solution

by:
requeue earned 250 total points
ID: 34877114
Hi jaxbrian, aarontomosky

Let me assist you guys.

Here's the code:
$data = mysql_query("SELECT * FROM test ORDER BY 1") ;
$data_count = mysql_num_rows($data);
$div_open = '<div style="float:left;margin-left:30px;">';
$div_close = '</div>';

print $div_open;
for ( $i = 0; i < 150 && $i < $data_count; $i ++)
{
	if ($i % 50 == 0 && $i > 1)
	{
		print $div_close;
		print $div_open;
	}
	$info = mysql_fetch_array( $data );
	print .$info['1'] . "&nbsp;&nbsp;&nbsp;" . $info['2'] . "&nbsp;&nbsp;&nbsp;" . '<br>';
}
print $div_close;

Open in new window

0
 
LVL 38

Expert Comment

by:Aaron Tomosky
ID: 34878668
Thanks requeue. I'm limited to non working code answers when I'm on my phone.
0
 
LVL 2

Author Comment

by:jaxbrian
ID: 34883443
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
0
 
LVL 38

Expert Comment

by:Aaron Tomosky
ID: 34883484
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.
0
 
LVL 2

Author Comment

by:jaxbrian
ID: 34883524
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?
0
 
LVL 38

Expert Comment

by:Aaron Tomosky
ID: 34883592
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.
0
 
LVL 2

Author Comment

by:jaxbrian
ID: 34884034
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.
0
 
LVL 38

Expert Comment

by:Aaron Tomosky
ID: 34884093
You have to add another counter variable and look for >=3
0
 
LVL 2

Author Comment

by:jaxbrian
ID: 34884207
I don't know how to do that, I will try and will look around for something.

thank you
0
 
LVL 38

Expert Comment

by:Aaron Tomosky
ID: 34884222
Just like the counter variable for the 50 per column.
0
 
LVL 2

Author Comment

by:jaxbrian
ID: 34884503
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?
0
 
LVL 38

Expert Comment

by:Aaron Tomosky
ID: 34884615
I suggest you start a new question for this new question. However I bet you will mostly get responses to use something like fpdf.
0
 
LVL 2

Author Comment

by:jaxbrian
ID: 34884754
thank you for your time
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mysql disables rename 4 68
when to use sequences in mysql 4 27
How would I do a website like this? 5 56
how to use 2 SELECT statements with prepared statements 19 30
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question