Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 08-10-2002, 10:23 PM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,446
sde is on a distinguished road
making a 3 column table dynamically

well it's pretty easy to make a 2 column table with the modulus operator, but tonight i wanted to make a 3 column table.

the problem with creating dynamic tables is that if the number of rows of data you have is not equal to the exact number of columns, you will get tables that are not closed properly.

for example:
PHP Code:
<table>
<
tr>
     <
tddata 1</td><tddata 2</td></tr>
     <
tr><tddata 3</td>
</
tr>
</
table
u can fix that simply by using the modulus operator like this: ( i'll use an array for this example)
PHP Code:
<?
$i
=0;

echo 
"<table>";

// start loop for each item in array
foreach($array as $each)
{
     
// if the remainder of $i divided by 2 == 0 then start a new row
     
if($i == 0)
     {
        echo 
"<tr>";
     }

     echo 
"<td>$each</td>";

     
// if it's on the last item of the array, add an extra <td></td> to complete the row
     // only if it is on the first column of the table
     
if($i == ($count 1))
     {
          if(
$i == 0)
          {
               echo 
"<td>&nbsp;</td>";

               
// increment $i so it knows to close the table
               
$i++;
          }
     }

     
// if the remainder of $i divided by 2 !=0 then end row
     
if($i != 0)
     {
          echo 
"</tr>";
     }
}

echo 
"</table>";

?>
2 is easy to work with since every other number divided by 2 == 0.

But what do you do when you want 3 columns instead of 2? Well tonight I spent a lot of time working on this and I just thought I'd share.
PHP Code:
<?
$count
=count($array);
$i=0;

echo 
"<table>";

foreach(
$array as $each)
{
     
// make a new if the remainder of $i divided by 3 == 0
     
if($i == 0)
     {
          echo 
"<tr>";
     }            
            
     echo 
"<td width=125>$each</td>\n";
            
     
// add an extra <td></td> if there are not enough columns to complete the table
     // only test if $i is on the last item of the array
     
if($i == ($count 1))
     {
          
// keep adding blank cells till it's at the end
          
while(($i 1) % != 0)
          {
               echo 
"<td>&nbsp;</td>\n";
               
$i++;
          }                
     }            
            
     
// end table row if the remainder of $i+1 divided by 3 is 0
     
if(($i 1) % == 0)
     {                
          echo 
"</tr>";
     }
        
     
$i++;
}

echo 
"</table>";
?>
let's say you wanted to make a 4, or 5 column table .. all you would have to do is replace the number "3" with however many columns you want, and this script should work.

maybe there is a better way to do this, but it is all i could come up with. fun fun =)
sde is offline   Reply With Quote
Old 05-07-2004, 04:43 PM   #2 (permalink)
organ_zola
Registered User
 
organ_zola's Avatar
 
Join Date: May 2004
Posts: 3
organ_zola is on a distinguished road
Hi,

After trawling the inernet, this was *nearly* the ideal solution for me!!

I have a 3 column table with 11 rows (currently) read from a MySQL database and returned as an array, butI need the last row of the table to display the last columns centered in the table.

I've tried hacking at the code on these lines:

***************************************
// add an extra <td></td> if there are not enough columns to complete the table
// only test if $i is on the last item of the array
if($i == ($count - 1))
{
// keep adding blank cells till it's at the end
while(($i + 1) % 3 != 0)
{
echo "<td>&nbsp;</td>\n";
$i++;
}
}
**************************************

but I've only succeded in frustrating myself after a couple of hours!!

What I've been trying to do is capture the last results which < 3 and place them in another table like so:
<td colspan="3" align="center">
<table>
<tr>
<td>odd number cell 1</td>
<td>odd number cell 2</td>
</tr>
</table>
</td>

Could somebody give some pointers please!!! I need to get some sleep tonight!!!

many thanks!
organ_zola is offline   Reply With Quote
Old 05-07-2004, 05:36 PM   #3 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,446
sde is on a distinguished road
ok .. first of all you need to get the total rows so you can determine what row is the final row. then set a row counter variable to count rows
PHP Code:
// assign the total full rows
$total_rows floor($count/3);

// add 1 to total rows if there is one more incomplete row
if($count%3>0){$total_rows++;};

// set row counter to 0
$row_number 0
at the beginning of each row, increment the row counter:
PHP Code:
$row_number++; 
then, when you print each cell .. test to see if it is the last row, and print the align tag if true. you can shorten up the code by using the ternary operator as i have here:
PHP Code:
echo "<td width=125" . (($row_number==$total_rows)?" align=center":"") . ">$each</td>\n"
here is a full working script with this implemented:
PHP Code:
<?
$array 
= array("a","b","c","d","e","f","g","h","i","j","k");
$count count($array);

