Tutorials
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOtherTutorials

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old October 17th, 2008, 06:27 PM
skullzy's Avatar
skullzy skullzy is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: belfast
Posts: 110 skullzy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 5 h 40 m 19 sec
Reputation Power: 3
Thumbs up Multicolumn Output from a Database with PHP a more efficient way

before beginning this is more of a simple script than a tutorial
so don't expect long explanations this was made in a spare of the moment thing

example of how it wuld show
Code:
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4


this script allows you to have so many columns per row it can come in handy

here is the code you will be using.

PHP Code:
 $columns '4'//this is allowing x amount of columns per row

($i $columns) ? $row FALSE $row TRUE; if($i && $row) { echo '<tr></tr>';} $i++;


//if you dont want to use $columns = '4'; you can always remove it from the script and add in your own numeric

($i 4) ? $row FALSE $row TRUE; if($i && $row) { echo '<tr></tr>';} $i++; 


a little break down

first bit of this code am showing you is another way of doing an ifelse statment so this

($i % $columns) ? $row = FALSE : $row = TRUE;

is basicaly

if($i % $columns) { $row = FALSE; } else { $row = TRUE; }

normaly when your using this version of an ifelse statement it would go something like this.

$me = ($_GET['me'] == 1) ? echo $_GET['me'] : echo 'sorry it does not == 1';

then you would echo $me; and it would either echo 1 or the other

next part is simple if($i && $row) { echo '<tr></tr>';} $i++;

which basicaly means if row exceeds columns limit then create another row after that you stick $i++ on then end which basicaly means

"add 1 and return the value of i-1" or "return the value of i, then add 1 to it"

when using this script you use it as shown

PHP Code:
//STEP 1 create your table but leave out <tr></tr>
<table border="0" cellspacing="2" cellpadding="2"> </table>

//STEP 2 inside your table open up php brackets and add your mysql code
<table border="0" cellspacing="2" cellpadding="2">

//when it comes to creating your query you do not have to do it identical to mine what ever way you do it is fine
$Q mysql_query("SELECT * FROM `table`") or die("ERROR: "mysql_error());

while(
$data mysql_fetch_array($Q)) {


}

</
table>

//STEP 3 inside your while loop you want to add your multicolumn code
<table border="0" cellspacing="2" cellpadding="2">


//when it comes to creating your query you do not have to do it identical to mine what ever way you do it is fine
$Q mysql_query("SELECT * FROM `table`") or die("ERROR: "mysql_error());

while(
$data mysql_fetch_array($Q)) {

//the script always goes after the while loop but before the information
($i $columns) ? $row FALSE $row TRUE; if($i && $row) { echo '<tr></tr>';} $i++;



}

</
table>

//FINAL STEP add your information between <td></td>

<table border="0" cellspacing="2" cellpadding="2">

//when it comes to creating your query you do not have to do it identical to mine what ever way you do it is fine
$Q mysql_query("SELECT * FROM `table`") or die("ERROR: "mysql_error());

while(
$data mysql_fetch_array($Q)) {

//the script always goes after the while loop but before the information
($i $columns) ? $row FALSE $row TRUE; if($i && $row) { echo '<tr></tr>';} $i++;

echo 
"<td>"$data['data'] ."</td>";

}

</
table


its pretty simple, easy and efficient i personally use this for the likes of rosters to show the users profile image and username.

but what ever you think you can use it for enjoy

p.s i appologies if this looks in anyways messy kind of a quick type don't have alot of time to sit down and go through it.

but the script works with no problems because i'm using it obviously. anyways enjoy
__________________
the best way to learn
1. check the site because it rules
2. search your functions up on php.net
3. surf the web google.com
4. lookup tutorials tutorialized.com

Reply With Quote
Reply

Viewing: Codewalkers ForumsOtherTutorials > Multicolumn Output from a Database with PHP a more efficient way


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek