Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 136,482 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,745 people online right now. Registration is fast and FREE... Join Now!




output results using multiple columns

 
Reply to this topicStart new topic

output results using multiple columns, output results using multiple columns

Shiznit
4 Feb, 2008 - 10:29 AM
Post #1

New D.I.C Head
*

Joined: 14 Sep, 2007
Posts: 31


My Contributions
ever have one of those days where you're all thumbs? or you think you know where you left a particular code snippet and find that it's run off on you? rolleyes.gif

such has been my monday...

I've been searching hi and lo to find a script that will allow me to ouput my results in multiple columns

when a user clicks on a category, that category can have anywhere between 5 and 105 listings so rather than have a list a mile long I'd like for the output to display in maybe 4-5 columns so it's neater and easier to sort through

if anyone can help I'd greatly appreciate it smile.gif
User is offlineProfile CardPM
+Quote Post

Glasseater
RE: Output Results Using Multiple Columns
4 Feb, 2008 - 11:04 AM
Post #2

New D.I.C Head
*

Joined: 15 Jun, 2007
Posts: 46


My Contributions
You could grab the # of total results and divide that number by the # of columns. That will give you an approx. of how many should be in each column (round up). Then you can use nested for loops to build your column code. The last column is where it will get complicated if you don't have a number that divides nicely.

eg.
CODE

$percol = ceil($numtotal/5); //if 5 is how many column you want

$table = '<table width="100%">';

for($c = 0; $c < 5; $c++)
{
    $table .= '<tr>';
    for($i = 0; $i < $percol; $i++)
    {
        $current = ($c*$percol)-$i; //the number of items we've processed
        if($current <= $numtotal) //if we're not finished yet
            $table .= '<td>Whatever you need.</td>';
        else
            break; //we're done? oh.. ok, let's kill the loop
    }
    $table .= '</tr>';
}
$table .= '</table>';

**This probably isn't the greatest code, plus it's untested but it gives you the idea.**
User is offlineProfile CardPM
+Quote Post

Shiznit
RE: Output Results Using Multiple Columns
4 Feb, 2008 - 12:17 PM
Post #3

New D.I.C Head
*

Joined: 14 Sep, 2007
Posts: 31


My Contributions
thanks glasseater - unfortunately it did not work and my debugging skills are not the best...I've already spent too much time on this - I will have to visit it another day
User is offlineProfile CardPM
+Quote Post

SpaceMan
RE: Output Results Using Multiple Columns
9 Feb, 2008 - 08:59 AM
Post #4

D.I.C Regular
Group Icon

Joined: 20 Feb, 2003
Posts: 270

if i understand the question.....
i like the code was posted here, for things i am doing...

as for table colms..

i usually just add the TR tag or not..

CODE

<table border=1><tr>
<?
$count = 0;
$max = 4;
while(your loop){
$count++;
echo '<td>'.$count.' record stuff </td>';

if($count >= $max){
  //reset counter
   $count = 0;
  //end and restart
   echo '</tr><tr>';
}

}
?>
</tr></table>

User is offlineProfile CardPM
+Quote Post

Shiznit
RE: Output Results Using Multiple Columns
11 Feb, 2008 - 03:40 PM
Post #5

New D.I.C Head
*

Joined: 14 Sep, 2007
Posts: 31


My Contributions
I just wanted to follow up real quick (now that I have a few mins to myself smile.gif )

I finally found a snippet that worked wonders - I've included the entire "if" statement for your perusal...

CODE

if (isset($_GET['searchname']))
              {
                if (isset($_GET['byname']))
                  {
                    if (strlen($_GET['searchname'])>0)
                      {
                        echo '<p>Search for domains containing <span style="color:#ff0000;font-weight:bold;text-decoration:underline;">"' . $_GET['searchname'] . '"</span>.</p>';
                        $search_name_query="SELECT id, name FROM domains where name LIKE '%" . $_GET['searchname'] . "%'";
                        $search_name_result=mysql_query($search_name_query) or die (mysql_error());
                        // Use result
                        // Set column count
                        $columns = 2;
                        $rowCount = mysql_num_rows($search_name_result);
                        echo '<p>There are currently <span style="color:#ff0000;font-weight:bold;text-decoration:underline;">' . $rowCount . '</span> domains that match your search criteria. Please select from the following:</p>';
                        // Calculate how many records per column
                        $html = '<table width="100%"><tr>';
                        $x = 1;
                        while ($search_name_row = mysql_fetch_assoc($search_name_result))
                          {
                            $html .= '<td width="50%"><a href="details.php?domainid=' . $search_name_row['id'] . '">' . $search_name_row['name'] . '</a></td>';
                            $html .= ($x++ % $columns == 0) ? '</tr><tr>' : '';
                          }
                        $html .= '</tr></table>';
                        echo $html;
                        mysql_free_result($search_name_result);
                      }

                    else
                      {
                        echo '<p style="color:#ff0000;font-weight:bold;">Please type in all or part of a domain name to complete your search.</p>';
                      }
                  }
              }


This post has been edited by Shiznit: 11 Feb, 2008 - 03:40 PM
User is offlineProfile CardPM
+Quote Post

SpaceMan
RE: Output Results Using Multiple Columns
1 Mar, 2008 - 06:51 AM
Post #6

D.I.C Regular
Group Icon

Joined: 20 Feb, 2003
Posts: 270

how make this work with more then 9?

php


$html .= ($x++ % $columns == 0) ? '</tr><tr>' : '';

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 07:04PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month