yyx748 Posted October 17, 2008 Share Posted October 17, 2008 i need to populate my table in this way but i dont seem to get it working. 4 columms. rows depend on number of items. pic1 text1 pic2 text2 pic3 text3 pic4 text4 pic5 text5 pic6 text6 ================== this are my sql codes $SQLstring = "Select * from filmo WHERE id='$id'"; $result = @mysqli_query($conn, $SQLstring); $Row = mysqli_fetch_assoc($result); ====my variables==== $Row['img'] /// image $vRow['text'] //// text i need the codes for the table like using for loop or while loop to make a table with those items inside.. thanks!! Link to comment https://forums.phpfreaks.com/topic/128865-php-loop-to-populate-table-question/ Share on other sites More sharing options...
Psycho Posted October 17, 2008 Share Posted October 17, 2008 Not tested, so there might be some errors. $records_per_row = 2; $SQLstring = "Select * from filmo WHERE id='$id'"; $result = @mysqli_query($conn, $SQLstring); if (!result) { echo "There was an error running the query<br /><br />"; echo "Query: $SQLstring<br /><br />"; echo "Error: " . @mysqli_error(); exit(); } if (mysqli_num_rows()==0) { echo "There were no results"; } else { $current_row_rec = 1; echo "<table>\n"; while ($Row = mysqli_fetch_assoc($result)) { if ($current_row_rec > $records_per_row) { echo "</tr>\n"; $current_row_rec = 1; } if ($current_row_rec == 1) { echo "<tr>\n"; } echo "<td>{$Row['img']}</td>\n"; echo "<td>{$Row['text']}</td>\n"; $current_row_rec++; } while ($current_row_rec <= $records_per_row) { echo "<td></td>\n"; echo "<td></td>\n"; $current_row_rec++; } echo "</table>\n"; } Link to comment https://forums.phpfreaks.com/topic/128865-php-loop-to-populate-table-question/#findComment-668101 Share on other sites More sharing options...
yyx748 Posted October 18, 2008 Author Share Posted October 18, 2008 oh gosh thanks so much for the help... it works now but it appears that if i have 5 items, only 4 is showed. when i hav 4 items, only 3 is showed. is something wrong with my sql statement? thannnks a million!! Link to comment https://forums.phpfreaks.com/topic/128865-php-loop-to-populate-table-question/#findComment-668513 Share on other sites More sharing options...
yyx748 Posted October 18, 2008 Author Share Posted October 18, 2008 oops i accidently added an extra sql statement on top. it works perfectly now. thank you so much!!! Link to comment https://forums.phpfreaks.com/topic/128865-php-loop-to-populate-table-question/#findComment-668515 Share on other sites More sharing options...
Psycho Posted October 18, 2008 Share Posted October 18, 2008 I think it's because I added some code to complete the row if there was not enough records to fill a row, but I forgot the ending TR tag. Change the last while loop as followsL if ($current_row_rec != $records_per_row+1) { while ($current_row_rec <= $records_per_row) { echo "<td></td>\n"; echo "<td></td>\n"; $current_row_rec++; } echo "</tr>\n"; } Link to comment https://forums.phpfreaks.com/topic/128865-php-loop-to-populate-table-question/#findComment-668516 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.