1

I have a issue with my display data into a HTML table from PHP

I get the correct data, but I don't know how to not get for each result the column names.

My PHP code:

$statement = $connect->prepare($query);
    $statement->execute();
    $result = $statement->fetchAll();
    $total_row = $statement->rowCount();
    $output = '';
    if($total_row > 0)
    {
        foreach($result as $row)
        {
            $output .= '
<table class="table">
 <thead>
    <tr>
      <th scope="col">ID</th>
      <th scope="col">Region</th>
      <th scope="col">Level</th>
      <th scope="col">Status</th>
      <th scope="col">BE</th>
      <th scope="col">RP</th>
      <th scope="col">Current Rank</th>
      <th scope="col">Previous Rank</th>
      <th scope="col">Flex Rank</th>
      <th scope="col">Champions Count</th>
      <th scope="col">Skin Count</th>
     <th scope="col">Last Play</th>
     <th scope="col">Price</th>
    </tr>
 </thead>
</tr>
<td>'.$row["id"].'</td>
<td>'.$row["region"].'</td>
<td>'.$row["level"].'</td>
<td>'.$row["account_status"].'</td>
<td>'.$row["be"].'</td>
<td>'.$row["rp"].'</td>
<td>'.$row["current_rank"].'</td>
<td>'.$row["previous_rank"].'</td>
<td>'.$row["flex_rank"].'</td>
<td>'.$row["champions_count"].'</td>
<td>'.$row["skins_count"].'</td>
<td>'.$row["last_play"].'</td>
<td>'.$row["price"].'</td>
 ';

        }
    }
    else
    {
        $output = '<h3>No Data Found</h3>';
    }
    echo $output;

HTML:

    <div class="col-md-9">
        <div class="row filter_data">

        </div>
    </div>

The filter_data comes from Ajax.

https://prnt.sc/rd4t3f - Results

I want to receive just in table just information from column and not every time column name

1
  • 1
    You're creating a new table in each iteration of your loop. Extract the common parts outside the loop and only create new rows in the loop. Commented Mar 7, 2020 at 21:53

1 Answer 1

3

Iterate only for rows

 $statement = $connect->prepare($query);
    $statement->execute();
    $result = $statement->fetchAll();
    $total_row = $statement->rowCount();
    $output = '';
    if($total_row > 0)
    {

    $output .= '
        <table class="table">
         <thead>
            <tr>
              <th scope="col">ID</th>
              <th scope="col">Region</th>
              <th scope="col">Level</th>
              <th scope="col">Status</th>
              <th scope="col">BE</th>
              <th scope="col">RP</th>
              <th scope="col">Current Rank</th>
              <th scope="col">Previous Rank</th>
              <th scope="col">Flex Rank</th>
              <th scope="col">Champions Count</th>
              <th scope="col">Skin Count</th>
             <th scope="col">Last Play</th>
             <th scope="col">Price</th>
            </tr>
         </thead>
        '

        foreach($result as $row)
        {
           $output .= ' <tr>
           <td>'.$row["id"].'</td>
           <td>'.$row["region"].'</td>
           <td>'.$row["level"].'</td>
           <td>'.$row["account_status"].'</td>
           <td>'.$row["be"].'</td>
           <td>'.$row["rp"].'</td>
           <td>'.$row["current_rank"].'</td>
           <td>'.$row["previous_rank"].'</td>
           <td>'.$row["flex_rank"].'</td>
           <td>'.$row["champions_count"].'</td>
           <td>'.$row["skins_count"].'</td>
           <td>'.$row["last_play"].'</td>
           <td>'.$row["price"].'</td>
          </tr>
          ';

         }

     $output .= ' </table>'
}
else
{
    $output = '<h3>No Data Found</h3>';
}
echo $output;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.