damju87 Posted May 5, 2012 Share Posted May 5, 2012 How do I set this kind of loop? First, it fills the cell with the data from the database. <table border="1"><tr><td>Row 1, cell 1</td></tr></table> Then, if there is more data added into the database. <table border="1"><tr><td>Row 1, cell 1</td><td>Row 1, cell 2</td></tr></table> Here's where I want the loop to start. I want it to repeat the process when the row has two cells by creating a new row. Link to comment Share on other sites More sharing options...
birbal Posted May 5, 2012 Share Posted May 5, 2012 have you tried anything? Link to comment Share on other sites More sharing options...
dsonesuk Posted May 5, 2012 Share Posted May 5, 2012 (edited) <table width="960" border="1" cellspacing="0" cellpadding="0"><?php $column_count = 6; $row_count = 6;if(isset($_POST['row_count']) && !empty($_POST['row_count']) && isset($_POST['column_count']) && !empty($_POST['column_count'])){ $column_count = $_POST['column_count']; $row_count = $_POST['row_count'];} $cell_count =0; for($row=1;$row<$row_count+1;$row++){echo '<tr>';for($col=1;$col<$column_count+1;$col++) { $cell_count++; echo '<td>'.$cell_count.'</td>'; }echo '</tr>';} ?></table><form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">Table rows <input type="text" name="row_count" /><br />Table columns <input type="text" name="column_count" /><br /><input type="submit" /></form> Edited May 5, 2012 by dsonesuk Link to comment Share on other sites More sharing options...
dsonesuk Posted May 5, 2012 Share Posted May 5, 2012 for database listing a quick method is <table width="960" border="1" cellspacing="0" cellpadding="0"><?phpwhile($row = mysql_fetch_row($result)) { echo "<tr>"; foreach($row as $column_value) { echo '<td>'.$column_value.'</td>'; } echo '</tr>'."\n"; } ?></table> Link to comment Share on other sites More sharing options...
damju87 Posted May 6, 2012 Author Share Posted May 6, 2012 (edited) have you tried anything? I tried the different ones already. If condition is true, execute <td>['field']</td> Then if I have more data in the database added, it should create more <td>['field']</td> When it reach a limit per row, it will create a new row. Repeat 1-3. Edited May 6, 2012 by damju87 Link to comment Share on other sites More sharing options...
dsonesuk Posted May 6, 2012 Share Posted May 6, 2012 if you use $result = mysql_query("SELECT * FROM mytable");if (!$result) {die("Query to show fields from table failed");} <table width="960" border="1" cellspacing="0" cellpadding="0"><?phpwhile($row = mysql_fetch_row($result)) { echo "<tr>"; foreach($row as $column_value) { echo '<td>'.$column_value.'</td>'; } echo '</tr>'."\n"; } ?></table> will loop through rows with while loop applying the starting <tr>, and then foreach will loop through ALL the fields within a row, applying within <td>, and when the foreach loop has finished, it applies the finishing </tr> closing tag. If you want only specific fields then add the field names in the SQL statement and not use wildcard * $result = mysql_query("SELECT fieldname1, fieldname2, fieldname3 FROM mytable");if (!$result) {die("Query to show fields from table failed");} Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now