Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    844
    Thanks
    15
    Thanked 9 Times in 9 Posts

    Formatting Output?

    PHP Code:
    <?php
    $textlist 
    mysql_query("SELECT * FROM utexts_category") or print mysql_error();
    $count=1;
    if(
    mysql_num_rows($textlist))
    {
        while(
    $row=mysql_fetch_array($textlist))
        { 
    ?>
            <td valign="top"><img src="files/folder.gif" width="31" height="30" align="absmiddle"><a href="list.php?cat=<?php echo $row["link"]; ?>"><strong><?php echo $row["category"]; ?></strong></a>*(                         <?php $total_results mysql_num_rows(mysql_query("SELECT * FROM ".$dbprefix."_catalog WHERE category='".$row["link"]."' ".$sql)); echo "$total_results"?> )</td>
    <?php



            $count
    ++;
        }
    }
    ?>
    Alright I got the following PHP code which works excellent... Just a bit of a formatting question.
    The current output of the script puts out categories in one row...
    Example:
    Code:
    	Cat1	Cat2	Cat3	Cat4	Cat5	Cat6	Cat7	Cat8
    // And so on
    However I only want rows of 3....
    Code:
    	Cat1	Cat2	Cat3
    	Cat4	Cat5	Cat6
    	Cat7	Cat8
    How would I go about implementing this?
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Assuming you have an opening <tr> prior to your code, you can implement a little count within your code which adds three columns then returns to column one of the next row:

    PHP Code:
    <?php
    $textlist 
    mysql_query("SELECT * FROM utexts_category") or print mysql_error();
    $count=1;
    if(
    mysql_num_rows($textlist))
    {

        
    $col=1// we start with column 1

        
    while($row=mysql_fetch_array($textlist))
        { 
    ?>
            <td valign="top"><img src="files/folder.gif" width="31" height="30" align="absmiddle"><a href="list.php?cat=<?php echo $row["link"]; ?>"><strong><?php echo $row["category"]; ?></strong></a>*(                         <?php $total_results mysql_num_rows(mysql_query("SELECT * FROM ".$dbprefix."_catalog WHERE category='".$row["link"]."' ".$sql)); echo "$total_results"?> )</td>
    <?php

            $count
    ++;
            
    // now we check which column we are on and whether we need to insert a new row
            
    if($col==3) {
                
    $col 1;
                
    ?>
                    </tr><tr>
                <?php
            
    } else {
                
    $col++;
            }
                
        }
    }
    ?>

  • #3
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The above code will work fine provided that you have a multiple of 3 categories, however if you dont then it could be a problem... I personally would do this.

    P.S: On a side note, closing PHP parsing (?>) then opening again (<?php) is ok, but I wouldnt reccomend it inside loops... This generally takes more time than just priting out the content to the page. However, I will leave your current method itact in my example...

    PHP Code:

    $counter = 0;

    while($row=mysql_fetch_array($textlist))
    {

       if($counter % 3 == 0)
       {
          echo '<tr>';
       }

       $counter++;
       
       ?>
       <td valign="top"><img src="files/folder.gif" width="31" height="30" align="absmiddle"><a href="list.php?cat=<?php echo $row["link"]; ?>"><strong><?php echo $row["category"]; ?></strong></a>*(                         <?php $total_results mysql_num_rows(mysql_query("SELECT * FROM ".$dbprefix."_catalog WHERE category='".$row["link"]."' ".$sql)); echo "$total_results"?> )</td>
       <?php

       
    if($counter == 0)
       {
          echo 
    '</tr>';
       }

    }

    $remaining = ($counter 3);
    if(
    $remaining 0)
    {
       echo 
    str_repeat('<td>&nbsp;</td>'$remaining);
       echo 
    '</tr>';
    }
    You should note that this is untested, but in my head it seems like it should work

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by missing-score
    The above code will work fine provided that you have a multiple of 3 categories, however if you dont then it could be a problem...
    You're absolutely right. I've spent so long trying to get my mind out of the table-layout mindset that I completely overlooked the messy results of having only one or two items in the final row of the table - oops!

  • #5
    Regular Coder
    Join Date
    Nov 2004
    Location
    The Netherlands
    Posts
    551
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This could be done with css. Just set float on the generated divs, give them a width of 33%. After the divs put a div with float:auto
    CATdude about IE6: "All your box-model are belong to us"


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •