Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Wouldn't know what to say
Old 08-15-2009, 05:03 AM Wouldn't know what to say
Super Talker

Posts: 102
Trades: 0
So what I want when I echo my rows from the DB is to break a <tr> every 4 rows

Example:

Code:
<table>
<tr>
<td>A</td>
<td>B</td>
<td>C</td>
<td>D</td>
</tr>
<tr>
<td>E</td>
<td>F</td>
<td>G</td>
<td>H</td>
</tr>
</table>
What I have

PHP Code:
$query mysql_query("SELECT * FROM users") or die(mysql_error()); 
while (
$row mysql_fetch_array($query)){
echo 
'<td>'.$row['name'].'</td>';";

Aaron™ is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-15-2009, 05:54 AM Re: Wouldn't know what to say
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
This should do it.
PHP Code:
$i 0;
while (...) {
   echo 
'<td>' $row['name'] . '</td>';
   if (
$i++ == 4) {
      
$i 0;
      echo 
'</tr><tr>';
   }

__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 08-15-2009, 06:59 AM Re: Wouldn't know what to say
Super Talker

Posts: 102
Trades: 0
Shows 5 rows across, What I want is 4.

| 1 | 2 | 3 | 4 |

Simple fix,
PHP Code:
if ($i++ == 3){ 
Does what I wanted it to now. Thanks.
Aaron™ is offline
Reply With Quote
View Public Profile
 
Old 08-15-2009, 02:16 PM Re: Wouldn't know what to say
NullPointer's Avatar
Will Code for Food

Posts: 2,784
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by Aaron™ View Post
Simple fix,
PHP Code:
if ($i++ == 3){ 
It may work, but that is terrible syntax. When you look at a line of code you should have an idea of what it does. Does that mean when i is equal to 4 do what is in the body of the if statement, or when it is 3? In a month from now looking back at this code will you be able to tell?

Since you reset i before referencing it again after that line of code, there is no reason to increment it.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 08-15-2009 at 02:17 PM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 08-15-2009, 04:09 PM Re: Wouldn't know what to say
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Quote:
Originally Posted by NullPointer View Post
Does that mean when i is equal to 4 do what is in the body of the if statement, or when it is 3? In a month from now looking back at this code will you be able to tell?
Yes, I will.
($i++ == 4) will first compare $i to 4, and then increment $i. However, of course it should have been 3 instead of 4, as Aaron pointed out.

I guess your looking for something like this instead Matt, in order to not do one increase more than necessary?
PHP Code:
$i 0;
while (...) {
   echo 
'<td>' $row['name'] . '</td>';
   if (
$i == 3) {
      
$i 0;
      echo 
'</tr><tr>';
   } else {
      
$i++;
   }

__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Reply     « Reply to Wouldn't know what to say
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.22237 seconds with 12 queries