0

I have tried to prepare a data table where any list query is passed its results would be sown in a table.

I have used 2 if conditions and foreach loop. Now i have doubt that this is would slow down if bulk records are fetched.. Can anyone suggest me much better way to do this..

Query as follows

 echo "<table width='100%' align='center' border=1 style='text-align:center; vertical-align:center; border-collapse:collapse; font-size:80%' class='main'>";
    $i=0;
    while($row=mysqli_fetch_array($result)){
      echo "<tr>";
        foreach($row as $rowvalues => $cellvalues){
            if (!is_numeric($rowvalues)){
               if($i==0){ 
                    echo "<th>".$rowvalues."</th>"; // For Table header
               }else{
                    echo "<td>".$cellvalues."</td>"; // For field values
               }
            }
        }
      echo "</tr>"; 
      $i++;
    }
    echo "</table>";

2 Answers 2

1

maybe this is a bit faster? (reduce the amount of echos)

$output = "<table width='100%' align='center' border=1 style='text-align:center; vertical-align:center; border-collapse:collapse; font-size:80%' class='main'>";
    $i=0;
    while($row=mysqli_fetch_array($result)){
      $output .= "<tr>";
        foreach($row as $rowvalues => $cellvalues){

            if (!is_numeric($rowvalues)){       
                $output .= ($i == 0) ? "<th>".$rowvalues."</th>" : "<td>".$cellvalues."</td>";              
            }
        }
      $output .= "</tr>"; 
      $i++;
    }
    echo $output."</table>";
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Michale, I welcome your attempt. and i have tried it but i didn't see major difference. still i feel code can be tweaked.
I don't see any more potential. If speed really matters you should use Perl instead of PHP.
you can decouple html generation from db fetching, MVC frameworks are getting db data from the model. the view is rendering the data to html ...
0

Create a php pagination to avoid slowing down of fetching records in your database.

1 Comment

Yes that i have already done; but still i feel that some my code can be adjusted in a better way.

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.