Jump to content

[SOLVED] Split result in multiple columns.


kevin_newbie

Recommended Posts

Hello,

 

I have been trying to search for a way where I can split my results dynamcially but everyone seems to do it with tables. I like to do this in divs.

 

 

Right now it is matching $count and $split at 13 then the rest of the entries go to the second divs. It only displays 2 divs I want to be able to have num_col be what ever number and still have the columns display how it should.

 

This is what I have so far;

<head>

	<title></title>

	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

	<link rel="stylesheet" type="text/css" href="" />

	<style type="text/css">

		div.right {
		float: left;
		padding: 10px;
		margin: 0px 30px 0px 0px;
		border: 1px solid #000000;
		}

	</style>

</head>

<body>


	<div class="right">

		<?php

			$s = mysql_query("SELECT name FROM us_states");
			$num_row = mysql_num_rows($s);

			//Number of Columns
			$num_col = 4;

			//$split = 13
			$split = ceil($num_row / $num_col);

			$count = 1;

			while($r = mysql_fetch_array($s))
			{
				$name = $r['name'];

				echo $name . "<br />";

				if ($count == $split) {
					echo "</div><div class=\"right\">";
				}

				$count++;
			}			

		?>		


	</div>


</body>

</html>

 

 

Is this a good start if so where should i go from here?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/173076-solved-split-result-in-multiple-columns/
Share on other sites

I did that code just now and it is not what I wanted.

 

The thing is I want the code is go column by column not row by row. My snippet is doing all of the data in one column then when it hits the if statement it closes the first column and dumps the rest of the record in the second column. I want that same concept but spread in how ever many columns I type in. So that the reminder is in the last column I specified.

Hi

 

If I understand correctly what you are after is something like a newspaper layout. Ie, a set number of columns and splitting the data between them.

 

You only check that $count is equal to $split, which will only happen once (ie, with 1000 rows you would have 250 per column and $split would be 250). Use the modulus of $count / $split and check it is 0 (which would be OK for 250, 500 and 750):-

 

if (($count % $split) == 0)

 

All the best

 

Keith

AWESOME! Thank you very much.

 

Is there a tutorial that you recommend that explains the % for php?

 

Also I was playing around with the if statement i had in the loop I changed to this:

				if ($count == $split) {
					echo "</div><div class=\"right\">";
					$count = 1;
					continue;
				}

 

It seems to work but just a little bit more lines of code. What do you think?

Hi

 

% is modulus. Basically does a divide and gives you the remainder. If you google for modulus you should find plenty on it (googling for % is pretty pointless!)

 

Your solution would also work. May actually be more efficient as there are no calculations to be done (in this case as it is all you are using $count for). Personally I would leave out the continue (either put the $count++ in an else, or just set $count = 0 within that if statement), as I do not like jumping around like that (a GOTO by another name).

 

All the best

 

Keith

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.