Solved

MySQL results in three columns

Posted on 2011-02-11
18
456 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
 
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
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: 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

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now