php script error 3 tables per row the first only shows 2

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chakytori
Forum Newbie
Posts: 3
Joined: Thu Jan 09, 2014 12:55 am

php script error 3 tables per row the first only shows 2

Post by chakytori »

Hi, am having trouble with a script to put 3 fields in a row it only shows 2 in the first row and then it shows 3 as it suppose to, the code is:

Code: Select all

<table><tr> 

<?php 

mysql_connect("localhost", "user","password") or die ("error"); 
mysql_select_db("database") or die ("error"); 

    $result = mysql_query ("select * from table order by RAND()") or die (mysql_error()); 

$i = 0; 

while ($row = mysql_fetch_array($result)) :    $i ++; 

    if($i==3) { 
        echo "</tr><tr>"; 
        $i = 0; 
    } 
?> 

<td> 

<table><tr><td> 
TABLE EXAMPLE 
</td></tr></table> 

</td> 

<?php endwhile; ?> 

</tr></table>
Any help will be appreciated, thanks!
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: php script error 3 tables per row the first only shows 2

Post by social_experiment »

Code: Select all

$result = mysql_query ("select * from table order by RAND() LIMIT 3") or die (mysql_error());
selects only 3 rows; easier to work with
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
chakytori
Forum Newbie
Posts: 3
Joined: Thu Jan 09, 2014 12:55 am

Re: php script error 3 tables per row the first only shows 2

Post by chakytori »

I need to select all the data on the database to show the results, selecting only 3 of them would not do the trick, thanks for the reply!
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: php script error 3 tables per row the first only shows 2

Post by social_experiment »

;) apologies; misread & misunderstood your question.
http://stackoverflow.com/questions/9464 ... n-each-row
hth
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
chakytori
Forum Newbie
Posts: 3
Joined: Thu Jan 09, 2014 12:55 am

Re: php script error 3 tables per row the first only shows 2

Post by chakytori »

Finally i did it, code:

Code: Select all

$i = 0;

while ($row = mysql_fetch_array($result)) :	$i ++;

    if($i==4) {

        echo "</tr><tr>";
        $i = 1;
    }
Post Reply