// assign the total full rows
$total_rows floor($count/3);

// add 1 to total rows if there is one more incomplete row
if($count%3>0){$total_rows++;};

// set row counter to 0
$row_number 0;

echo 
"<table border=1>";



foreach(
$array as $each)
{
    
// make a new if the remainder of $i divided by 3 == 0
    
if($i == 0)
    {
        
// increment the row number variable
        
$row_number++;
        echo 
"<tr>";
    }
          
    
// use the ternary operator to inject align tag if on the last row
    
echo "<td width=125" . (($row_number==$total_rows)?" align=center":"") . ">$each</td>\n";
          
    
// add an extra <td></td> if there are not enough columns to complete the table
    // only test if $i is on the last item of the array
    
if($i == ($count 1))
    {
      
// keep adding blank cells till it's at the end
        
while(($i 1) % != 0)
        {
           echo 
"<td>&nbsp;</td>\n";
           
$i++;
        }                
    }            
          
    
// end table row if the remainder of $i+1 divided by 3 is 0
    
if(($i 1) % == 0)
    {                
      echo 
"</tr>";
    }
      
    
$i++;
}

echo 
"</table>";
?>
sleep well
__________________
Mike
sde is offline   Reply With Quote
Old 05-08-2004, 01:03 AM   #4 (permalink)
organ_zola
Registered User
 
organ_zola's Avatar
 
Join Date: May 2004
Posts: 3
organ_zola is on a distinguished road
Thank you sde for your super-quick reply!!

This is *nearly* there again!! :s

The structure of the table I'm trying to build is:

****************
Code:
<table border="1" width="200">
	<tr>
		<td>1</td>
		<td>2</td>
		<td>3</td>
	</tr>
	<tr>
    	<td colspan="3" align="center">
                         <table border="1" width="100%">
                        	<tr>
                        		<td align="center">4</td>
					<td align="center">5</td>
                        	</tr>
                        </table></td>
    </tr>
</table>
****************

Now that the last row is captured, I've been trying to run another loop that will place a nested table that holds the last X records, and that nested table is centered within the main table... (if you follow my meaning!!)

The closest I can get is :
**********************
PHP Code:
// use the ternary operator to inject align tag if on the last row 
if ($row_number==$total_rows)
    {    
        
// last cell is colspanned by 3 and centered
        
echo "<td colspan=\"3\" align=\"center\">";
        
        
//start loop to print last remaining columns in their own table
        
for ($z=1$z <= ($count-$i); $z++)
        {
        echo 
"<table align=\"center\">
                         <tr>
                           <td align=\"center\" valign=\"top\">
                              <table>
                                <tr>
                                    <td width=\"125\">$each</td>
                                </tr>
                                 </table></td>
                          </tr>
                       </table>"
;
                }
        echo 
"</td>";
    }
    else
    {
        echo 
"<td width=\"125\">$each</td>\n";
    } 
***********************

I feel like I'm almost there, but need help to finally nail it!!

I'm feeling much more relaxed than I was earlier, so a good nights sleep is getting closer!!!
organ_zola is offline   Reply With Quote
Old 05-08-2004, 09:08 AM   #5 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,446
sde is on a distinguished road
ok, there might be more efficient ways of doing this, but this will do what you want. if the last row is 3 cells, then it prints normal.
PHP Code:
<?
$array 
= array("a","b","c","d","e","f","g","h","i","j","k");
$count count($array);

// assign the total full rows
$total_rows floor($count/3);

// add 1 to total rows if there is one more incomplete row
if($count%3>0){$total_rows++;};

// set row counter to 0
$row_number 0;

echo 
"<table border=\"1\" width=\"200\">\n";



foreach(
$array as $each)
{
    
// make a new if the remainder of $i divided by 3 == 0
    
if($i == 0)
    {
        
// increment the row number variable
        
$row_number++;
        echo 
"<tr>\n";
        
        
// setup last row only if row isn't 3 in length
        
if( $row_number == $total_rows && count($array)%3!=0)
        {
          echo 
"    <td colspan=\"3\" align=\"center\">\n  <table border=\"1\" width=\"100%\">\n  <tr>\n";
        }          
    }
          
    
// use the ternary operator to inject align tag if on the last row
    
echo "    <td" . (($row_number==$total_rows && count($array)%3!=0)?" align=center":"") . ">$each</td>\n";
 
  
    
// end table row if the remainder of $i+1 divided by 3 is 0
    
if( (($i 1) % == 0) || ($row_number == $total_rows && ($i+1)==count($array) ))
    {
      
// complete last row
      // only close table if row isn't 3 cells
      
if($row_number == $total_rows && count($array)%3!=0)
      {
        echo 
"  </tr>\n  </table>\n</td>\n";
      }

      echo 
"</tr>\n";
    }
      
    
$i++;
}

