Solved

MySQL results in three columns

Posted on 2011-02-11
18
449 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
Comment Utility
How about filling up a div set to float left, then fixture two more times?
0
 
LVL 2

Author Comment

by:jaxbrian
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks requeue. I'm limited to non working code answers when I'm on my phone.
0
 
LVL 2

Author Comment

by:jaxbrian
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 2

Author Comment

by:jaxbrian
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
You have to add another counter variable and look for >=3
0
 
LVL 2

Author Comment

by:jaxbrian
Comment Utility
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
Comment Utility
Just like the counter variable for the 50 per column.
0
 
LVL 2

Author Comment

by:jaxbrian
Comment Utility
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
Comment Utility
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
Comment Utility
thank you for your time
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

771 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

11 Experts available now in Live!

Get 1:1 Help Now