Created
September 9, 2015 08:44
-
-
Save sanjaybhowmick/82cec8b3500672e47502 to your computer and use it in GitHub Desktop.
Display data horizontally from MySQL table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP TABLE IF EXISTS `member_table`; | |
CREATE TABLE `member_table` ( | |
`id` tinyint(4) NOT NULL AUTO_INCREMENT, | |
`member_name` varchar(20) NOT NULL, | |
PRIMARY KEY (`id`) | |
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1; | |
-- ---------------------------- | |
-- Records of member_table | |
-- ---------------------------- | |
INSERT INTO `member_table` VALUES ('1', 'Sir Isaac Newton'); | |
INSERT INTO `member_table` VALUES ('2', 'Leonardo Vinchi'); | |
INSERT INTO `member_table` VALUES ('3', 'William Shakespeare'); | |
INSERT INTO `member_table` VALUES ('4', 'Adolf Hitler'); | |
INSERT INTO `member_table` VALUES ('5', 'Siddhartha Gautama'); | |
INSERT INTO `member_table` VALUES ('6', 'Moses'); | |
INSERT INTO `member_table` VALUES ('7', 'Abraham'); | |
INSERT INTO `member_table` VALUES ('8', 'Jesus of Nazareth'); | |
INSERT INTO `member_table` VALUES ('9', 'Walt Disney'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
mysql_connect ('localhost','root','password'); | |
mysql_select_db ('test'); | |
?> | |
<table> | |
<tr> | |
<?php | |
$sql_result = mysql_query ("SELECT member_name FROM member_table") or die ('Unable to select'. mysql_error()); | |
$record_count = 0; //Keeps count of the records echoed. | |
while ($row=mysql_fetch_row($sql_result)) | |
{ | |
//Check to see if it is time to start a new row | |
//Note: the first time through when | |
//$record_count==0, don't start a new row | |
if ($record_count % 3==0 && $record_count != 0) | |
{ | |
echo '</tr><tr>'; | |
} | |
echo '<td>'; | |
//Echo out the entire record in one table cell: | |
for ($i=0; $i< count($row); $i++) | |
{ | |
echo $row[$i]; | |
} | |
echo '</td>'; | |
//Indicate another record has been echoed: | |
$record_count++; | |
} | |
?> | |
</tr> | |
</table> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank You so much