hai...
i want to dispaly table like this. actually i am fetching data from database. i am displaying data in tables. but i want to display like column wise after row wise.

A           F        K
   B          G         L   
   C          H        M
   D          I         N
   E          J

like this. i m in just confusion using for loop in tables. help me.

Recommended Answers

All 14 Replies

is this possible to do any way?

We can do it, I am giving you the code

<table border="2" cellpadding="3" cellspacing="3">
<tr>
<?php
for($i=65;$i<91;$i=$i+4)
echo "<td>".chr($i)."</td>";
echo "</tr><tr>";
for($i=66;$i<91;$i=$i+4)
echo "<td>".chr($i)."</td>";
echo "</tr><tr>";
for($i=67;$i<91;$i=$i+4)
echo "<td>".chr($i)."</td>";
echo "</tr><tr>";
for($i=68;$i<91;$i=$i+4)
echo "<td>".chr($i)."</td>";
?>
</tr>
</table>

But this is not the correct solution, It is weird way of implementing....So I suggest you to search for better one. For time being you can use it like this..

We can do it, I am giving you the code

<table border="2" cellpadding="3" cellspacing="3">
<tr>
<?php
for($i=65;$i<91;$i=$i+4)
echo "<td>".chr($i)."</td>";
echo "</tr><tr>";
for($i=66;$i<91;$i=$i+4)
echo "<td>".chr($i)."</td>";
echo "</tr><tr>";
for($i=67;$i<91;$i=$i+4)
echo "<td>".chr($i)."</td>";
echo "</tr><tr>";
for($i=68;$i<91;$i=$i+4)
echo "<td>".chr($i)."</td>";
?>
</tr>
</table>

But this is not the correct solution, It is weird way of implementing....So I suggest you to search for better one. For time being you can use it like this..

thanks. but you are calling with ascii values. actually don't want to display alphabets. actually i have to display states like that. just i gave a example like that only. is there any way to display.

is there any way to do this? plz reply me how to do?

exactly i want to display like this in url

in that categorys are displayed in row wise and after that column wise.

Below code will display you data in following way
1 2 3 4 5
6 7 8 9 10
......

<?php
	$query = "select state_id,state_name from statemaster a ";
	$result_array =execute(query);;
	//you must use proper syntax to bring data in $result_array	
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
    <td height="5"></td>
  </tr>
<?php
	$count=count($result_array); //no of rows in table 
	$noofcols=5
	$rows=ceil($count/$noofcols);  //no of columns you want to break here it is 5

	$i=0;
	for($k=0;$k<$rows;$k++)
	{
?>
  <tr>
	
	<?php 
		for($j=0;$j<$noofcols;$j++)
		{                    		
	?>
    <td valign="top">

    <a href="referecne_page.php?stateid=<?php echo $result_array[$i]['state_id'];?>" class="links"><?php echo $result_array[$i]['state_name'];?></a>
    </td>
	<?php 
			$i++;
		}
	?>
    
  </tr>
  <?php
	}
?>
</table>

Below code will display you data in following way
1 2 3 4 5
6 7 8 9 10
......

yaa....i know this type to display. but is there any chance to display the above which i send url. in that all categories are displayed like this..how to do?haaa...?

1     6    11
2     7    12
3     8    13
4     9    14
5    10   15

Following code will give you exactly the required output i.e.
1 4 7
2 5 8
3 6

1) First create blank HTML table by finding rows and colmns, define id of each table cell
2) At the end of page using php/javascript write data into appropriate table cell.

<?php	
	$query = "select state_id,state_name from statemaster a ";
	$result_array =execute(query);;
	//you must use proper syntax to bring data in $result_array	
?>
<html>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
    <td height="5"></td>
  </tr>
<?php
	$count=count($result_array); //no of rows in table 
	$noofcols=5
	$rows=ceil($count/$noofcols);  //no of columns you want to break here it is 5

	$i=0;
	for($k=0;$k<$rows;$k++)
	{
?>
  <tr>
	
	<?php 
		for($j=0;$j<$noofcols;$j++)
		{                    		
	?>
    <td valign="top" id='cell<?php echo $k.$j?>'> 		&nbsp;    </td>
	<?php 
			$i++;
		}
	?>
    
  </tr>
  <?php
	}
	
?>
</table>
</body>

<?php

	$index=0;
	echo "\n<script lang='javascript'>";
	for ($i=0;$i<$noofcols;$i++)
	{
		for ($j=0;$j<$rows;$j++)
		{
		 
			echo "\ndocument.getElementById('cell{$j}{$i}').innerHTML='<a href=\"referecne_page.php?stateid={$result_array[$i]['state_id']}\" class=\"links\">{$result_array[$i]['state_name']}</a>';";

	 		$index++;
		}	
	}
	echo "\n</script>";
	
	
