简体   繁体   中英

Display a table in while loop

I am trying to display a table in while loop with 3 rows and 3 td for each row. But I am confusing how to archived thit.

This is my code so far:

while($row = mysqli_fetch_array($result)){

    $testimonial    = $row['testimonial'];
  $cityName         = $row['city_name'];
  $name             = $row['name'];
  $imageName        = $row['image_name'];
  $member           = $row['membership_type'];
  $testimonial  = nl2br($testimonial);

    //Create new testimonial output
    $output  = "<table>\n";
    $output .= "  <tr>\n";
    $output .= "     <td>\n";
    $output .= "      <p>\n";
    $output .= "        <img src=\"".UPLOAD_DIR.$imageName."\" />";
    $output .= "         {$testimonial}";
    $output .= "      </p>\n";
    $output .= "     <p class=\"name\">{$name}<br />\n";
    $output .= "      <span>A Teacher - From {$cityName}</span></p>\n";
    $output .= "   </td>\n";
    $output .= "  </tr>\n";
    $output .= "</table>\n";

    echo $output;
}

The above code given me a table with multiple rows and 1 td for each row. But it is not my expecting result.

Can anybody tell me how can I display such a table in my While loop ?

The current code creates a new table for every single entry. What you want is to move the <table> and </table> pieces outside of the loop, then you want a counter to keep track of how many cells you've handled. If it's even, start a new <tr> . Otherwise, end the current </tr> . Make sure you check at the end to see if you've finished a </tr> , and optionally add an empty <td></td> if needed.

Try this

$i = 0;
echo "<table>\n<tr>";
while($row = mysqli_fetch_array($result)){
  $testimonial    = $row['testimonial'];
  $cityName         = $row['city_name'];
  $name             = $row['name'];
  $imageName        = $row['image_name'];
  $member           = $row['membership_type'];
  $testimonial  = nl2br($testimonial);
  //Create new testimonial output
  $output .= "     <td>\n";
  $output .= "      <p>\n";
  $output .= "        <img src=\"".UPLOAD_DIR.$imageName."\" />";
  $output .= "         {$testimonial}";
  $output .= "      </p>\n";
  $output .= "     <p class=\"name\">{$name}<br />\n";
  $output .= "      <span>A Teacher - From {$cityName}</span></p>\n";
  $output .= "   </td>\n";
  echo $output;
  if( $i %2 == 1 )
    echo "</tr><tr>\n";
  $i++;
}
echo "</tr>\n</table>\n";

EDIT

In general, for displaying n cells per row, change the if statement to

if( $i % n == (n - 1) )

this aught to do it

<?php

echo "<table>\n";

$cells = $total = 0;
$total_cells = mysqli_num_rows($result);

while($row = mysqli_fetch_array($result))
{
    $testimonial      = nl2br($row['testimonial']);
    $cityName         = $row['city_name'];
    $name             = $row['name'];
    $imageName        = $row['image_name'];
    $member           = $row['membership_type'];

    if ($cells === 0)
        echo "<tr>\n";

    //Create new testimonial output
    $output = "     <td>\n";
    $output .= "      <p>\n";
    $output .= "        <img src=\"".UPLOAD_DIR.$imageName."\" />";
    $output .= "         {$testimonial}";
    $output .= "      </p>\n";
    $output .= "     <p class=\"name\">{$name}<br />\n";
    $output .= "      <span>A Teacher - From {$cityName}</span></p>\n";
    $output .= "   </td>\n";

    echo $output;

    $cells++;
    $total++;       

    if ($cells === 3 || $total === $total_cells)
    {
        echo "</tr>\n";
        $cells = 0;
    }
}

echo "</table>\n";

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM