Results 1 to 2 of 2

Thread: Arranging rows for dynamically creating table

  1. #1

    Thread Starter
    Fanatic Member bharanidharanit's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    673

    Arranging rows for dynamically creating table

    Hi,
    I am using this coding to create a dynamic table for filling only 2x3 tables.
    Code:
    <html>
    <head>
    <style type="text/css">
    .friends_avatar
    {
    width: 50px;
    height: 50px;
    }	
    </style>
    </head>
    <body>
    <?PHP
    require_once('functions/dbconnect.php');
    $columns=3;
    $count=1;
    $query="SELECT u.username
    FROM users u
    WHERE u.id
    IN(
    SELECT friendid
    FROM friend 
    WHERE userid='113'
    AND arefriends='0')";					
    $result=mysql_query($query) or
    die(mysql_error($dbconn));	
    if(mysql_num_rows($result) <1 )
    {
    echo "No friends for users";
    return false;
    }
    ?>
    <table border="0" cellpadding="0" cellspacing="0">
    <?PHP
    while($userdata=mysql_fetch_array($result,MYSQL_ASSOC))
    {
    $username=$userdata['username'];
    if($count%$columns == 1)
    	{
    		?>
    			<tr>
    				<td class="friends_avatar_list">
    				<table border="0" cellpadding="0" cellspacing="0">
    					<tr>
    						<td align="center">
    							<a href="<?PHP echo($navurl); ?>"><?PHP echo($username); ?></a>
    						</td>
    					</tr>
    				</table>
    				</td>
    				
    		<?PHP
    	}
    	else if($count%$columns == 0)
    	{
    		?>
    			
    				<td class="friends_avatar_list">
    				<table border="0" cellpadding="0" cellspacing="0">
    					<tr>
    						<td align="center">
    							<a href="<?PHP echo($navurl); ?>"><?PHP echo($username); ?></a>
    						</td>
    					</tr>
    				</table>
    				</td>
    			</tr>							
    		<?PHP
    	}
    	else
    	{
    		?>
    		
    			<td class="friends_avatar_list">
    			<table border="0" cellpadding="0" cellspacing="0">
    				<tr>
    					<td align="center">
    						<a href="<?PHP echo($navurl); ?>"><?PHP echo($username); ?></a>
    					</td>
    				</tr>
    			</table>
    			</td>
    		
    	<?PHP
    	}
    	$count++;
    }	
    	
    
    
    $count--;
    	if($count%$columns!=0)
    	{
    		?>
    			</td>
    	  		</tr>
    			</table>
    			</body>
    			</html>
    		<?PHP	
    	}
    
    
    
    ?>
    But now i want to fill each 2x3 table in a seperate div. How to do this?
    Any Ideas???

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Arranging rows for dynamically creating table

    umm. first of all, you should just make your code always echo out the <td>, all you're doing is repeating the same code 3 times and you're just adding a <tr> or </tr>. use your IF statements to figure out whether or not you need to echo the <tr> before the <td> or the </tr> after the <td>.

    anddd, if you want to start creating multiple <table>s, each within their own <div>s, then you need to have IF statements that checks the value of $count to see if you're at the beginning of the table so that you can create the div/table, and then another IF statement to check if you're at the end of your table, so that you can end the table/div and reset the counter. then, the next iteration will start a new table.

    hope that makes sense.

Posting Permissions

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



Click Here to Expand Forum to Full Width