calculate average from rows in a database
hi, can anyone tell me how to calculate the average of data retrieves from a database. The situation is I have a table "number" which consist of several data like "2,5,4,8,6...." I want to calculate the average and display it on the browser. thanks in advance for your help.
http://www.islandinfo.mu
Hi,
I have a 2 tables: category, subCategory
Can anyone explained me how to create a menu like a tree menu except there won't be any collapsible feature.
for example
Food(category)
meat(sub)
fruits(sub)
vegetables(sub)
Furniture(category)
chair(sub)
tables(sub)
bed(sub)
as you see, I want to display the category and under the category, I want to display the sub. I am using a database for that so that I can put other items without going through the code.
I tried a lot but couldn't do it. Any help will be grateful. Thanks a lot
http://www.islandinfo.mu
create a menu
Moderator: General Moderators
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: create a menu
Two posts in one...? Okay... o_O
Get each entry from the database, run a total of how many entries you retrieve and the total of the entries, and get your average.abhikerl wrote:hi, can anyone tell me how to calculate the average of data retrieves from a database. The situation is I have a table "number" which consist of several data like "2,5,4,8,6...." I want to calculate the average and display it on the browser.
Code: Select all
$totalAmount = 0;
for ($totalEntries = 0; $data = mysql_fetch_object($queryResult); $totalEntries++) {
$totalAmount += $data->number;
}
if ($totalEntries > 0) {
$average = $totalAmount / $totalEntries;
} else {
$average = 0;
}Make sure the subCategory table has a field for the id of the parent category. Then, just output each parent category and output the children.abhikerl wrote:I have a 2 tables: category, subCategory
Can anyone explained me how to create a menu like a tree menu except there won't be any collapsible feature.
Code: Select all
$output = '<ul>';
foreach ($categories as $category) {
$output .= '<li>' . $category->name . '<ul>';
foreach ($subcategories as $subcategory) {
if ($subcategory->parentCategoryId = $category->id) {
$output .= '<li>' . $subcategory->name . '</li>';
}
}
$output .= '</ul></li>';
}