?>

</html>

i am also tried using matrix functionality. i was getting same out put.
below is your code which you send now. just i was changed in to my code.
displaying like this. just excute and see what you are getting in output

<?php	

$states_array=array("Alabama","Alaska","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","Florida","Georgia","Hawaii","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland","Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshir","New Jersey","New Mexico","New York","North Carolina","North Dakota","Ohio","Oklahoma","Oregon","Pennsylvania","Puerto Rico","Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Vermont","Virginia","Washington","Washington DC","West Virginia","Wisconsin","Wyoming");
	//$query = "select state_id,state_name from statemaster a ";
//	$result_array =execute($query);
$result_array=$states_array;
	//you must use proper syntax to bring data in $result_array	
?>
<html>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
    <td height="5"></td>
  </tr>
<?php
	$count=count($result_array); //no of rows in table 
	$noofcols=5;
	$rows=ceil($count/$noofcols);  //no of columns you want to break here it is 5

	$i=0;
	for($k=0;$k<$rows;$k++)
	{
?>
  <tr>
	
	<?php 
		for($j=0;$j<$noofcols;$j++)
		{                    		
	?>
    <td valign="top" id='cell<?php echo $k.$j?>'>&nbsp; 		    </td>
	<?php 
			$i++;
		}
	?>
    
  </tr>
  <?php
	}
	
?>
</table>
</body>

<?php

	$index=0;
	echo "\n<script lang='javascript'>";
	for ($i=0;$i<$noofcols;$i++)
	{
		for ($j=0;$j<$rows;$j++)
		{
		 
			echo "\ndocument.getElementById('cell{$j}{$i}').innerHTML='<a href=\"referecne_page.php?stateid={$result_array[$i]}\" class=\"links\">{$result_array[$i]}</a>';";

	 		
		}	
		$index++;
	}
	echo "\n</script>";
	
	
?>

</html>

1. Alabama Colorado Hawaii ...

2. Alaska Connecticut Idaho ...

3. Arizona Delaware Illinois ...

4. Arkansas Florida Indiana ...

5. California Georgia Iowa ...

Do you want to have the display like above ?

1. Alabama Colorado Hawaii ...

2. Alaska Connecticut Idaho ...

3. Arizona Delaware Illinois ...

4. Arkansas Florida Indiana ...

5. California Georgia Iowa ...

Do you want to have the display like above ?

yes

<?php
   
$states_array=array("Alabama","Alaska","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","Florida","Georgia","Hawaii","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland","Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshir","New Jersey","New Mexico","New York","North Carolina","North Dakota","Ohio","Oklahoma","Oregon","Pennsylvania","Puerto Rico","Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Vermont","Virginia","Washington","Washington DC","West Virginia","Wisconsin","Wyoming");
$result_array=$states_array;
$noofrows = 5;
$noofcols = ( count($states_array)%5 == 0) ? count($states_array)/5 : ( count($states_array)/5 + 1);
$tabledata = "<table border = '1'>";
for ( $k = 0; $k < $noofrows; $k++ ) {
	$tabledata .= "<tr>";
	for ( $j = 0; $j < $noofcols; $j++ ) {
		$array_index = $k + ($noofrows * $j );
		$tabledata .= "<td>";
		$tabledata .= $result_array[$array_index]; 
		$tabledata .= "</td>"; 
	}
	$tabledata .= "</tr>";
}
$tabledata .= "</table>";
echo $tabledata;

?>

Okay.. here you go.
The above code gives you the output you wanted.

commented: did a greate job. +1
<?php
   
$states_array=array("Alabama","Alaska","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","Florida","Georgia","Hawaii","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland","Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshir","New Jersey","New Mexico","New York","North Carolina","North Dakota","Ohio","Oklahoma","Oregon","Pennsylvania","Puerto Rico","Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Vermont","Virginia","Washington","Washington DC","West Virginia","Wisconsin","Wyoming");
$result_array=$states_array;
$noofrows = 5;
$noofcols = ( count($states_array)%5 == 0) ? count($states_array)/5 : ( count($states_array)/5 + 1);
$tabledata = "<table border = '1'>";
for ( $k = 0; $k < $noofrows; $k++ ) {
	$tabledata .= "<tr>";
	for ( $j = 0; $j < $noofcols; $j++ ) {
		$array_index = $k + ($noofrows * $j );
		$tabledata .= "<td>";
		$tabledata .= $result_array[$array_index]; 
		$tabledata .= "</td>"; 
	}
	$tabledata .= "</tr>";
}
$tabledata .= "</table>";
echo $tabledata;

?>

Okay.. here you go.
The above code gives you the output you wanted.

thank you banu and urtrivedi.

-

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.