MySQL - PHP - How to filter text and calculate average
Posted: Tue Nov 01, 2011 4:17 pm
[text]Hi,
I have records in MySQL database with text like this:
- Good job, 7
- Beautiful lines, an 8
I would like to filter the text with php so only numbers are left. I will show the numbers behind the post as rating. After that I would like to calculate the average of all ratings and save it in a mysql record behind the subject rated.
I have got the following code which shows only the numbers, but I do not know how to calculate the average and put it in a record. Anyone any idea? Is the question clear?[/text]
I have records in MySQL database with text like this:
- Good job, 7
- Beautiful lines, an 8
I would like to filter the text with php so only numbers are left. I will show the numbers behind the post as rating. After that I would like to calculate the average of all ratings and save it in a mysql record behind the subject rated.
I have got the following code which shows only the numbers, but I do not know how to calculate the average and put it in a record. Anyone any idea? Is the question clear?[/text]
Code: Select all
//CONNECTION MADE
$query = "SELECT review FROM reviews ";
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0){
while ($row = mysql_fetch_assoc($query)){
$review = $row['review'];
$id = $row['id'];
$string = "$review";
preg_match('/\d+/', $string, $number);
$number = $number[0];
echo "
$number
";
}
}