Jump to content

Displaying Multi-Column data...


iPixel

Recommended Posts

To keep it short, i pull info from database. DISTINCT ItemName.

 

i need to give the results in 4 columns.

 

$total_results = $items->num_rows; // contains total # of results 
$columns = 4; // max columns
$items_per_column = ceil($total_results / $columns); //estimated results per column.

 

let's say the letters of the alphabet are these "ItemNames"

 

I dont want to display

[A][C][D][E][F][G]

 

but rather

 

[A][C][E][G]

[D][F]

 

I cant for the life of me find any examples or the math logic to figure this out.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/244241-displaying-multi-column-data/
Share on other sites

Here's what i was working with. Could you explain the % 4 to me, and i assume $count starts at 0 ?

 

<table cellpadding="3" cellspacing="0" border="0" width="100%">
						<?php
						for($i=0;$i<=$items_per_column;$i++)
							{
								echo "<tr>";

								for($r=1;$r<=$columns;$r++)
									{
										echo "<td>";

										print_r($items->result[$i][itemName]);

										echo "</td>";
									}

								echo "</tr>";
							}
						?>
                        </table>	

Aha i see. Ok but my bigger problem is figuring out which result to show. so if you have 96 results, 4 columns that's 24 results per column so the first row of the results would look like

 

[0][24][48][72]

 

I honestly have no idea what you mean by that. What is the logic (in plain english) you wish to use for deciding what goes in which column ?

in that case, you need to separate the values first (because html tables are built in rows)... so you could do something like this:

$cols = array();
$count = 1;
foreach ($original_array as $value){
    $cols[$count][] = $value;
    $count++
    if($count == 5) $count = 1;
}

 

this will build $cols[1], $cols[2], $cols[3] and $cols[4] with the values you want for each column

Oooh i managed to figure it out before i saw this post... but thank you for the help.

 

<?php
$total_results = $items->num_rows;
$columns = 4;
$rows = ceil($total_results / $columns);
?>
    <table cellpadding="3" cellspacing="0" border="0" width="100%">
    <?php
    for ($r=0; $r < $rows; $r++) 
        {
            echo "<tr>";
            
            for ($c=0; $c < $columns; $c++)
                {
                    
                    if ($r + $c * $rows < $total_results)
                        {
                            echo "<td>";
                            print_r($items->result[$r + $c * $rows][itemName]);
                            echo "</td>";
                        }
                    else
                        {
                            echo "<td> </td>";
                        }
                        
                }
            
            echo "</tr>";
        }
    ?>
    </table>	

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.