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
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>	

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

This really is the best way i can explain it. [#] = result shown within a table cell.

 

 

So as opposed to showing the results like this

 

[zero][1][2][4]

[4][5][6][7]

[8][9]

 

i want to display them like so...

 

[zero][3][6][9]

[1][4][7]

[2][5][8]

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>	

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.