Re: Sepating MySQL result set into html tables

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Sep 26, 2008 at 2:41 PM, Robert Cummings <robert@xxxxxxxxxxxxx> wrote:
> On Fri, 2008-09-26 at 21:23 +0300, Thodoris wrote:
>> > On Fri, 2008-09-26 at 13:50 -0400, admin@xxxxxxxxxxxxxxxxxxx wrote:
>> >
>> >> Might I suggest you count the fields and divide it by the cols you want to display?
>> >> Example
>> >> $forest = mysql_query("SELECT * FROM your_table");
>> >> $gump = mysql_num_fields($forest);
>> >>
>> >>
>>
>> First of all the problem is with the rows not the columns. The problem
>> is that I want to divide the rows into equal  (or anything close to
>> that) chunks and put every chunk in a separate table.
>>
>> There I said it :-) .
>>
>> >> Because I know my table contains 15 rows I can do this.
>> >> $tulip = floor($gump /5);
>> >>
>> >
>> >
>>
>> You can count the rows (not the cols) in the result set but you can't
>> base your algorithm on that only.
>>
>> The problem is that if for e.g. you have 91 rows and you want to divide
>> it into 3 you will have three chunks of 30 like this:
>>
>> 30, 30, 31
>>
>> > You have a bug if you have 16 rows.
>> >
>>
>> In my case I really don't care in what table the extra row goes.
>
> The solution is pretty easy, I just don't have time right now to write
> it for you. Either way, I would approach the problem inverseley to your
> current solution. Instead of traversing the rows and performing internal
> calculations, I'd calculate the tables and rows needed and then traverse
> that information and in the innermost loop traverse the result set using
> the next() function.
>
> Cheers,
> Rob.
> --
> http://www.interjinn.com
> Application and Templating Framework for PHP
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

When I need to do something like this I always use this base code that
I just edit.  I wrote it a few years ago and haven't really changed it
so it works for what I need.


<?php
$results    = 17; // how many cells do we actually have data for? [put
your record count here]
$cols       = 8; // desired column width of the table
$total      = $cols * ceil($results / $cols);  // total cells to create
$rows       = ceil($total / $cols);            // total rows to create
$position      = 0; // current position
echo "<br>results: ",    $results;
echo "<br>cols: ",       $cols;
echo "<br>total: ",      $total;
?>

<table border="1">

	<?php for ($row=0; $row < $rows; ++$row): ?>
		
		<tr>
		
		<?php for ($col=0; $col < $cols; ++$col): ?>
		
			<?php if ($results > $position): ?>
				<td>
					<?php
					// call your fetch record code here
					echo 'Col: '. $col .'<br>';
					echo 'Row: '. $row .'<br>';
					echo 'Position: '. $position .'<br>';
					?>
				</td>
			<?php else: ?>
				<td>
					empty cell
				</td>
			<?php endif; ?>
			
			<?php ++$position; ?>
			
		<?php endfor; ?>
		
		</tr>
		
	<?php endfor; ?>
	
</table>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux