Welcome to Dream.In.Code
Become a PHP Expert!

Join 137,420 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,974 people online right now. Registration is fast and FREE... Join Now!




while loop !

 
Reply to this topicStart new topic

while loop !

myharshdesigner
10 Jan, 2008 - 07:04 PM
Post #1

New D.I.C Head
*

Joined: 26 Dec, 2007
Posts: 24


My Contributions
i have some query.

lest take an example :-
CODE

<table>
<tr>
  <td></td>
  <td></td>
</tr>
</table>




now i want to set data in both TD tag but on the bases of result.

suppose we have 3 records in the table.
so we have to display records in some pattern.

for 1st 2 rows.

it will use
<td></td>
<td></td>


but for the 3rd row.

1st it will create new row and the it will use only 1 TD tab
<td></td>

in last row it not print 2nd TD tag because there are not any more records.

so cany one tell me how to do that ?
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: While Loop !
10 Jan, 2008 - 10:50 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,260



Thanked: 227 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
The trick to this is using the modulus operator. As you loop through the records, you want to use a counter. The counter will be mod against 3. For instance....

CODE

$counter = 1;

// We loop through a recordset we pulled from the database.
while ($row = mysql_fetch_array($result)) {
     if (($counter % 3) == 1) {
          echo "<tr><td>{$row['field1']}</td>";
     }
     else if (($counter % 3) == 2) {
          echo "<td>{$row['field2']}</td>";
     }
     else { echo "<td>{$row['field3']}</td></tr>"; }

     $counter++;
}

// Test to see if we need a closing row tag (count was not divisible by 3)
if ((($counter - 1) % 3) > 0) { echo "</tr>"; }


So what this does is this, if the counter was 5 it would first trigger the first if creating <tr><td>{$row['field1']}</td> and then it would go 2 and print <td>{$row['field2']}</td> and then it would go 3 and print <td>{$row['field3']}</td></tr> closing off the row. It would then go 4 and print <tr><td>{$row['field1']}</td> and lastly hit 5 and print <td>{$row['field3']}</td></tr> and exit the loop, but it would check 5 % 3 = 2 so it would tack on the closing row tag. Resulting in an output of...

CODE

<tr><td>data1</td>
<td>data2</td>
<td>data3</td></tr>
<tr><td>data4</td>
<td>data5</td>
</tr>


which as you can see will give you one row of 3 items and a second row of two items.

Hopefully you get the idea by manipulating the counter with a mod operator (%) you will be able to dictate the pattern. It will only create a new row when the multiple has been filled. In this instance, after it hits three cells, it will start a new row.

smile.gif

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/5/08 04:22AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month