Jump to content

PHP Loops


damju87
 Share

Recommended Posts

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

have you tried anything?

Link to comment
Share on other sites

<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 by dsonesuk
Link to comment
Share on other sites

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

have you tried anything?
I tried the different ones already.
  1. If condition is true, execute <td>['field']</td>
  2. Then if I have more data in the database added, it should create more <td>['field']</td>
  3. When it reach a limit per row, it will create a new row.
  4. Repeat 1-3.

Edited by damju87
Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...