echo 
"</table>";
?>
__________________
Mike
sde is offline   Reply With Quote
Old 05-08-2004, 09:21 AM   #6 (permalink)
organ_zola
Registered User
 
organ_zola's Avatar
 
Join Date: May 2004
Posts: 3
organ_zola is on a distinguished road
You beauty!!

Thank you so much!

Here comes the overwhelming rush of joy (and the knowledge that I'll sleep well tonight!) as my frustration with this problem melts away!
organ_zola is offline   Reply With Quote
Old 08-28-2004, 06:23 PM   #7 (permalink)
xtrax_22
Registered User
 
xtrax_22's Avatar
 
Join Date: Aug 2004
Posts: 8
xtrax_22 is on a distinguished road
I was wondering if you would help me out with this

I was wondering if you would help me out with this l have been fighting with this for days


<?php
include("header.php");
$index = 0;
//require_once("mainfile.php");
$module_name = basename(dirname(__FILE__));
function who_online() {
global $prefix, $db;
$sql = "SELECT uname, guest FROM ".$prefix."_session WHERE guest = 0";
$result = $db->sql_query($sql);
$member_online_num = $db->sql_numrows($result);
$who_online_now = "";
$i = 1;
OpenTable();
$count = 0;
echo "<table border=\"1\" cellspacing=\"10\" cellpadding=\"10\">";
while($session = $db->sql_fetchrow($result)) {
if(isset($session["guest"]) and $session["guest"] == 0) {
$sql ="select user_id from ".$prefix."_users where username='$session[uname]'";
list($user_id) = mysql_fetch_row(mysql_query($sql));
$count++;
if($count == 1) { echo "<tr>"; }
if(file_exists("/home/virtual/site13/fst/var/www/html/modules/Photo/memberphotos/$user_id.jpg")) {
$who_online_now .= "<TD><center><A HREF=\"modules.php?name=Your_Account&amp;op=userin fo&amp;username=$session[uname]\"><img src=\"modules/Photo/memberphotos/$user_id.jpg\" border=\"0\" alt=\"$session[uname]\"><br>$session[uname]</center></TD></a>\n";
if($count == 3) { echo "</tr>"; }
if($count == 3) { $count = 0; }
}
}
}
if($count == 1) { echo "<td>&nbsp;</td><td>&nbsp;</td></tr>"; }
if($count == 2) { echo "<td>&nbsp;</td></tr>"; }
echo "</table>";
$count++;
if($count == 1) { echo "<tr>"; }
$i++;
}elseif(file_exists("/home/virtual/site13/fst/var/www/html/modules/Photo/memberphotos/$user_id.gif")) {
$who_online_now .= "<TD><center><A HREF=\"modules.php?name=Your_Account&amp;op=userin fo&amp;username=$session[uname]\"><img src=\"modules/Photo/memberphotos/$user_id.gif\" border=\"0\" alt=\"$session[uname]\"><br>$session[uname]</center></TD></a>\n";
if($count == 3) { echo "</tr>"; }
if($count == 3) { $count = 0; }
}
}
}
if($count == 1) { echo "<td>&nbsp;</td><td>&nbsp;</td></tr>"; }
if($count == 2) { echo "<td>&nbsp;</td></tr>"; }
echo "</table>";
$i++;
}
}
}
return $who_online_now;
}
$some_stuff = who_online();
echo "$some_stuff";
CloseTable();
include("footer.php");
}
?>

Any and all help you could provide would be apreciated
xtrax_22 is offline   Reply With Quote
Old 08-28-2004, 06:55 PM   #8 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,446
sde is on a distinguished road
sure i'd be happy to, .. i'm not gonna look at that code till you explain what exactly you want it to do, .. and what exactly it is doing as it is.

i wouldn't mind having a fresh thread for it too
__________________
Mike
sde is offline   Reply With Quote
Old 08-28-2004, 07:14 PM   #9 (permalink)
xtrax_22
Registered User
 
xtrax_22's Avatar
 
Join Date: Aug 2004
Posts: 8
xtrax_22 is on a distinguished road
Sure thing l will start a new post right know with that code..

XtraX_22
xtrax_22 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Making a dynamic Table cell xtrax_22 PHP 18 08-31-2004 09:08 AM
help me name a table sde Lounge 4 12-07-2002 12:50 PM


All times are GMT -8. The time now is 04:49 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting