有没有比使用echo更好的显示结果的方法

I posted in a previous question how to get my rating script to work. I have now finally got it to work using maths rather than the avg function but i still have two questions to ask.

Q1) Is there a better way of displaying my results other than using echo all the time

Q2) How do i add in my other three performance values in to this script they are all in the same table

<?php
mysql_connect("localhost", "username", "password") or die ("sorry we could not connect to our servers");
mysql_select_db("db") or die ("sorry we could not our database");

$find_data = mysql_query("SELECT * FROM rating");

while($row = mysql_fetch_assoc($find_data))
{
$id = $row['id'];
$current_rating = $row['Reliability_timekeeping'];
$reviews = $row['reviews'];
$new_rating = $current_rating / $reviews;
echo "($reviews Reviews)";
echo "Reliability & timekeeping: ";
echo round($new_rating,1);
echo "/10";
}
?>

sorry for my writing skill writing is not my strong point and still new to php

To avoid using echo all the time, you can just accumulate all the output in a variable and use a single echo. As a good practice, in my scripts I try to use just one echo at the script end. This will make each page a few mili-seconds faster, as less PHP / web server interactions will be necessary:

$Response = "";
$Response .= "($reviews Reviews)".
             "Reliability & timekeeping: ".round($new_rating,1)."/10";

...
more code
...

echo $Response;

Just indent and break lines in a way that the code gets readable, and everything will be fine.

You can use a template engine that fills your data in to a template that you can write like HTML with some additional placeholders. A template engine has some great advantages over echoing out the values:

  • separate code from layout
  • reusable
  • better maintainability and readability

Here are some of the template engines I used in PHP, they also have good documentation to get you started: