- Forums
- Snippets
- Php - How To Create Make Multiple Table Columns From Mysql Database Php
will show you how to get data from a database and then display it on the browser depending on how many colums you want to use [1370], Last Updated: Sat May 18, 2024
webmaster
Thu Dec 14, 2006
9 Comments
5521 Visits
the purpose of this snippet is to demonstrate how to retrieve data from a mysql database and display it in colums, this will make your website look much cleaner.
The only requirement for this to work on your web pages is that you have PHP in your hosting. IF you don't have php, you can signup with www.webune.com for a php plan. if you have php, continue on...
To show you how this work, i've provide a sample file i use often as a template when i am displaying mysql data into a talble in html, so here is the code to copy and paste into your text editor like notepad:
MAKE SURE TO EDIT THE PART IN THE CONFIGURATION SECTION TO WHATEVER YOU USERNAME, PASSWORD, DATABASE NAME, ETC..In my example here, i want to display all the topic titles from the topics table in the forums database. MySQl host is called "localhost", my username is user1, the password is P@ssw and the database is called forums, and I will be displaying all the titles from the topics table in 3 colums of the a table. this is how the PHP code would look like:
CODE:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Wallpaperama Tutorials - Display Number of Colums from a Database</title>
</head>
<body>
<h1 align="center">Display Number of Colums from a Database</h1>
<hr />
<?php
# ************* SCRIPT BY WALLPAPERAMA.COM/FORUMS ***************
################### S T A R T C O N F I G U R A T I O N ######################
# SET THE NUMBER OF COLUMS IN THE TABLE
$number_of_colums=3;
# SET YOUR MYSQL HOSTNAME, USERNAME, PASSWORD AND DATABASENAME
$db = mysql_connect("MYSQL_hostname", "user_name", "password");
mysql_select_db("database_name",$db);
# ENTER NAME OF TABLE TO BE displayed
$table_name = "topics"; // change to whatever table you want to get data from
$field_name = "topic_title"; // change to whatever field name from the $table_name
################### E N D C O N F I G U R A T I O N ######################
####################################################################################
#################### STOP HERE - NO NEED TO CHANGE FROM THIS POIN #################
####################################################################################
$sql = "SELECT $field_name FROM $table_name";
$result = mysql_query($sql ,$db);
$total_records = mysql_num_rows($result);
$num_rows = ceil($total_records / $number_of_colums);
if ($result)
{
if ($myrow = mysql_fetch_array($result))
{
do
{
?><table width="100%" border="0" cellspacing="5" cellpadding="5">
<tr>
<?php
do
{
if ($newrowcount == $number_of_colums)
{
$newrowcount = 0;
?><tr>
<?php
}
?><td>
<?php
################### DISPLAY cell info ##########
?><a href="#"><?php echo $myrow[$field_name]; ?></a><?php
################### DISPLAY cell info ##########
?></td>
<?php
$newrowcount++;
if ($newrowcount == $number_of_colums) {echo"</tr>";}
}
while ($myrow = mysql_fetch_array($result));
?></tr></table><?php
} while ($myrow = mysql_fetch_array($result));
}
}
?>
<div align="center">
<p align="left"><a href="http://www.wallpaperama.com/forums">< < Go back to forums </a><br />
</p>
<p>Hosting by <a href="http://www.webune.com">Webune.com</a></p>
</div>
</body>
</html>
Now that you have copied and paste the code, make sure to change the configuration section to match whatever you mysql information you are going to retrieve from your database.
now save it ast wallpaperma-colums.php and upload to your website to see it in action.
here is the mysql data you can use,
CREATE TABLE IF NOT EXISTS `topics` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`topic_title` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
--
-- Dumping data for table `topics`
--
INSERT INTO `topics` (`id`, `topic_title`) VALUES
(1, 'how to work with mysql'),
(2, 'how to use php on your websites'),
(3, 'page 3 how to view all topics on a table'),
(4, 'page 4 - using mysql and php to store data');
We hope this short and easy tutorial guide has help you in undertand how you can display many columns and row on a table and retrieve the data from a database and show it on your web pages through a PHP loop.
Courtesy of Webune.com Support
https://www.wallpaperama.com/forums/php-how-to-create-make-multiple-table-columns-from-mysql-database-php-t605.html