Solved

MySQL results in three columns

Posted on 2011-02-11
18
462 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
18 Comments
 
LVL 39

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 39

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
Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

 
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 39

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 39

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 39

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 39

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 39

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 39

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 39

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

717 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