Bravat Posted January 27, 2011 Share Posted January 27, 2011 First the code: <?php $columns = 4; $num_results = mysql_num_rows($result); $col_rows = intval($num_results / $columns); $count = 1; ?> <table cellspacing="2" class="tabela"> <tr> <td> <?php while ($row = mysql_fetch_array($result)) { $count = $count++; $title = $row["Rim"]; $model = $row["model"]; $dimenzija = $row["Dimenzija"]; $ime = $row['name']; echo "$model<br>$dimenzija<br>$ime" ; if ($count == $col_rows) { echo "</td>\n"; echo "<td>\n"; $count = 1; }} ?> </td> </tr> </table> (table is 100% width and td is 25%) Problem with this is that td will go in linear like structure. I want it after 4 td's to go into new row (something like <br> tag). How to do this? Also when their is one result, td is 100% width (it stretch across hole screen). How to define it to be 25% at all time? Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted January 27, 2011 Share Posted January 27, 2011 after 4 td's close the row with a /tr then open a new row with tr and continue Quote Link to comment Share on other sites More sharing options...
Bravat Posted January 27, 2011 Author Share Posted January 27, 2011 It seams that method does not work. Any other thoughts? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 27, 2011 Share Posted January 27, 2011 What do mean it doesn't work? Post the code you tried that doesn't work. Quote Link to comment Share on other sites More sharing options...
Bravat Posted January 27, 2011 Author Share Posted January 27, 2011 <?php $columns = 4; $num_results = mysql_num_rows($result); $col_rows = intval($num_results / $columns); $count = 1; ?> <table cellspacing="2" class="tabela"> <tr> <td> <?php while ($row = mysql_fetch_array($result)) { $count = $count++; $title = $row["Rim"]; $model = $row["model"]; $dimenzija = $row["Dimenzija"]; $ime = $row['name']; echo "$model<br>$dimenzija<br>$ime" ; if ($count == $col_rows) { echo "<br>$count</td></tr><tr>"; echo "<td>"; $count = 1; } }?> </td> </tr> </table> It seems that counter does not work. The echo result of $count is 1. Quote Link to comment Share on other sites More sharing options...
Bravat Posted January 27, 2011 Author Share Posted January 27, 2011 I managed to make it work. Here is the code $go = TRUE; ?> <table cellspacing="2" class="tabela"> <?php while ($go) { ?> <tr> <?php for ($i = 0; $i < 4; $i ++) { if (!($row = mysql_fetch_array($result))) { $go = FALSE; CONTINUE; } $title = $row["Rim"]; $model = $row["model"]; $dimenzija = $row["Dimenzija"]; $ime = $row['name']; echo "<td>$model<br>$dimenzija<br>$ime</td>"; } ?> </tr> <?php } ?> </table> <?php unset ($row); unset ($Go); ?> But the problem with row width is still there. If there is only 1 result it will be 100% wide, 2 results 25% wide and so on. Any idea how to stop this? Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted January 27, 2011 Share Posted January 27, 2011 couple ideas: modify your CSS for class tabela td's or set the td widths explicitly using width='150' where 150 is pixels Quote Link to comment Share on other sites More sharing options...
Bravat Posted January 27, 2011 Author Share Posted January 27, 2011 I looked around the web and there is one way to make it work. It is needed to add rows to fill the gap, but at the moment i don't know how to do this. I tried this code but it does not work. $go = TRUE; ?> <table cellspacing="2" class="tabela"> <?php while ($go) { ?> <tr> <?php for ($i = 0; $i < 4; $i ++) { if (!($row = mysql_fetch_array($result))) { $go = FALSE; CONTINUE; } $title = $row["Rim"]; $model = $row["model"]; $dimenzija = $row["Dimenzija"]; $ime = $row['name']; echo "<td>$model<br>$dimenzija<br>$ime</td>"; } if ($i < 4){ echo "<td></td>"; } ?> </tr> <?php } ?> </table> <?php unset ($row); unset ($Go); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.