php output from mysql to html table

17,665

Replace the while inside the if with this:

echo '<table class="table table-striped table-bordered table-hover">'; 
echo "<tr><th>Name</th><th>Description:</th><th>Status</th></tr>"; 
while($row = mysqli_fetch_array($results))
{
  echo "<tr><td>"; 
  echo $row['name'];
  echo "</td><td>";   
  echo $row['des'];
  echo "</td><td>";    
  echo $row['status'];
  echo "</td></tr>";  
}
echo "</table>";    
Share:
17,665

Related videos on Youtube

David_Hogan
Author by

David_Hogan

Updated on July 04, 2022

Comments

  • David_Hogan
    David_Hogan almost 2 years

    currently I'm working on a site uptime search engine however I'm having a silly issue. I want to output more than one mysql row in a table however my code below causes a separate table to be created for each row thats found. Thank you in advance for your assistance

     $searchTerm = trim($_GET['searchterm']);
    
     //check whether the name parsed is empty
     if($searchTerm == "")
    {
    echo "Please enter something to search for...";
    exit();
    } 
    
    //database connection info
    $host = "localhost"; //server
    $db = "DB NAME"; //database name
    $user = "USER"; //dabases user name
    $pwd = "PASSWORD"; //password
    
    
    $link = mysqli_connect($host, $user, $pwd, $db);
    
    
     $query = "SELECT * FROM sites WHERE name OR des LIKE '%$searchTerm%'";
    
     $results = mysqli_query($link, $query);
    
    
     if(mysqli_num_rows($results) >= 1)
     {
    
    while($row = mysqli_fetch_array($results))
    {
    echo '<table class="table table-striped table-bordered table-hover">'; 
    echo"<TR><TD>Name</TD><TD>Description:</TD><TD>Status</TD></TR>"; 
    echo "<tr><td>"; 
    echo $row['name'];
    echo "</td><td>";   
    echo $row['des'];
    echo "</td><td>";    
    echo $row['status'];
    echo "</TD></tr>";  
    echo "</table>";    
    }
    
        }
        else
    echo "There was no matching record for the name " . $searchTerm;
    ?>
    
    • Ichigo Kurosaki
      Ichigo Kurosaki over 10 years
      You can put your table tag outside the loop and only tr in the loop
  • Patrick Kostjens
    Patrick Kostjens over 10 years
    You are printing the headers between all the rows.