hello

I am trying to display some form values. Of course no problem I can display that particular data by using That my following code it is perfectly working. but I need to display those data in two column.Actually those data containing Names.I need to put those name according their First name A to M in first column and N - Z names are second column

ex-


|Column A | Column B |

| A | N |

| B | O |

| C | P |

| . | . |

| . | . |

| M | Z |


I am try to use while loop for it.

<table>
<?php
require "conetion.php";
$query = "SELECT * FROM forms WHERE active = '1' ORDER BY Name ASC";
$result=mysql_query ($query);

$total_rows = mysql_num_rows($result);
$count = 1;

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

    if($count==1 || $count%2==1)
    {
        echo '<tr>';
    }
   
    //echo "<td>". $row['text_2'] . "</td>";
	
	echo "<td>";
	echo '<div >'.$row['Name'].'</div>';
   	echo "</td>";
   
    if($count == $total_rows || $count%2 == 0)
    {
        echo '</tr>';
    }

$count++;
  }
  
?>  </table>

Ok I am agree with you here in my code I had lot of mistake.ok If can tell me either very better way to achieve this my requirement or other proper solution I am so glad.

cheers.

Umanda Jayo Bandara

Recommended Answers

All 7 Replies

You should start putting names in the first td, until you hit N. Then switch to the second. Add br tags after the names.

it could be helpful to you.

<?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 = 10;
$noofcols = ( count($states_array)%5 == 0) ? count($states_array)/5 : ( count($states_array)/5 + 1);
$tabledata = "<table >";
for ( $k = 0; $k < $noofrows; $k++ ) {
	$tabledata .= "<tr>";
	for ( $j = 0; $j < $noofcols; $j++ ) {
		$array_index = $k + ($noofrows * $j );
		$tabledata .= "<td>";
		$tabledata .= <<<EOD
		<a href=referecne_page.php?stateid=$result_array[$array_index] class=links>$result_array[$array_index]
EOD;
		$tabledata .= "</td>";
	}
	$tabledata .= "</tr>";
}
$tabledata .= "</table>";
echo $tabledata;

?>

I have made this code..
Check this out.

<table>
<?php
	require "conetion.php";
	
	$query = "SELECT * FROM forms WHERE active = '1' ORDER BY Name ASC";
	$result=mysql_query ($query);
	
	$total_rows = mysql_num_rows($result);
	$count = 1;

	while($row = mysql_fetch_array($result))
	{
		$all[] = $row['Name'];
	}
	$halfCount =  ceil(count($all)/2) ;
	$ptr1 = 0;
	$ptr2 = $halfCount;
	for($i=0;$i<$halfCount;$i++)
	{
?>
	<tr>
		<td><?=$all[$ptr1]?></td>
		<td><?=$all[$ptr2]?></td>
	</tr>
<? 
		$ptr1++;
		$ptr2++;
	} ?>  
</table>

Thank you all of you...all are awesome..

ok hello Mr vibhadevit your code is perfectly working no problem. but in your code you try to get all the fields and you try to dived it two column.it is ok but what I am going to telling you...I am telling what hapen if the data base have 2 or more same letters...if you can not get me...try this..

<?php
echo '<table>';

	$db = @mysql_connect("localhost", "root", "");
if( ! ($db = @mysql_connect("localhost", "root", "")) ) {
} else {
mysql_select_db("test",$db) or die("Select DB Error: ".mysql_error());
}
	
	$query = "SELECT * FROM test_1 ";
	$result=mysql_query ($query);
	
	$total_rows = mysql_num_rows($result);
	$count = 1;

	while($row = mysql_fetch_array($result))
	{
		$all[] = $row['Name'];
	}
	$halfCount =  ceil(count($all)/2) ;
	$ptr1 = 0;
	$ptr2 = $halfCount;
	for($i=0;$i<$halfCount;$i++)
	{

	echo '<tr><td>'.$all[$ptr1].'</td><td>'.$all[$ptr2].'</td></tr>';

		$ptr1++;
		$ptr2++;
	}  
    ?>
 
</table>

use this sql it is help you

