PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

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 Dev Shed Forums Sponsor:
  #1  
Old May 7th, 2000, 03:38 PM
acmearts acmearts is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2000
Posts: 4 acmearts User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I have a results page that has to display records in a table that is three columns by an infinite number of rows.

for example:
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre><TR>
<TD>item1</TD><TD>item2</TD><TD>item3</TD>
</TR>
<TR>
<TD>item4</TD><TD>item5</TD><TD>item6</TD>
</TR>
etc...[/code]

I use the following code to run the MySQL statement and format the results into an array

<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
/* select items from table */
$sql = "SELECT *
FROM Table
WHERE ID = '$ID'";

/* execute sql query and get result */
$sql_result = mysql_query($sql, $connection) or die ("Couldn't execute query.");

/* format results by row */
while($row = mysql_fetch_array($sql_result)) {
$item1 = $row["item1"];
$item2 = $row["item2"];
};
[/code]

I'm assuming I have to create a for loop to create table rows of three columns each, but am unsure of how to do this.

Any suggestions.

Reply With Quote
  #2  
Old May 8th, 2000, 07:23 AM
Shiju Rajan's Avatar
Shiju Rajan Shiju Rajan is offline
.Net Developer
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2000
Location: London
Posts: 987 Shiju Rajan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 26 m 22 sec
Reputation Power: 14
Send a message via MSN to Shiju Rajan Send a message via Yahoo to Shiju Rajan
acmearts,

you can use the looping for fetching those record.

try the following ....


if ($row = mysql_fetch_array($sql_result)) {

do {
print $row["item1"];
print $row["item2"];
print $row["item3"];
} while($row = mysql_fetch_array($sql_result));

}else{
print "No Records!!";
}

GOOD LUCK!!!


------------------
SR -
shiju.dreamcenter.net

Reply With Quote
  #3  
Old May 8th, 2000, 09:50 AM
acmearts acmearts is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2000
Posts: 4 acmearts User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I see now that my example was not very clear. My sql statement was correct...
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
/* select items from table */
$sql = "SELECT *
FROM Table
WHERE ID = '$ID'";

/* execute sql query and get result */
$sql_result = mysql_query($sql, $connection) or die ("Couldn't execute query.");

/* format results by row */
while($row = mysql_fetch_array($sql_result)) {
$item1 = $row["item1"];
$item2 = $row["item2"];
};
[/code]

but my table example was misleading. A better representation of what my table needs to look like is:

<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
<TR>
<TD>item1 item2</TD><TD>item1 item2</TD><TD>item1 item2</TD>
</TR>
<TR>
<TD>item1 item2</TD><TD>item1 item2</TD><TD>item1 item2</TD>
</TR>
[/code]

Each table cell should show a different instance of data pulled from the fields item1 and item2.

For example: If item1 is a persons First name, and item2 is the Last name, I could have a 3(columns) by 2(rows) table with different names in each cell.

Reply With Quote
  #4  
Old May 9th, 2000, 02:56 PM
donarb donarb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 1999
Location: Seattle
Posts: 133 donarb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 14
I would do something like:
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
/* select items from table */
$sql = "SELECT * FROM Table WHERE ID = '$ID'";

/* execute sql query and get result */
$sql_result = mysql_query($sql, $connection) or die ("Couldn't execute query.");

/* format results by row */
$x = 0;
while($row =mysql_fetch_array($sql_result)) {
if ($x == 0) print "<TR>";
print "<TD>$row['item1'] $row['items2']</TD>";
$x++;
if ($x == 2) {
$x = 0;
print "</TR>";
}
}
[/code]

Of course you'll have to add code after the loop to add columns if the returned count is not a multiple of three and to close out the table row tag (</TR> ).

Don

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > splitting resutlts into multiple table rows

Developer Shed Advertisers and Affiliates



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

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap