PHP - How To Create Make Multiple Table Columns From MYSQL Database PHP

Mobile
feeds
Welcome Login | Register

PHP - How To Create Make Multiple Table Columns From MYSQL Database PHP
Reply Your Comments:
Click this button if you are interested in replying to this topic and leave your comments
Sent To Friend
CLick this button if you want to send this page to a friend.
Subsribe To Rss Feeds
Subscribe to RSS
CLick this button if you want to subscribe to this RSS Feed. You can use your browsers feeds burners if you have mozilla or internet explorer 7 or higher and keep up with updates.
  Forums Index
      » Snippets Forums
        » » PHP - How To Create Make Multiple Table Columns From MYSQL Database PHP
PHP - How To Create Make Multiple Table Columns From MYSQL Database PHP
Post Description: php how to create make multiple table columns from mysql database php Snippets
Post Tags:
This Post Has Been Viewed 1635 Times Since Thu Dec 14, 2006 6:08 am Author webmaster with 7 replies
PHP - How To Create Make Multiple Table Columns From MYSQL Database PHP
Advertise On This Page




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">&lt; &lt; 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.

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


Leave Your Comments

Your Name
Your Email Address (Will Not Be Published)
Notify Me When Someone Replies to this Page
(An email will be sent to you when someone replies to your comments)
Your Comments
Include A Picture with your comments
Share
| More
Share this page by putting this URL in your comments to other websites like myspace, Facebook, Twitter friendster, Hi5, Groups, Boards, Forum or others. Just Copy and Paste this Code
URL:
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.
Embed:
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
BBCODE:
Subscribe Feeds
Webmasters - Exchange Links With Us. Add related websites to this topic
Add Link:
Links Related to : PHP - How To Create Make Multiple Table Columns From MYSQL Database PHP

Comments and replies About PHP - How To Create Make Multiple Table Columns From MYSQL Database PHP
:: 1 :: Reply #70942 Reply By John On Wed Sep 17, 2008 11:55 am
John:
nice clean snippet how can it be split into sections alphabetically like names of towns.
:: 2 :: Reply #89539 Reply By mike On Sat Jan 10, 2009 8:19 pm
mike:
can you teach me how to get data from db and make a table in php
:: 3 :: Reply #93720 Reply By Dave On Thu Feb 12, 2009 3:37 pm
Dave:
thanks heaps was exactly what i've been looking for and was easy to follow :)
:: 4 :: Reply #99260 Reply By Halycon.Prime On Sat Apr 04, 2009 2:52 am
Halycon.Prime:
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.
:: 5 :: Reply #102566 Reply By Gman On Wed May 06, 2009 10:15 pm
Gman:
thanks!!!! perfect
:: 6 :: Reply #114852 Reply By Jim On Sat Sep 05, 2009 6:54 pm
Jim:
<?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));

}
}
?>
:: 7 :: Reply #116991 Reply By chris On Fri Oct 02, 2009 12:45 pm
chris:
great script, easy to follow and manipulate.