|
|
|
| Title : |
Output database records in multiple table columns.
multiple, table, columns, output, format, query output, html table, php |
| Categories : |
General SQL, Algorithms, PHP, MySQL |
 Christopher Chaduka |
| Date : |
Jul 13th 2000 |
| Grade : |
1 of 5 (graded 1 times) |
| Viewed : |
10845 |
| File : |
php_multiple_columns.txt
|
| Images : |
No Images for this code example. |
|
| Search : |
More code by Christopher Chaduka |
|
| Action : |
Grade This Code Example
|
|
| Tools : |
My Examples List |
|
|
|
|
|
|
Say you have the following database table:
database name: db_business
table name: tbl_clients
---------------------------------------------------
|client_name|client_age|client_city |client_salary|
---------------------------------------------------
|Bob | 42|Dubai | 12000|
---------------------------------------------------
|Pinky | 18|Harare | 6000|
---------------------------------------------------
|Susan | 23|Cape Town | 57000|
---------------------------------------------------
|Brain | 102|Earth | 0|
---------------------------------------------------
The following illustrates how to output the results in the following HTML table:
----------------------
|Bob |Pinky |
|42 |18 |
|Dubai |Harare |
|12000 |6000 |
----------------------
|Susan |Brain |
|23 |102 |
|Cape Town |Earth |
|57000 |0 |
----------------------
<?php
mysql_connect("localhost","your_username","your_password");
$sql_client_details = "SELECT * FROM tbl_clients";
$sql_client_details_result = mysql_db_query("your_database",$sql_client_details);
// desired columns in a table
$num_cols = 2;
// dummy variable to hold our loop count
$while_count = 1;
// number of items we need to output. this number is determined by the number of
// records returned by our query
$num_records = mysql_num_rows($sql_client_details_result);
// start a new table row
$new_row = 1;
// count the number of <td>s we have so far in this row
$td_count = 0;
// I want to use colspan for the last row if it's incomplete.
// Set this to any other value other than 1 if you do not want to span --UGLY--
$want_to_colspan = 1;
// the total number of table rows we will end up having
$num_rows = ceil($num_records / $num_cols);
// number of rows we have so far
$row_number = 0;
print "<table border=\"1\">\n";
while ($client_details_array = mysql_fetch_array($sql_client_details_result)) {
$client_name = $client_details_array["client_name"];
$client_age = $client_details_array["client_age"];
$client_city = $client_details_array["client_city"];
$client_salary = $client_details_array["client_salary"];
if °LžÏÈNžÏÐNžÏDÀ n=All+Categor>\n";
$new_row = 0; // don't start a new table row
$row_number++;
}
if (($want_to_colspan == 1) && ($row_number == $num_rows) && ($while_count
== $num_records)) {
// we want to colspan the remaining table columns
$num_cols_to_span = $num_cols - $td_count;
print "<td colspan=\"$num_cols_to_span\">\n";
print "$client_name<br>\n";
print "$client_age<br>\n";
print "$client_city<br>\n";
print "$client_salary\n";
print "</td>\n";
$td_count++; // not necessary as this is the last row anyway
} else {
// just print
print "<td>\n";
print "$client_name<br>\n";
print "$client_age<br>\n";
print "$client_city<br>\n";
print "$client_salary\n";
print "</td>\n";
$td_count++;
}
if ($td_count == $num_cols) {
// close the table row since we have the required columns in one table
row
print "</tr>\n";
$new_row = 1; // we need to start a new row
$td_count = 0;
}
$while_count++;
}
// HTML compliance and for Netscape && similar browsers' sake!!
if (($num_records % $num_cols) != 0) {
print "</tr>\n";
}
print "</table>\n";
mysql_free_result($sql_client_details_result);
?>
http://site.mweb.co.zw/users/kiri/php_multiple_columns.cfm |
|
| Point and Click Interface ala MS Access for creating SQL statements. Categories : MySQL, Complete Programs, General SQL, PHP, Databases | | | A very simple way to build and do a hierarchical html categories browser without javascript , just using html php and mySql
Categories : HTML and PHP, Databases, Algorithms, PHP, MySQL | | | Browse a MySQL database & draw a tree view & load final items into a template page. Categories : MySQL, Complete Programs, Algorithms, PHP, Databases | | | This function will populate the options in a drop down HTML select list
in a form from a database query.
Categories : MySQL, General SQL, PHP, HTML and PHP, Databases | | | I`d like to use the mysql_fetch_row function along with a "randomizer"
function that would give me a random result from a mySQL table. Categories : General SQL, MySQL, PHP, Databases | | | phpEasySQL - Easily connect to your MySQL database with just 1 php file and 3 easy steps! Categories : Databases, PHP, MySQL, General SQL | | | Paginating the mySQL data Categories : PHP, Algorithms, Databases, MySQL, HTML and PHP | | | You have a set of many resources , and You need to know wich of these resources are available for a given use during a given period but MySql does not allow SUB-Selection :(
here is the right way to do that in a single query
Categories : MySQL, General SQL, Databases, Algorithms | | | bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager Categories : MySQL, PHP, MySQL, Complete Programs, Databases | | | A very simple and efficient split bar the B-Z bar , for mysql and php ...
Tired of obfuscated code try this one ...
Categories : PHP, Databases, MySQL, Algorithms | | | Writing Portable MySQL Code in PHP: Porting to Oracle, Microsoft SQL
Server, Sybase, Interbase, PostgreSQL and other databases using ADODB
class library. Categories : MySQL, PHP, PHP Classes, ODBC, General SQL | | | Multiple Search using PHP and Mysql Categories : PHP, Databases, General SQL, MySQL | | | Displaying records of database in more than one page (paging) Categories : Databases, MySQL, PHP | | | Message of the Day - Random Message (Needs MySQL!) Categories : Databases, HTML and PHP, PHP, MySQL | | | Retrieve text from table and email to your e-
address in pipe delimited format. Categories : PHP, MySQL | |
|
|
|