Php - How To Create Make Multiple Table Columns From Mysql Database Php
Posted On Thu Dec 14, 2006 By webmaster In Snippets forums topics about simple codes to make programming fun Forums
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:
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,
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
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>
<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');
`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
Gerald Fri Jan 13, 2012
Cannot possibly work.
$num_rows is defined but never used....and this is the ONLY thing that uses the number of columns!
$num_rows is defined but never used....and this is the ONLY thing that uses the number of columns!
John Sun Oct 24, 2010
Hi,
the code looks really simple. After filling the variables, when I check on my browser, it returns the page correctly, but instead of showing a table,
it shows the string:
";} } while ($myrow = mysql_fetch_array($result ?>
Anyone knows why? could it be that it couldn't connect to MySQL database?
the code looks really simple. After filling the variables, when I check on my browser, it returns the page correctly, but instead of showing a table,
it shows the string:
";} } while ($myrow = mysql_fetch_array($result ?>
Anyone knows why? could it be that it couldn't connect to MySQL database?
chris Fri Oct 02, 2009
great script, easy to follow and manipulate.
Jim Sat Sep 05, 2009
<?php
include("../db_connect.php"); = "select distinct(make) from elite_size_chart order by make";
$result = mysql_query($query);
$count = mysql_num_rows($result);
= "elite_size_chart";
$field_name = "make";
$total_records = mysql_num_rows($result);
$num_rows = ceil($total_records / $number_of_colums);
if($result){
if($myrow = mysql_fetch_array($result)){
do{
echo "<table width='yxx%' border='x' cellspacing='y' cellpadding='y'>";
echo "<tr>";
do{
if ($newrowcount == $number_of_colums){
$newrowcount = x;
echo "<tr>";
}
echo "<td>";
echo "<a href='#'>";
echo "<font size='z'>";
echo $myrow[$field_name];
echo "</font>";
echo "</a>";
echo "</td>";
$newrowcount++;
if($newrowcount == $number_of_colums){
echo"</tr>";
}
}
while($myrow = mysql_fetch_array($result));
echo "</tr></table>";
}
while ($myrow = mysql_fetch_array($result));
}
}
?>
include("../db_connect.php"); = "select distinct(make) from elite_size_chart order by make";
$result = mysql_query($query);
$count = mysql_num_rows($result);
= "elite_size_chart";
$field_name = "make";
$total_records = mysql_num_rows($result);
$num_rows = ceil($total_records / $number_of_colums);
if($result){
if($myrow = mysql_fetch_array($result)){
do{
echo "<table width='yxx%' border='x' cellspacing='y' cellpadding='y'>";
echo "<tr>";
do{
if ($newrowcount == $number_of_colums){
$newrowcount = x;
echo "<tr>";
}
echo "<td>";
echo "<a href='#'>";
echo "<font size='z'>";
echo $myrow[$field_name];
echo "</font>";
echo "</a>";
echo "</td>";
$newrowcount++;
if($newrowcount == $number_of_colums){
echo"</tr>";
}
}
while($myrow = mysql_fetch_array($result));
echo "</tr></table>";
}
while ($myrow = mysql_fetch_array($result));
}
}
?>
Gman Wed May 06, 2009
thanks!!!! perfect
Halycon.Prime Sat Apr 04, 2009
your code snippet color/background is unredable. at least under firefox and linux. sites that offer a lot of reading should not have a black background, a very n00bish mistake.
Dave Thu Feb 12, 2009
thanks heaps was exactly what i've been looking for and was easy to follow :)
mike Sat Jan 10, 2009
can you teach me how to get data from db and make a table in php
John Wed Sep 17, 2008
nice clean snippet how can it be split into sections alphabetically like names of towns.
Related Content
Information
Forums »
Snippets forums topics about simple codes to make programming fun »
Php - How To Create Make Multiple Table Columns From Mysql Database Php
Snippets forums topics about simple codes to make programming fun »
Php - How To Create Make Multiple Table Columns From Mysql Database Php
Title: Php - How To Create Make Multiple Table Columns From Mysql Database Php
Description: 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
Tags: php ,computer ,programming ,script
Info: This Post Has Been Viewed 0 Times Since
Date: Thu Dec 14, 2006
Author webmaster Received 9 Replies #1370
Date: Thu Dec 14, 2006
Author webmaster Received 9 Replies #1370
Share
URL: 

Embed: 

To embed this topic, just copy the code from the "Embed" box. Once you've copied the code, just paste it into your website or blog to embed it
BBCODE:: 

BBCODE is use on forums. You can put this code on all your BBCODE enabled forums like PhpBB, vBulletin® and others. Just Copy and Paste this code on your Posts and Replies on your forums
wallpaperama | Wallpapers | Forums | Terms Of Service
copyright © 2013 wallpaperama - All Rights Reserved - Last Updated Mon May 06, 2013 (-8 GMT)
Powered by: Webune Forums V5
copyright © 2013 wallpaperama - All Rights Reserved - Last Updated Mon May 06, 2013 (-8 GMT)
Powered by: Webune Forums V5