CREATE TABLE IF NOT EXISTS `test_1` (
  `id` int(255) NOT NULL,
  `Name` varchar(255) NOT NULL,
  `City` varchar(255) NOT NULL,
  `char` varchar(255) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `test_1`
--

INSERT INTO `test_1` (`id`, `Name`, `City`, `char`) VALUES
(26, 'Z', '1', '1'),(25, 'Y', '1', '1'),(24, 'X', '1', '1'),(23, 'W', '1', '1'),
(22, 'V', '1', '1'),(21, 'U', '1', '1'),(20, 'T', '1', '1'),(19, 'S', '1', '1'),
(18, 'R', '1', '1'),(17, 'Q', '1', '1'),(16, 'P', '1', '1'),(15, 'O', '1', '1'),
(14, 'N', '1', '1'),(13, 'M', '1', '1'),(12, 'L', '1', '1'),(11, 'K', '1', '1'),
(10, 'J', '1', '1'),(9, 'I', '1', '1'),(8, 'H', '1', '1'),(7, 'G', '1', '1'),
(6, 'F', '1', '1'),(5, 'E', '1', '1'),(4, 'D', '1', '1'),(3, 'C', '1', '1'),
(2, 'B', '1', '1'),(1, 'A', '1', '1'),(52, 'Z', '1', '1'),(51, 'Y', '1', '1'),
(50, 'X', '1', '1'),(49, 'W', '1', '1'),(48, 'V', '1', '1'),(47, 'U', '1', '1'),
(46, 'T', '1', '1'),(45, 'S', '1', '1'),(44, 'R', '1', '1'),
(43, 'Q', '1', '1'),(42, 'P', '1', '1'),(41, 'O', '1', '1'),
(40, 'N', '1', '1'),(39, 'M', '1', '1'),(38, 'L', '1', '1'),
(37, 'K', '1', '1'),(36, 'J', '1', '1'),(35, 'I', '1', '1'),
(34, 'H', '1', '1'),(33, 'G', '1', '1'),(32, 'F', '1', '1'),
(31, 'E', '1', '1'),(30, 'D', '1', '1'),(29, 'C', '1', '1'),
(28, 'B', '1', '1'),(27, 'A', '1', '1');

ok I think now you can understand my what I say....but this is not the way I am find...but exactly your code is so interesting..tank you...but I need like this


|Column A | Column B |

| A_1 | N_1 |

| A_2 | N_2 |

| A_3 | N_3 |

| B_1 | N_4 |

| B_2 | O_1 |

| B_2 | P_1 |

| B_2 | P_2|

| C_1 | P_3 |

| C_1 | P_4 |

| . | . |

| . | . |

| M_1 | Z_1 |
| M_2 | Z_2 |
| M_3 | Z_3 |

THANK YOU.....

umanda

Hi umanda,
first of all, i am Miss vibha not Mr. ;)

If you will have query with order by name:
$query = "SELECT * FROM test_1 order by Name ";

then you will have output exact what you want no matters the data base have 2 or more same letters .
Even you can define numberOfColumn to one variable rather having fix '2'.
Based on numberOfColumn <tr> will be also in if condition.

Kindly share your thought if you want dynamic column.

OMG Miss Vibha...I am so sorry..yep Agree it is perfectly working...Yes of course I need a dynamic column..

This is dynamic coding for you,
you can change $numberOfColumn = 5; // here 5 column is generated

<table>
<?php
	
	$query = "SELECT * FROM test_1 order by Name ";
	$result=mysql_query ($query);
	
	$total_rows = mysql_num_rows($result);
	$count = 1;
	$numberOfColumn = 5;

	while($row = mysql_fetch_array($result))
	{
		$all[] = $row['Name'];
	}
	
	$masterCount =  ceil(count($all)/$numberOfColumn) ;
	
	for($i=0;$i<$masterCount;$i++)
	{
		echo '<tr>';
		for($j=0;$j<$numberOfColumn;$j++)
		{
			echo '<td>&nbsp;&nbsp;'.$all[(($masterCount*$j)+$i)].'</td>';
		}		
		echo '</tr>';
		
	}  
?> 
</table>

Hope this is exactly what you want.. !!!!

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.