Page 1 of 1

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

Posted: Thu Jan 09, 2014 12:58 am
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!

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

Posted: Thu Jan 09, 2014 1:31 am
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

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

Posted: Thu Jan 09, 2014 2:34 am
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!

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

Posted: Thu Jan 09, 2014 3:11 am
by social_experiment
;) apologies; misread & misunderstood your question.
http://stackoverflow.com/questions/9464 ... n-each-row
hth

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

Posted: Thu Jan 09, 2014 11:46 am
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;
    }